I have a C++ method with this signature:
STDMETHODIMP ClassName::FunctionName(long number, BSTR* names, long* status)
Inside the method the names variable is accessed as an array of strings, i.e.
char * tempString = NULL;
for (int n = 0; n < number; n++)
{
tempString = OLE2T(names[n]);
...
I compile the C++ project, which generates a dll, I then register this dll and add a reference to it in the VB project. When I add the reference, an Interop assembly is automatically generated, and the signature of the method in the Interop assembly is as follows:
FunctionName (number as Integer, ByRef names as String) As Integer
From VB.Net I invoke the method like this:
result = FunctionName (number, names(0))
Where names is a string array with multiple elements, and number and result are Integers.
The problem is that when the C++ code tries to access the rest of the elements in the names array (names[1] and ahead) it starts getting "garbage" on those fields.
My question is, how do I send the whole string array instead of just the first value.
The C++ code is a library that I cannot modify, therefore any change that I do must be on the VB.Net code.
I was thinking that maybe using PInvoke to call the method might do the trick (declaring a correct signature), but I was hoping for something better.
Any ideas?
Thanks!
Edit:
I'm no expert in Interop/Marshaling but I checked the IDL definition of the method and it is as follows:
[id(60), helpstring("method FunctionName")]
HRESULT FunctionName(
[in] long number,
[in, size_is(number)] BSTR* names,
[out, retval] long* status);
Shouldn't the size_is indicate that the names parameter is an array, and thus, when the Interop assembly is generated act accordingly?
Thanks again
numbermeans the size of the string arraynames. Otherwise the marshaller does not know this, and just expects independent values, not knowing thatnamesis an array at all. Of course you can write a C++ wrapper, which "knows" the invalid behavior of the original C++ library, and corrects the interface.MarshalAsAttribute, perhaps you meant VB.net? Andnames[0]isn't array element syntax in VB either? The cleanest way to solve your problem would be with a C++/CLI wrapper that accepts a .NETarray<String^>, converts to an array ofBSTR, and then calls the C++ function.