我在Memory management in COM中读到com可以编组内存块。但是我怎么能把c ++ com客户端的内存块编写成用c#编写的进程外服务器呢?
我有以下界面&用C#编写的实现:
[Guid(...), ComVisible(true)]
public interface ISomeInterface
{
void DoSomething(IntPtr memoryBlockPtr);
}
[ClassInterface(ClassInterfaceType.None)]
[Guid(...), ComVisible(true)]
public class SomeObject : ISomeInterface
{
public void DoSomething(IntPtr memoryBlockPtr) {
byte b = Marshal.ReadByte(memoryBlockPtr);
...
}
}
在tlb中,此界面变为:
interface ISomeInterface : IDispatch {
HRESULT DoSomething([in] long memoryBlockPtr);
};
在c ++ com客户端中,我想将CoTaskMemAlloc分配的内存传递给服务器
char * memoryBlockPtr = (char*)CoTaskMemAlloc(size);
// write to memoryBlockPtr variable length structure
// which could have char* fields that point to another memory block
// allocated by CoTaskMemAlloc
HRESULT hr = ISomeInterface->DoSomething((long)memoryBlockPtr);
但是这个调用失败是因为com编组长而不是内存块指针而com服务器获取指向错误内存块的IntPtr。