473,320 Members | 2,029 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,320 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 16 '05 #1
5 9234
Michael,

The string types that you are returning should be marshaled as
UnmanagedType.BStr, not UnmanagedType.LPWStr.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michael Murphy" <Mi***********@discussions.microsoft.com> wrote in message
news:37**********************************@microsof t.com...
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 16 '05 #2


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Op**************@TK2MSFTNGP10.phx.gbl...
Michael,

The string types that you are returning should be marshaled as
UnmanagedType.BStr, not UnmanagedType.LPWStr.

Hope this helps.


Which is the default for COM interop, so there is no need to use these
attributes.

Willy.
Nov 16 '05 #3
.... default for string marshaling using COM interop....

Willy.

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:O6**************@TK2MSFTNGP10.phx.gbl...


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:Op**************@TK2MSFTNGP10.phx.gbl...
Michael,

The string types that you are returning should be marshaled as
UnmanagedType.BStr, not UnmanagedType.LPWStr.

Hope this helps.


Which is the default for COM interop, so there is no need to use these
attributes.

Willy.

Nov 16 '05 #4
Actually specifiying the MarshalAs type as BStr resolved the issue. I am not
sure that Bstr is the default. If the MarshalAs attribute is left out, the
error occurs.

Thanks Nicholas

You are a MVP!

Mike Murphy

"Willy Denoyette [MVP]" wrote:
.... default for string marshaling using COM interop....

Willy.

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:O6**************@TK2MSFTNGP10.phx.gbl...


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:Op**************@TK2MSFTNGP10.phx.gbl...
Michael,

The string types that you are returning should be marshaled as
UnmanagedType.BStr, not UnmanagedType.LPWStr.

Hope this helps.


Which is the default for COM interop, so there is no need to use these
attributes.

Willy.


Nov 16 '05 #5
Check the Framework developers guide in MSDN (search for Default Marshaling
for Strings)
The default for interfaces is:
UnmanagedType.BStr
(default) A COM-style BSTR with a prefixed length and Unicode
characters.

However, for structure members there is no documented default. That means
the COM interop marshaler uses the typelib info if any. Are you sure the
Interop Assembly typelib is registered? You can register the typelib using
regasm.exe (run regasm /? for details).
Willy.

"Michael Murphy" <Mi***********@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.com...
Actually specifiying the MarshalAs type as BStr resolved the issue. I am
not
sure that Bstr is the default. If the MarshalAs attribute is left out,
the
error occurs.

Thanks Nicholas

You are a MVP!

Mike Murphy

"Willy Denoyette [MVP]" wrote:
.... default for string marshaling using COM interop....

Willy.

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:O6**************@TK2MSFTNGP10.phx.gbl...
>
>
> "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
> wrote
> in message news:Op**************@TK2MSFTNGP10.phx.gbl...
>> Michael,
>>
>> The string types that you are returning should be marshaled as
>> UnmanagedType.BStr, not UnmanagedType.LPWStr.
>>
>> Hope this helps.
>>
>>
>
> Which is the default for COM interop, so there is no need to use these
> attributes.
>
> Willy.
>


Nov 16 '05 #6

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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.