熟悉com和c#看过来,用IStream传输数据,第三贴了
现在准备在c#边传输文件到com,通过IStream
class MyIStream : MemoryStream, IStream
{
public MyIStream()
: base()
{
}
#region IStream 成员
public void Clone(out IStream ppstm)
{
throw new Exception( "The method or operation is not implemented. ");
}
public void Commit(int grfCommitFlags)
{
throw new Exception( "The method or operation is not implemented. ");
}
public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
{
throw new Exception( "The method or operation is not implemented. ");
}
public void LockRegion(long libOffset, long cb, int dwLockType)
{
throw new Exception( "The method or operation is not implemented. ");
}
public void Read(byte[] pv, int cb, IntPtr pcbRead)
{
long bytesRead = Read(pv, 0, cb);
if (pcbRead != IntPtr.Zero)
Marshal.WriteInt64(pcbRead, bytesRead);
}
public void Revert()
{
throw new Exception( "The method or operation is not implemented. ");
}
public void Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition)
{
//throw new Exception( "The method or operation is not implemented. ");
long pos = base.Seek(dlibMove, (SeekOrigin)dwOrigin);
if (plibNewPosition != IntPtr.Zero)
Marshal.WriteInt64(plibNewPosition, pos);
//Seek(0, SeekOrigin.Begin);
}
public void SetSize(long libNewSize)
{
throw new Exception( "The method or operation is not implemented. ");
}
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag)
{
throw new Exception( "The method or operation is not implemented. ");
}
public void UnlockRegion(long libOffset, long cb, int dwLockType)
{
throw new Exception( "The method or operation is not implemented. ");
}
public void Write(byte[] pv, int cb, IntPtr pcbWritten)
{
Write(pv, 0, cb);
if (pcbWritten != IntPtr.Zero)
Marshal.WriteInt64(pcbWritten, (Int64)cb);
}
public void Write(System.Drawing.Bitmap bmp)
{
bmp.Save(this,System.Drawing.Imaging.ImageFormat.Jpeg);
}
#endregion
}
////////////////////////////////////////////
上面类是c#从IStream com接口派生的
////////////////////////////////////////////
//com
TESTLib.tttClass bb = new TESTLib.tttClass();
MyIStream st=new MyIStream();
//传递istream 到com
bb.pic(st);
////////////////////////////////////////////
com端
////////////////////////////////////////////
STDMETHODIMP Cttt::pic(LPUNKNOWN Stream)
{
IStream* tmpStream;
tmpStream=(IStream* )Stream;
LARGE_INTEGER lnOffset;
lnOffset.QuadPart = 0;
ULARGE_INTEGER ulnSize;
lpStream-> Seek(lnOffset, STREAM_SEEK_END, &ulnSize);
lpStream-> CopyTo(tmpStream,ulnSize,NULL,NULL);
//把成员变量IStream lpStream的内容拷贝到Stream
*************************************************
问题就出在这,拷贝成功,可传出去的stream为空
tmpStream-> Seek(lnOffset, STREAM_SEEK_END, &ulnSize);
取tmpStream的长度时出错
*************************************************
}
[解决办法]
http://www.sqlxml.org/faqs.aspx?faq=98
[解决办法]
顶
[解决办法]
帮LZ顶
[解决办法]
顶
[解决办法]
帮顶
[解决办法]
帮顶
[解决办法]
检查你的C++代码.