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;
}
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;
}
试传
SafeArray or byte[]
或者你的mystream 实现 ISTREAM 接口,且表极为COM的
[ClassInterface(ClassInterfaceType.AutoDispatch)] 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)
{
// throw new Exception("The method or operation is not implemented.");
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);
}
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()
{
byte[] bb=new byte[3];
bb[0]=6;
bb[1]=1;
bb[2]=2;
Write(bb, 0, 3);
} #endregion
}
可com传不出来,唉
http://community.csdn.net/Expert/topic/5340/5340086.xml?temp=.9813959