How to Marshal a double structure?

  • Thread starter Brusex via DotNetMonster.com
  • Start date
B

Brusex via DotNetMonster.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
 
G

Guest

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
 
B

Brusex via DotNetMonster.com

Hi,

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

Any ideas?


Regards
 
B

Brusex via DotNetMonster.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
 
S

Shamil Salakhetdinov

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...
 
B

Brusex via DotNetMonster.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
 
A

alejandro lapeyre

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" <[email protected]> escribió en el
mensaje 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
 
B

Brusex via DotNetMonster.com

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
 
S

Shamil Salakhetdinov

I posted solution here:

http://smsconsulting.spb.ru/.net/tutorials/vb.net/cars.htm

based on information from this source
http://www.dotnetinterop.com/faq/?q=StructWithStructArray, 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top