将数组结构传递到COM接口 [英] Passing an array of Structs to a COM Interface

查看:99
本文介绍了将数组结构传递到COM接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有IDL文件的COM接口,声明了以下内容:

I have a COM interface with an IDL file with the following declared:

typedef [uuid(D7B6C495-FFF3-11E0-8A39-08002700D831)]
struct PORT_CONFIG
{
  unsigned char  rack;
  unsigned short port;
  unsigned char  offset;
} PORT_CONFIG;

[object, uuid(D7B6C492-FFF3-11E0-8A39-08002700D831), dual, nonextensible, pointer_default(unique)]
interface IMED704 : IDispatch
{
  [id(5), helpstring("method PortConfig")] HRESULT PortConfig([in] SAFEARRAY(PORT_CONFIG) portCfg, [in, defaultvalue(-1)] VARIANT_BOOL clearInputs);
};

现在在我的C#程序中,我试图调用PortConfig方法:

Now in my C# program I am trying to call the PortConfig method:

PORT_CONFIG[] portCfg = new PORT_CONFIG[12];

// ...Initialize code goes here

dig704.PortConfig(portCfg, true);

但是,调用该方法时程序会引发异常。我在做什么错?

However the program throws an exception when the method is called. What am I doing wrong?

例外是:

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

如果我尝试以下操作,将获得更多信息:

More information if I try the following:

IntPtr pointer = Marshal.GetITypeInfoForType(typeof(PORT_CONFIG));

我收到的例外是:

The specified type must be visible from COM.\r\nParameter name: t


推荐答案

我现在可以解决自己的问题。由于某种原因,当嵌入互操作类型时(VS2010中的默认设置),互操作层在SAFEARRAY参数上失败。要变通解决此问题,请右键单击对COM对象的引用,然后将Embed Interop Type设置为False。

I now have the answer to my own problem. For some reason the interop layer fails on SAFEARRAY parameters when the interop type is embedded (the default in VS2010). To work around this issue right-click the reference to the COM object and set Embed Interop Type to False.

我希望我能为找出这个答案而功劳,但信用归Michael Taylor所有:

I wish that I could take credit for figuring out this answer, but the credit belongs to Michael Taylor:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/1325d24c-db0f-43a1-9780-b68a843d816b

这篇关于将数组结构传递到COM接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆