469,332 Members | 6,612 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

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 3131
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by spamacon | last post: by
reply views Thread by bumbala | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.