PInvoke and IStream

◇◆丶佛笑我妖孽 提交于 2019-12-11 17:13:39

问题


I have an exported function from a dll written in c++ with the following signiture:

Foo( LPSTREAM *pStream, UINT &Size ) 

that returns an memory stream and obviously its size. What I am having difficulties with is creating a signiture for the exported function and then attempting to read the stream in C#. At one point was able to use "unsafe" byte pointer to get the information, but this does not fit our requirements.

Any thoughts, examples, samples etc would be greatly appreciated.


回答1:


You can do this like so:

[DLLImport(@"mydll.dll")]
public static extern void Foo(out ComTypes.IStream Stream, ref uint Size);

Call it like this:

ComTypes.IStream Stream = null;
uint Size;
Foo(out Stream, ref Size);

As normal, make sure your calling conventions match (C# defaults to stdcall, C++ defaults to cdecl).


As an aside, why return the Size separate from the stream since IStream knows its size and will tell you if you ask it to?



来源:https://stackoverflow.com/questions/6361314/pinvoke-and-istream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!