如何将IDispatch *放入托管代码中 [英] how to put IDispatch* in managed code
问题描述
我一直在考虑尝试使用实现OPOS服务对象的C#编写COM对象.我已经在C ++中使用Automation和MFC完成了这一任务,这并不难.因此,我陷入了尝试将其转换过来的方法之一.我会在界面中排除其他方法,因为它们很简单(或者我希望如此).
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);
到目前为止,我的C#代码看起来像这样,但是我被困在OpenService上.
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.
}
如您所见,我不知道要为IDispatch *添加什么.在这种情况下我该怎么用?
as you can see I don't know what to put for IDispatch*. What do I use in this case?
推荐答案
您无需为COM IDispatch
创建托管定义或显式实现其成员.该框架具有内置支持.只需这样声明您的OpenService
:
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);
这篇关于如何将IDispatch *放入托管代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!