Link to home
Start Free TrialLog in
Avatar of miggie_ra
miggie_raFlag for United Kingdom of Great Britain and Northern Ireland

asked on

convert a Variant (String Array) from VB 6 DLL to C#.NET String() array

Hi,

I have a VB6 DLL that passes a variant string array paramters to an event that is being call from my C#.net application. when i delegate the event in C# the parameters becomes object.

I need to convert this object into array of string in C#.NET
the code below doesnt work. It seems that i need to convert object array into string array
Can somegody please help me. ASAP please


private void  AuthorizeComplete(object varOutput, bool bSuccess)
{
string x = varOutput(0,1);

}
Avatar of Headspace
Headspace

well, i'm a little confused, but i'll give it a shot : ).

if i were to assign a new string array to an existing string array, i'd do this:

string[] x = (string[])varOutput;
Avatar of miggie_ra

ASKER

Hi,

Its giving an error of specified cast is not valid when i try that. the parameter is a variant string array coming from vb dll. but when i receive it to c#.net it converts it to and object system array type. and i cant iterate on that to retrieve values.

ok, well, what if you assign varOutput to an object array, and then convert each object in the array to a string?

so something like:

string[] str;
object[] obj = varOutput;
for (int i = 0; (i <= obj.Length - 1); i++)
{
      str[i] = (string)obj[i];
}
Hello,

its returning an error of 'Cannot implicitly convert type objec to object[] when i try this.

cheers.
Avatar of Bob Learned
It might help if you marshal the array:

Default Marshaling for Arrays
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondefaultmarshalingforarrays.asp

<Quote>
...a safe array of variants becomes a managed array of objects. The SAFEARRAY element type is captured from the type library and saved in the SAFEARRAY value of the UnmanagedType enumeration...
</Quote>

Bob
@bob:  ...but doesn't that happen automatically?  And if so, wouldn't:

object[] obj = (object[])varOutput;
string[] str = new string[obj.Length]; //sry, forgot to declare this string as new and assign a length to it in the previous ex.
for (int i = 0; (i <= obj.Length - 1); i++)
{
      str[i] = (string)obj[i];
}

...work, theoretically speaking?
hi,

thanks for the infos, bob and headspace.
I agree it will work theoretically but I tried doing that already but and still giving me
'Cannot implicitly convert type objec to object[].

cheers
If you are talking about this line:

   private void  AuthorizeComplete(object varOutput, bool bSuccess)

it would need to be:

   private void  AuthorizeComplete(object[] varOutput, bool bSuccess)

Marshalling variants is not automatic--marshalling string arrays is.

Bob

oooooooooh!  thx Bob : )

wil
i tried that already bob...
private void  AuthorizeComplete(object[] varOutput, bool bSuccess)

is an event handler coming from vb.dll and i dont think i can change object varOutput into object[] varOutput...when i do that i get an error...
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial