473,394 Members | 1,916 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,394 software developers and data experts.

How to Marshal a double structure?

Hi

I am trying to make to work this unmanaged Function from C to VB.Net
My GetCarList Function returns always 0 (ERROR_NONE=0) but the structures
are all empty.

Can someone help me to resolve this problem, even with an example?

Regards

struct Cars
{
char Name[256]; //car Name. max 255 characters
DWORD Flags;
};

struct CarList
{
DWORD Count; //number of cars
DWORD Current; //current car out
Cars car[200];
};

extern "C" DWORD __stdcall GetCarList(OUTcarlist* carList);

ERROR_NONE=0

Public Structure Cars
Dim Name as string
Dim Flags as integer
End Structure

Public Structure CarList
Dim Count as integer
Dim Current as integer
Dim Cars as car
End Structure

<DllImport("cars.dll", EntryPoint:="GetCarList", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function GetCarList(ByVal MyCarList As CarList) As Int32

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #1
9 3307
Hi,

Use ByRef instead of ByVal where you are getting back a value.

<DllImport("cars.dll", EntryPoint:="GetCarList", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function GetCarList(ByRef MyCarList As CarList) As Int32
Ken
--------------------------
"Brusex via DotNetMonster.com" wrote:
Hi

I am trying to make to work this unmanaged Function from C to VB.Net
My GetCarList Function returns always 0 (ERROR_NONE=0) but the structures
are all empty.

Can someone help me to resolve this problem, even with an example?

Regards

struct Cars
{
char Name[256]; //car Name. max 255 characters
DWORD Flags;
};

struct CarList
{
DWORD Count; //number of cars
DWORD Current; //current car out
Cars car[200];
};

extern "C" DWORD __stdcall GetCarList(OUTcarlist* carList);

ERROR_NONE=0

Public Structure Cars
Dim Name as string
Dim Flags as integer
End Structure

Public Structure CarList
Dim Count as integer
Dim Current as integer
Dim Cars as car
End Structure

<DllImport("cars.dll", EntryPoint:="GetCarList", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function GetCarList(ByVal MyCarList As CarList) As Int32

--
Message posted via http://www.dotnetmonster.com

Nov 21 '05 #2
Hi,

Thanks a lot for your reply but the structures are still empty.

Any ideas?
Regards

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #3
I've changed the structures as follow

Public Structure Cars
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)> _
Dim Name() as char
Dim Flags as integer
End Structure

Public Structure CarList
Dim Count as integer
Dim Current as integer
<MarshalAs(UnmanagedType.AsAny, SizeConst:=200)> _
Dim Cars() as car
End Structure

now I have a "System.TypeLoadException" with the CarList Structure

Please help me :D

Regards

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #4
Maybe byref as proposed earlier in this thread:

<DllImport("cars.dll", EntryPoint:="GetCarList", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function GetCarList(ByRef MyCarList As CarList) As Int32

plus

<StructLayout(LayoutKind.Sequential)> _
Public Structure Cars
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> _
Dim Name As String
Dim Flags As Integer
End Structure

<StructLayout(LayoutKind.Sequential)> _
Public Structure CarList
Dim Count As Integer
Dim Current As Integer
Dim Cars() As Cars
Public Sub Initialize()
ReDim Cars(199)
End Sub
End Structure

will make the trick?

CarList structure have to be initialized like in this example:

Dim myCarsList As CarList
myCarsList.Initialize()

This is just a test that all that works:
Dim myCarsList As CarList
myCarsList.Initialize()

With myCarsList.Cars(199)
.Name = "Toyota"
.Flags = 503
End With

HTH,
Shamil

P.S. I must say I can't find how to properly define CarList structure/class
to have it OK for Marshal.StructureToPtr(...) but when API calls are made
and structures are used then my proposal I hope may work...
--
Web: http://smsconsulting.spb.ru/shamil_s

"Brusex via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in message
news:e0******************************@DotNetMonste r.com...
I've changed the structures as follow

Public Structure Cars
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)> _
Dim Name() as char
Dim Flags as integer
End Structure

Public Structure CarList
Dim Count as integer
Dim Current as integer
<MarshalAs(UnmanagedType.AsAny, SizeConst:=200)> _
Dim Cars() as car
End Structure

now I have a "System.TypeLoadException" with the CarList Structure

Please help me :D

Regards

--
Message posted via http://www.dotnetmonster.com

Nov 21 '05 #5
If I use ByRef I get a "System.ExecutionEngineException"

But also with the initialization the returned values are empty

What can I try ?

Thanks a lot for your time

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #6
In case of doubt, like this one, remember you can use StructureToPointer and
PointerToStructure and do the marshalling yourself.

It is more work, but it will work!

best regards,
Alejandro Lapeyre

"Brusex via DotNetMonster.com" <fo***@DotNetMonster.com> escribió en el
mensaje news:e2******************************@DotNetMonste r.com...
If I use ByRef I get a "System.ExecutionEngineException"

But also with the initialization the returned values are empty

What can I try ?

Thanks a lot for your time

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #7
>In case of doubt, like this one, remember you can use >StructureToPointer
and PointerToStructure and do the marshalling >yourself.

I'd like to try but I don't know how. :P

I'm not so well with marshal and structures

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #8
I posted solution here:

http://smsconsulting.spb.ru/.net/tut...b.net/cars.htm

based on information from this source
http://www.dotnetinterop.com/faq/?q=...ithStructArray, which states that
there is no simple way to marshal structure with array of structure(s)
inside of it.....

I used some C# to manipulate IntPtr, if somebody knows how to avoid it
please post here this information...

If somebody knows better and shorter solution please don't hesitate to
comment my one out to dust....

HTH,
Shamil

"Brusex via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in message
news:f1******************************@DotNetMonste r.com...
Hi

I am trying to make to work this unmanaged Function from C to VB.Net
My GetCarList Function returns always 0 (ERROR_NONE=0) but the structures
are all empty.

Can someone help me to resolve this problem, even with an example?

Regards

struct Cars
{
char Name[256]; //car Name. max 255 characters
DWORD Flags;
};

struct CarList
{
DWORD Count; //number of cars
DWORD Current; //current car out
Cars car[200];
};

extern "C" DWORD __stdcall GetCarList(OUTcarlist* carList);

ERROR_NONE=0

Public Structure Cars
Dim Name as string
Dim Flags as integer
End Structure

Public Structure CarList
Dim Count as integer
Dim Current as integer
Dim Cars as car
End Structure

<DllImport("cars.dll", EntryPoint:="GetCarList", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function GetCarList(ByVal MyCarList As CarList) As Int32

--
Message posted via http://www.dotnetmonster.com



Nov 21 '05 #9
@Shamil

Your solution works great! :)
Best Regards

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #10

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

Similar topics

1
by: Camilo Telles | last post by:
Sirs I have the foolowing structures in C #define POLY_MAX 8 // Número máximo de lados em um polígon typedef struct { // Um vértice do polígon double u, v; // texture space coord...
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
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...
0
by: bumbala | last post by:
Below is my unmanaged structure: public struct LasHeader { public string FileSignature;
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.