繁体   English   中英

将字符串数组从C#传递到C ++ Com

[英]Passing array of strings from C# to C++ Com

我需要将字符串数组从C#模块传递到C ++ COM组件。 以下是idl声明;

[id(11), helpstring("method deleteObjectsEx")] HRESULT deleteObjectsEx(
                [in] BSTR userName,
                [in] BSTR userPasswd, 
                [in] SAFEARRAY(VARIANT) varValues, 
                [in] BSTR deleteMode
                );

在C#中,我们使用以下代码来调用API

List<string> ObjectIDS = new List<string>();
ObjectIDS.Add(obj._ObjectId[0]);
ObjectIDS.Add(obj._ObjectId[1]);

/*Array ar = Array.CreateInstance(typeof(string), size);
ar.SetValue(obj._ObjectId[0], 0);
ar.SetValue(obj._ObjectId[1], 1);*/

mhubBridge.deleteObjectsEx(Encrypt(auth.UserName), 
                           Encrypt(auth.UserPassword), 
                           ObjectIDS.ToArray(),
                           obj._delMEthod);

在调用deleteObjectsEx API时,我得到“类型为System.Runtime.InteropServices.SafeArrayTypeMismatchException occurred in IPDWebService.DLL IPDWS::trace::(Tuesday, 06 August 2013 13:27): Exception in deleteObjectsEx:: message - Specified array was not of the expected type.第一次机会异常System.Runtime.InteropServices.SafeArrayTypeMismatchException occurred in IPDWebService.DLL IPDWS::trace::(Tuesday, 06 August 2013 13:27): Exception in deleteObjectsEx:: message - Specified array was not of the expected type.

不起作用

尝试使用ar数组。

mhubBridge.deleteObjectsEx(Encrypt(auth.UserName), 
                           Encrypt(auth.UserPassword), 
                           ar,
                           obj._delMEthod);

如果ObjectIDS ,请删除所有ObjectIDS “物件”。

或尝试:

工作中

object[] ar = new object[] { obj._ObjectId[0], obj._ObjectId[1] };

并将其传递给deleteObjectsEx(...)

因为从技术上讲, VARIANTobject ,所以SAFEARRAY(VARIANT)object[]

尝试这个:

object[] ar = new object[2];
ar [0] = obj._ObjectId[0];
ar [1] = obj._ObjectId[1];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM