簡體   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