开发者

How to send an event callback parameter to a COM object with .net

开发者 https://www.devze.com 2023-02-06 01:16 出处:网络
I have a referenced a COM dll. Some methods expect a callback parameter. I checked the interop.MyComLib.dll in reflector:

I have a referenced a COM dll. Some methods expect a callback parameter. I checked the interop.MyComLib.dll in reflector:

public virtual extern void Foo([In, MarshalAs(UnmanagedType.IDispatch)] object pDispProgressCallBack);

How do I send this parameter? I can'开发者_开发知识库t send a delegate (compilation exception). I have tried sending this and received InvalidCastException.


IDispatch is a COM interface. The CLR will automatically implement it if you use [ComVisible(true)] and [ClassInterface(ClassInterfaceType.AutoDispatch)] attributes on your class. You can then pass an instance of the class and the cast will succeed. The code is then probably going to call some kind of method on that class so be sure that it is implemented. It isn't clear from the question what method that might be and what its signature should look like. It must exactly match, a mismatch is liable to prevent the callback from ever occurring without a diagnostic.


You want to pass in an object that's an IDispatch and has a method with a DispId of 0. See this article for how to create an IDispatch, then give it a single method with DispId(0).

0

精彩评论

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