473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Marshal C# structure to VB6

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
Nov 22 '05 #1
1 2534
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.

"Michael Murphy" wrote:
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# Code
---------------------------------------------------------------------------------------------
[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 Code
-----------------------------------------------------------------------------------------
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

Nov 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. ...
3
by: kevin | last post by:
Hi I have a C struct that is of the following typedef struct{ DWORD num_conversions; ... short *sample_values; ....
2
by: twawsico | last post by:
I have a piece of code that needs to read the contents of a binary file (that I've created with another app) into an array of structures. The binary data in the file represents just a series of...
2
by: RYoung | last post by:
Given this native struct: typedef struct vendor { char name; } VENDOR I want to make managed equivalent, so I did this: public value struct Vendor
0
by: Paul | last post by:
I am trying to get the size of a structure that has an array of characters using the Marshal::SizeOf( ) method. The Marshal::SizeOf method fails because it says no meaningful size can be computer....
1
by: spamacon | last post by:
Hello, I have a strange situation using .Net FW 1.1. I want to use Marshal.PtrToStructure to fill the structure below. The first 3 fields get filled correctly: ulStruct describes how big the...
2
by: O.B. | last post by:
I have a structure named EntityState with an explicit layout. The following two operations exist within the class to return a byte array representing the current object. Upon executing them each a...
4
by: cleanrabbit | last post by:
Hello! I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
2
by: O.B. | last post by:
I have operation within a class that marshals the data into a byte array. Below are three different ways that work. Are there any downsides to using one over the the other? public virtual byte...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.