Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top