Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,
I have a C# DLL with a call like this:
[DispId(7)]
void Test([MarshalAs(UnmanagedType.SafeArray,
                      SafeArraySubType = VarEnum.VT_BSTR)] ref string[] s);

On the C++ side, the function is called:
SAFEARRAY *sArr1 = NULL;
iface.Test(&sArr1);

I'm getting a message box: "The parameter is incorrect."

Looking at the output debugging window, I see this:
Warning: constructing COleException, scode = E_INVALIDARG ($80070057).

Does anyone know of other ways to pass an array as a parameter, C# DLL and C++ client?

Any help would be appreciated.


Sincerely,
Mike
Posted
Updated 7-Dec-10 3:41am
v3

OK, I found a solution to my problem.

//C# DLL
public void Test(ref object s)
{
   s = new string[2] { "one", "three" };
}

//C++ call
VARIANT s = COleVariant();
BOOL b = iface.CreateDispatch( ... );  //Parameter omitted for conciseness
iface.Test(&s);


Thank you all for your efforts.
 
Share this answer
 
Admittedly, I can't read C#, but hopefully you'll get some help from this post anyway.

Something I noticed; you have a variable sArr1, which is a pointer to a SAFEARRAY. When you call your Test function, the argument is a pointer to this pointer to a SAFEARRAY; you should maybe call it like so:

SAFEARRAY *sArr1 = NULL;
iface.Test(sArr1);


It's not impossible you actually want to use this double pointer though.

Second idea; the message box comes from within the DLL, it may simply be complaining about the NULL-value.
 
Share this answer
 
v3
Comments
mla154 6-Dec-10 17:29pm    
The argument is of type SAFEARRAY**.
mla154 7-Dec-10 9:29am    
It may be complaining about the NULL-value, but I have tried other things as well, such as:

//C++ code
SAFEARRAYBOUND sab[1];
SAFEARRAY *sArr1;

sab[0].cElements = 0;
sab[0].lLbound = 0;
sArr1 = SafeArrayCreate(VT_BSTR, 1, (SAFEARRAYBOUND*)&sab);
iface.Test(&sArr1);
mla154 7-Dec-10 9:38am    
It's hard to say whether the message box comes from within the DLL. I placed this message box code as the first line of the C# function, and the message box was never displayed:

MessageBox.Show("here");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900