sameer karjatkar sameer karjatkar - 2 months ago 9
C# Question

Passing array of strings from C# to C++ Com

I have requirement of passing an array of strings from a C# module to my C++ COM component. The following is the idl declaration;

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


And from the C# we use the following code to invoke the 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);


On invoking the deleteObjectsEx API i get "A first chance exception of 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.

Answer

(not working)

Try using the ar array.

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

If it works, remove all the ObjectIDS "things".

or try:

(working)

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

and pass it to the deleteObjectsEx(...)

because technically a VARIANT is an object, so a SAFEARRAY(VARIANT) is an object[].

Comments