2

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.

3
  • What is the C# declaration of deleteObjectsEx ? (if you imported the .tlb, one has probably been auto-generated) Commented Aug 6, 2013 at 9:18
  • @Medinoc its public virtual void deleteObjectsEx(string userName, string userPasswd, Array varValues, string deleteMode); Commented Aug 6, 2013 at 9:20
  • @xantos Sorry for the extra code ...i was trying out different combinations Commented Aug 6, 2013 at 9:39

2 Answers 2

1

(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[].

Sign up to request clarification or add additional context in comments.

1 Comment

@sameerkarjatkar Try the object[] (second example). Remember to pass it to the deleteObjectsEx!
0

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.