开发者

Access "THIS_" COM parameter via C# ComImport

Most com methods have a signature like this:

STDMETHOD(someFunc) (THIS_ ParamType param) PURE;

Which translates to C# via ComImport as:

[PreserveSig()]
int someFunc(MarshaledParamType param);

Is there some way to preserve the "THIS_" parameter in the PInvoke signature? So that I can do something like:

int someFunc(IntPtr ptrToCOMInstance, MarshaledParamType param);

or

int someFunc(IMyCOMClass comInstance, M开发者_Go百科arshaledParamType param);


That's a fake annotation, representing the this pointer that gets passed to a class method. You don't actually declare it in neither C++ nor C#, it gets passed without writing it out explicitly.

But do note the difference between the server and the client. In the server, you use the this keyword to recover that pointer. In the client, you have the object reference. For example:

Word.Application app = new Word.Application();
Word.Document doc = new Word.Document();

The app and doc variables are the object references you are looking for. The value of this inside Word's implementation of the Application and Document interfaces. Ignoring the intricacies of RCWs for a moment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜