我一直在考虑使用实现的C#来编写COM对象。我在C++中使用自动化和MFC做过这件事,这并不太困难。所以我被困在其中一种方法上,试图把它转换过来。我将排除接口中的其他方法,因为它们是直接向前的(至少我希望如此)。
[id(6), helpstring("method OpenService")]
LONG OpenService(BSTR lpclDevClass, BSTR lpclDevName, IDispatch* lpDispatch);到目前为止,我的C#代码看起来是这样的,但是我被困在了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.
}正如您所看到的,我不知道该为IDis补丁写什么*。我在这个箱子里用什么?
发布于 2014-01-31 06:36:19
您不需要为COM IDispatch创建托管定义或显式实现其成员。该框架对此有内置的支持。只需像这样声明您的OpenService:
[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
复制相似问题