how to put IDispatch* in managed code

↘锁芯ラ 提交于 2019-12-11 01:59:03

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/21472700/how-to-put-idispatch-in-managed-code

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