473,651 Members | 2,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(L ayoutKind.Seque ntial, CharSet:=CharSe t.Ansi, Pack:=4)> _
Private Structure struct1

<VBFixedString( 200), MarshalAs(Unman agedType.ByValT Str, SizeConst:=200) >
_
Public name As String

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

<VBFixedArray(1 00), MarshalAs(Unman agedType.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.Argumen tException' occurred in
WindowsApplicat ion2.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.TypeLoa dException' occurred in
WindowsApplicat ion2.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 2331
"Sin" <br****@hotmail .com> schrieb
<VBFixedArray(1 00), MarshalAs(Unman agedType.I2, SizeConst:=100) >
_ Public d() As Int16


Does

<VBFixedArray(1 00), MarshalAs(Unman agedType.BYVALA RRAY, 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(1 00), MarshalAs(Unman agedType.I2, SizeConst:=100) >
_ Public d() As Int16


Does

<VBFixedArray(1 00), MarshalAs(Unman agedType.BYVALA RRAY, 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(1 00), MarshalAs(Unman agedType.BYVALA RRAY, 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(Unma nagedType.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
2660
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 anything helpfull in internet for 4 months ;-( . Please someone who is more expirenced help me. I am trying to use the Terminal Server APIs There is as an API function WTSEnumerateSessions, which returns a WTS_SESSION_INFO custom structure n times....
5
96482
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 what it is. Is converting a C-style struct to a C# class (or struct) marshalling? And what about the function exports? Are those Marshalling too? Thanks for your help and thanks for helping me with the Dll exports (the several messages I posted in...
3
3637
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
1867
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 can't find the correct sequence to declare the marshal attributes of my structures, I always got an exception at runtime when marshaling my data. Here an example of code, which gives me the exception "Type TypeData can not be marshaled as an...
3
3665
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
303
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 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 :
6
4212
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), MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> Public action As Char() <VBFixedString(7), MarshalAs(UnmanagedType.ByValArray, SizeConst:=7)> Public equip_key As Char() <VBFixedString(256), MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)> Public message As Char()
1
2672
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 P/Invoke", which is unfortunately in C++ with managed extensions; I am working in C#. As with Kate's example I want to marshal from a System.DateTime to a proprietary format. However I cannot figure out how to tell the system to box the DateTime. ...
0
1140
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 StructureToBytesArray(object obj) { int len = Marshal.SizeOf(obj); byte arr = new byte; IntPtr ptr = Marshal.AllocHGlobal(len); Marshal.StructureToPtr(obj, ptr, true);
0
8361
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8278
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8807
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8466
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8584
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.