Mashaling a VB6 Array Of Struct to C# Array of Struct

C

Cybertof

Hello,

Is it possible to convert a VB6 Array of Struct
to
a C# Array Of Struct ?

The test context is a C# application calling a VB6 ActiveX DLL Function
using UDT (User Defined Type) and array of UDT.


***************************************
Example : (VB6AX is an ActiveX VB6 DLL)
***************************************

(VB6 Side)

Type VB6Struct
d1 as double
d2 as double
l1 as long
End Type


(C# Side)

struct CSharpStruct
{
double d1;
double d2;
double d3;
long l1;
}
private VB6AX.CTest AXInstance; // ActiveX Class Instance

AXInstance = new VB6AX.CTest(); // Instanciate vb6 class

AXInstance.VB6Struct myData = new AXInstance.VB6Struct();
AXInstance.FillStruct(ref myData);
// (FillStruct is a vb6 class method to fill the structure)

IT WORKS, BUT...IF I DO
CSharpStruct myData = new CSharpStruct();
AXInstance.FillStruct(ref myData);

I have a
'cannot convert from ref CSharpStruct to ref AXInstance.VB6Struct'.

How to convert the ActiveX DLL Struct to the C# managed struct ?
The end of the story is that I would like my VB6 ActiveX DLL to fill an
array of structure, called by the C# client.
The filled array of structure should be a 'managed' array of struct so I
do have fast access speed to its elements later on in the application.

-> How to convert/mashal a VB6 Array Of Struct to a C# Array of Struct
(the C# array should be definied as an array of managed struct and not
through the ActiveX unmanaged structure declaration)

VB6AX.VB6Struct[] array1 = null; // array of unmanaged VB6 Struct
CSharpStruct[] array2 = null; // array of C# managed Struct
// bot struct represents the same thg


-> How to convert a ref VB6AX.VB6Struct to a ref CSharpStruct ?

-> How to convert a ref VB6AX.VB6Struct[] to a ref CSharpStruct[] ?


Can you help ?


Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

Cybertof,

The VB6Struct that is defined in the interop assembly is actually a
managed structure. When you receive the array back from the call to the
method, the structure and the array are both managed, no different from any
other array or structure, as the marshaller has already done the conversion
for you. There is no need to create another definition, as they will work
exactly the same.

Hope this helps.
 
C

Cybertof

Cybertof,

The VB6Struct that is defined in the interop assembly is actually a
managed structure. When you receive the array back from the call to the
method, the structure and the array are both managed, no different from any
other array or structure, as the marshaller has already done the conversion
for you. There is no need to create another definition, as they will work
exactly the same.

Hope this helps.

Thanks Nicholas, but :

If i want my C# application to work only with my Struct defined on the
C# side, how can i pass my
ref ArrayOfC#SideStruct[]
to the vb6 activex function which is waiting for a
ref ArrayOfInternalVB6StructDefiniedInsindeAXClass[]

?

I would like my application to work with array of items and i would like
the type of these items not depending on the type inside the activex
dll.

Is it possible ?


Thanks,
Cybertof.
 
N

Nicholas Paldino [.NET/C# MVP]

Cybertof,

The only way you could do this would be to write a custom marshaller, or
a wrapper for the VB component that would transform the results from the
structure defined in the interop assembly.

The thing is, why? For asthetic reasons? There is absolutely NO
difference between the two structures, with the exception of the name.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Cybertof said:
Cybertof,

The VB6Struct that is defined in the interop assembly is actually a
managed structure. When you receive the array back from the call to the
method, the structure and the array are both managed, no different from
any
other array or structure, as the marshaller has already done the
conversion
for you. There is no need to create another definition, as they will
work
exactly the same.

Hope this helps.

Thanks Nicholas, but :

If i want my C# application to work only with my Struct defined on the
C# side, how can i pass my
ref ArrayOfC#SideStruct[]
to the vb6 activex function which is waiting for a
ref ArrayOfInternalVB6StructDefiniedInsindeAXClass[]

?

I would like my application to work with array of items and i would like
the type of these items not depending on the type inside the activex
dll.

Is it possible ?


Thanks,
Cybertof.
 
C

Cybertof

Cybertof,

The only way you could do this would be to write a custom marshaller, or
a wrapper for the VB component that would transform the results from the
structure defined in the interop assembly.

The thing is, why? For asthetic reasons? There is absolutely NO
difference between the two structures, with the exception of the name.

Yes for 'asthetic' coding reasons, not to see the vb6ax interop name,
when accessing an inner data through the structure.

Would making a custom marshaller/wrapper would cost additional
processing time somewhere ?

The only thing i'm looking for is, how to pass a
ref CSharpStruct[] array of struct
to a function inside my ActiveXDll which is waiting for a
ref VB6AX.VB6Struct[] array of 'inner vb6ax struct


I actually have a
'Cannot convert ref CSharpStruct[] to ref VB6AX.VB6Struct[]'


Regards,
Cybertof.
 
N

Nicholas Paldino [.NET/C# MVP]

Cybertof,

Creating a marshaller is overkill in this situation. I would just
create another class which wraps access to the COM object in VB6 and
converts the arguments as they come in and out.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Cybertof said:
Cybertof,

The only way you could do this would be to write a custom marshaller,
or
a wrapper for the VB component that would transform the results from the
structure defined in the interop assembly.

The thing is, why? For asthetic reasons? There is absolutely NO
difference between the two structures, with the exception of the name.

Yes for 'asthetic' coding reasons, not to see the vb6ax interop name,
when accessing an inner data through the structure.

Would making a custom marshaller/wrapper would cost additional
processing time somewhere ?

The only thing i'm looking for is, how to pass a
ref CSharpStruct[] array of struct
to a function inside my ActiveXDll which is waiting for a
ref VB6AX.VB6Struct[] array of 'inner vb6ax struct


I actually have a
'Cannot convert ref CSharpStruct[] to ref VB6AX.VB6Struct[]'


Regards,
Cybertof.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top