472,330 Members | 1,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,330 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 3236
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...
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...
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...
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...
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...
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...
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.