Marshal C# structure to VB6

G

Guest

I am writing a C# class library that exposes a method that return a
structure. I am registering the assembly for COM interop and can reference
the assembly in VB (Actually an Acces 2000 Code Module). I can instantiate
the object in VB but when I declare the variable for the returning structure
I get the following error.

Variable uses and automation type not supported Visual Basic.

Also, if I declare the variable as an Object, the error occurs when I call
the method.

Here is my code below...

C# Cod
---------------------------------------------------------------------------------------------
[GuidAttribute("B7D043BD-1003-4c6e-9050-0D0C4D93E213")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class VINDecoder
{
[StructLayout(LayoutKind.Sequential)]
[GuidAttribute("C39A4B1A-9186-40d8-BB26-CEE38D92A084")]
public struct VehicleInformation
{
[MarshalAs(UnmanagedType.LPWStr)]
public string Year;
[MarshalAs(UnmanagedType.LPWStr)]
public string Make;
[MarshalAs(UnmanagedType.LPWStr)]
public string Model;
[MarshalAs(UnmanagedType.LPWStr)]
public string MSRP;
}

public VINDecoder()
{
//
// TODO: Add constructor logic here
//
}

public VehicleInformation Decode(string VIN)
{
VehicleInformation vi = new VehicleInformation();
vi.Make = "Honda";
vi.Model = "Accord";
vi.Year = "2005";
vi.MSRP = "23000";
return vi;
}

}


VB Cod
-----------------------------------------------------------------------------------------
Public Function Test()

Dim lVINDecoder As VINDecoder.VINDecoder
Set lVINDecoder = New VINDecoder.VINDecoder

Dim vi As VehicleInformation '<---
Error occurs here
Set vi = lVINDecoder.Decode("Test")

Debug.Print vi.Make
End Function
 
G

Guest

It's pretty simple. COM does not support structures...

Turn the struct into a class and you will be fine.

Actually, when I say it does not support structures, that is not entirely
true, I have managed to return structures to Vb6 from c#, however they are
strictly non-updateable.. (Unless you use a few nasty late binding tricks..).
Either way, I do suggest not using them.
 

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