473,386 Members | 2,042 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,386 software developers and data experts.

Marshaling structure members

Sin
Hello everybody,

I'm currently trying to understand how marshaling can use used for accessing
Win32 API functions as well as custom C/C++ code we design which exposes
functions the same way as the Win32 API.

I figured how to use strings... No problem there, there was a couple undred
examples on the net. I have yet to find how to do the same with an array of
integers for example... Here's my test code :

C strucure I'm trying to match :

typedef struct {
char name[200];
long id;
double a;
bool b;
char c;
short d[100];
} struct1;
Here's the VB structure in it's current (non working) state :

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=4)> _
Private Structure struct1

<VBFixedString(200), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=200)>
_
Public name As String

Public id As Int32
Public a As Double
Public b As Boolean
Public c As Char

<VBFixedArray(100), MarshalAs(UnmanagedType.I2, SizeConst:=100)> _
Public d() As Int16

End Structure
Two different problems occur using this definition. First, the
Marshall.SizeOf() call on an instance of the structure gives me this msg:

An unhandled exception of type 'System.ArgumentException' occurred in
WindowsApplication2.exe

Additional information: Le type struct1 ne peut pas être marshalé comme une
structure non managée ; il n'est pas possible de calculer une taille ou un
offset.

Rough traduction from french :

The struct1 type cannot be marshalled as an unmanaged type ; it is
impossible to calculate a size or an offet.

I don't quite understand why I get this message, or what it means for that
matter.
The second problem is that if I modify the member "d" from my C DLL, I get
the following error :

An unhandled exception of type 'System.TypeLoadException' occurred in
WindowsApplication2.exe

Additional information: Impossible de marshaler le champ d du type struct1 :
ce type ne peut pas être marshalé comme un champ structuré.

Rough traduction from french :

Unable to marshall the d "field" of struct1 : this type cannot be marshaled
as a structured "field".

I've been looking over MSDN and the internet all morning and I still can't
figure how to make this work...

Any help would be appreciated!

Alex.
Nov 20 '05 #1
3 2310
"Sin" <br****@hotmail.com> schrieb
<VBFixedArray(100), MarshalAs(UnmanagedType.I2, SizeConst:=100)>
_ Public d() As Int16


Does

<VBFixedArray(100), MarshalAs(UnmanagedType.BYVALARRAY, SizeConst:=100)> _
Public d() As Int16
work?

Marshal.sizeof(gettype(struct1)) returns 420 then. Seems to be correct.
I don't know, but isn't the C type "long" (field 'id') also "long" in
VB.NET?

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
"Sin" <br****@hotmail.com> schrieb
<VBFixedArray(100), MarshalAs(UnmanagedType.I2, SizeConst:=100)>
_ Public d() As Int16


Does

<VBFixedArray(100), MarshalAs(UnmanagedType.BYVALARRAY, SizeConst:=100)> _
Public d() As Int16
work?

Marshal.sizeof(gettype(struct1)) returns 420 then. Seems to be correct.
I don't know, but isn't the C type "long" (field 'id') also "long" in
VB.NET?

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
Sin
> Does

<VBFixedArray(100), MarshalAs(UnmanagedType.BYVALARRAY, SizeConst:=100)> _
Public d() As Int16

work?

Marshal.sizeof(gettype(struct1)) returns 420 then. Seems to be correct.
Yes!

I did have to change my boolean declaration, either of these work :

C++ bool (C++ base type) = <MarshalAs(UnmanagedType.U1)> Public b As Boolean
C++ BOOL (ie : macro for int) = Public b As Boolean

I don't know, but isn't the C type "long" (field 'id') also "long" in
VB.NET?


Not sure where I picked that from but I'm fairly sure VB.NET's Long (as
opposed to any other language I know of) is 64 bits, rather than the 32 bits
an experienced programmer has come to expect. I guess they're laying the
grounds for the 64 bit version of Windows... It's odd nonetheless..

In other words, an unmanaged C/C++ long (a standard one that is) is either
an integer or an Int32 (which I find clearer). You never know when they're
going to change Integer to a 64 bit either, so better be safe than sorry!

Thanks alot for clearing it up!

Alex.
Nov 20 '05 #4

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

Similar topics

3
by: Nikolay Petrov | last post by:
Guys, please help. I am trying to make this work from at least 4 months. I am new to programming and some things are difficult to me, but I really need to make my project work. I can't find...
5
by: VM | last post by:
What's marshalling? I've had to use it extensively for a project but I don't know what it means. I tried to look for a definition in the Internet but I couldn't find anything that would explain...
3
by: Rudy Velthuis | last post by:
Hello, Does anyone know how to create a struct that will marshal to the following C++ struct A, containing an array of the user defined String10 type: struct String10 { char SLen; char S;
2
by: Pas de Spam | last post by:
Hi, I have some problem in marshaling array of structure. I have to exchange a structure of data with a C++ program. This structure data contains a field which is an array of a custom type. I...
3
by: Jeffrey B. Holtz | last post by:
THANK YOU 1,000 fold if anyone can help me in this Marshalling: C structure =========== struct mystruct { char tag; char field1; char field2; char field3; long field4;
0
by: Sin | last post by:
Hello everybody, I'm currently trying to understand how marshaling can use used for accessing Win32 API functions as well as custom C/C++ code we design which exposes functions the same way as...
6
by: Tom | last post by:
I'm trying to pass this structure to a dll: <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _ Public Structure udtINTER01 <VBFixedString(8),...
1
by: Bill Medland | last post by:
I am trying to do some P/Invoke custom marshaling and am looking for a little help on custom marshaling a value type. I am working based on Kate Gregory's "Arranging Custom Marshaling With...
0
by: swts | last post by:
hi, the following marshaling code gives me an errror of "type packet cannot be marshaled as an unmanaged structure; no meaningful size or offset can be managed". public static byte...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.