C# 将结构数组传递给COM接口
我有一个带有IDL文件的COM接口,声明如下:C# 将结构数组传递给COM接口,c#,com,parameter-passing,C#,Com,Parameter Passing,我有一个带有IDL文件的COM接口,声明如下: 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, nonext
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方法:
PORT_CONFIG[] portCfg = new PORT_CONFIG[12];
// ...Initialize code goes here
dig704.PortConfig(portCfg, true);
但是,当调用该方法时,程序会引发异常。我做错了什么
例外情况是:
The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
如果我尝试以下操作,请提供更多信息:
IntPtr pointer = Marshal.GetITypeInfoForType(typeof(PORT_CONFIG));
我收到的例外情况是:
The specified type must be visible from COM.\r\nParameter name: t
我现在有了自己问题的答案。由于某种原因,当嵌入互操作类型(VS2010中的默认值)时,互操作层在SAFEARRAY参数上失败。要解决此问题,请右键单击对COM对象的引用,并将“嵌入互操作类型”设置为False 我希望我能为找到这个答案而获得荣誉,但荣誉属于迈克尔·泰勒:
帮助我们帮助您Gary-例外情况详细信息是什么?