c#给com传入istream,居然取不出来
c#端代码
MyIStream ms = new MyIStream();
ms.Write();
//MyIStream派生于MemoryStream和IStream
//即class MyIStream : MemoryStream, IStream
//ms.write调用的是下面函数
// public void Write()
// {
// byte[] bb=new byte[3];
// bb[0]=6;
// bb[1]=1;
// bb[2]=2;
// Write(bb, 0, 3);
// }
ms.Seek(0, SeekOrigin.Begin);
TESTLib.BBClass bb = new TESTLib.BBClass();
bb.pic(ms);
com端代码
STDMETHODIMP BB::pic(LPUNKNOWN Stream)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
CComQIPtr <IStream> pStream = Stream;
CFile fFile;
ULARGE_INTEGER ulnSize;
LARGE_INTEGER lnOffset;
lnOffset.QuadPart = 0;
pStream-> Seek(lnOffset, STREAM_SEEK_END, &ulnSize);
pStream-> Seek(lnOffset, STREAM_SEEK_SET, NULL);
if(fFile.Open(_T( "c:\\test.txt "), CFile::modeCreate | CFile::modeWrite))
{ char *pBuff = new char[ulnSize.QuadPart];
ULONG ulBytesRead;
ULARGE_INTEGER aa;
//pStream-> Seek(lnOffset, STREAM_SEEK_SET, &aa);
if(pStream-> Read(pBuff, ulnSize.QuadPart, &ulBytesRead)!= S_OK)
{
delete pBuff;
// return S_OK;
}
/************************/
//取出来的pbuff为空,但ulBytesRead读出来了为3
fFile.Write(pBuff, ulBytesRead);
fFile.Close();
delete pBuff;
}
return S_OK;
}
------解决方案--------------------.NET 的STREAM 和 COM的STREAM 不是一个东西,不能直接互用
试传
SafeArray or byte[]
或者你的mystream 实现 ISTREAM 接口,且表极为COM的
------解决方案--------------------try
[Guid(IStream 的guid)]
------解决方案--------------------关注