Question

I've been considering trying to write a COM object using C# that implements a OPOS Service Object. I've done it in C++ using Automation and MFC and it wasn't too difficult. So I'm stuck on one of the methods trying to convert it over. I'll exclude the other methods in the interface since they are straight forward (or so I hope).

[id(6), helpstring("method OpenService")]
LONG OpenService(BSTR lpclDevClass, BSTR lpclDevName, IDispatch* lpDispatch);

My C# code looks something like this so far, but i'm stuck on OpenService.

[ComVisible(true)]
[Guid("76F8309C-3837-4065-960F-BE156383896D")]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class IErtMSR
{
    [DispId(1)]
    int COFreezeEvents([MarshalAs(UnmanagedType.VariantBool)] bool Freeze);
    [DispId(2)]
    int GetPropertyNumber([In] int lPropIndex);
    [DispId(3)]
    void SetPropertyNumber([In] int lPropIndex, [In] int nNewValue);
    [DispId(4), MarshalAs(UnmanagedType.BStr)]
    string GetPropertyString([In] int lPropIndex);
    [DispId(5)]
    void SetPropertyString([In, MarshalAs(UnmanagedType.BStr)] string StringData);
    [DispId(6)]
    int OpenService([In, MarshalAs(UnmanagedType.BStr)] string lpclDevClass, [In, MarshalAs(UnmanagedType.BStr)] string lpclDevName, IDispatch* lpDispatch);
    //...the rest of the 24 methods.
}

as you can see I don't know what to put for IDispatch*. What do I use in this case?

Was it helpful?

Solution

You don't need to create a managed definition for COM IDispatch or implement its members explicitly. The Framework has a built-in support for it. Just declare your OpenService like this:

[DispId(6)]
int OpenService(
    [In, MarshalAs(UnmanagedType.BStr)] string lpclDevClass, 
    [In, MarshalAs(UnmanagedType.BStr)] string lpclDevName, 
    [In, MarshalAs(UnmanagedType.IDispatch] object lpDispatch);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top