473,792 Members | 2,877 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Structure with array unblittable? (C++ > VB.NET porting)

I'm trying to port some C++ code to VB.NET but I have hit a snag. I need to
add the pointer of a structure which includes an array to an array and then
pass it to an API. Here are the two code snippets:

long inbuf[2]; // Array that gets passed to the API
struct cmbuf {
short cmds[2];
long cm2;
} cbuf; // Structure whos pointer must be added to inbuf
cbuf.cmds[0] = 0;
cbuf.cmds[1] = 4;
cbuf.cm2 = 2;
inbuf[0] = 2;
inbuf[1] = (long)&cbuf; // Add the pointer of cbuf to inbuf

--------

' The equivalent of cbuf
Private Structure Command
Dim Commands() As Short
Dim Command2 As Long
End Structure

' Function to get pointer of blittable object
Public Function VarPtr(ByVal o As Object) As Long
Dim GC As System.Runtime. InteropServices .GCHandle =
System.Runtime. InteropServices .GCHandle.Alloc (o,
System.Runtime. InteropServices .GCHandleType.P inned)
Dim ret As Integer = GC.AddrOfPinned Object.ToInt64
GC.Free()
Return ret
End Function

...
Dim InputBuffer(2) As Long ' inbuf
Dim CommandBuffer As Command ' cbuf
ReDim CommandBuffer.C ommands(2)
CommandBuffer.C ommands(0) = 4
CommandBuffer.C ommands(1) = 4
CommandBuffer.C ommand2 = 0
InputBuffer(0) = 2
InputBuffer(1) = VarPtr(CommandB uffer) ' inbuf[1] = (long)&cbuf;

When I run it, I get the following error:

An unhandled exception of type 'System.Argumen tException' occurred in
mscorlib.dll

Additional information: Object contains non-primitive or non-blittable data.

I did some googling and found out that both structures and arrays are
blittable if they contain only primitives. Shouldn't my structure still be
blittable if it contains a blittable array? I tried making the structure
without the array and I didn't get the error but of course now the data no
longer fits the API.
Nov 21 '05 #1
1 3772

"Jonathan Amend" <ce*******@hotm ail.com> wrote in message
news:%_******** ************@ne ws20.bellglobal .com...
I'm trying to port some C++ code to VB.NET but I have hit a snag. I need
to add the pointer of a structure which includes an array to an array and
then pass it to an API. Here are the two code snippets:

long inbuf[2]; // Array that gets passed to the API
struct cmbuf {
short cmds[2];
long cm2;
} cbuf; // Structure whos pointer must be added to inbuf
cbuf.cmds[0] = 0;
cbuf.cmds[1] = 4;
cbuf.cm2 = 2;
inbuf[0] = 2;
inbuf[1] = (long)&cbuf; // Add the pointer of cbuf to inbuf

--------

' The equivalent of cbuf
Private Structure Command
Dim Commands() As Short
Dim Command2 As Long
End Structure


By default array are marshaled using pointers.

You need to tell the framework how to marshal the structure using
attributes.
<StructLayout(L ayoutKind.Seque ntial)> _
Public Structure Command
<MarshalAs(Unma nagedType.ByVal Array, SizeConst:=2)> _
Dim Commands() As Short
Dim Command2 As Long
End Structure

But this doesn't make the structure blittable to the unmanaged type. It
just tells the framework how to copy the managed type to unmanaged memory or
back.

Or, an easy workaround would be to define your struct as:

<StructLayout(L ayoutKind.Seque ntial)> _
Private Structure Command
Dim Commands_0 As Short
Dim Commands_1 As Short
Dim Command2 As Long
End Structure
The real problem with the using an array as a struct member is that the
managed type is no longer 100% bitwise compatable with the unmanaged type.
Arrays are reference types and are always stored on the managed heap. An so
structure with an array member is only partially stored on stack. So every
time the type is marshalled it requires additional copying.

David
Nov 21 '05 #2

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

Similar topics

28
3409
by: prunoki | last post by:
Hello, I am an SQL server newbie. Our company has a massive application written in PL/SQL. I need to port parts of it to SQL Server. - Which SQL server version should I choose, to have a reasonable chance of porting? - Could you recommend any best practices, tools? I use Toad for SQL development on Oracle, I am looking for something similar for SQL
0
1257
by: Vidya Bhagwath | last post by:
Hello Experts, I am porting the C++ code to the C#. In my C++ code, I have used structure of function pointers. Then I have created the array of that structure and intialized that array. Here is the code typedef struct { unsigned char (CMyclass::*Construct)(LINKINFO&); unsigned char (CMyclass::*parse)(LINKINFO&,LINKINFO&,unsigned
26
7098
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of the structure inside the array. When I try this, an error about late assignment appears. Is it possible to assign a value to a structure field that is in an array? I'm currently getting around the problem by creating a new structure, assign...
17
3044
by: Peter Bromley | last post by:
The following code snippet does not seem to work correcly unsigned __int64 result = 0xFFFFFFFFFFFFFFFF; result = result << 64; Debugger.WriteLine(System::String::Format(S"{0:X16}", __box(result)); result = 0xFFFFFFFFFFFFFFFF; result = result >> 64; Debugger.WriteLine(System::String::Format(S"{0:X16}", __box(result)); __int64 result = -1;
4
13411
by: David Bargna | last post by:
Hi I have a problem, I have a string which needs to be converted to a byte array, then have the string representation of this array stored in an AD attribute. This string attribute then has to be read and the string representation of the byte array has to be converted back to the original byte array and converted back to the original string - confused yet? in psuedo
4
3897
by: marco_segurini | last post by:
Hi, From my VB program I call a C++ function that gets a structure pointer like parameter. The structure has a field that contains the structure length and other fields. My problem is that each 'double' fields get 12 bytes instead of 8 so the structure length results wrong. '----Sample
14
1807
by: Dennis | last post by:
If I have a structure like; Public Structure myStructureDef Public b() as Byte Public t as String End Structure If I pass this structure, will the values in the array b be stored on the stack or will just a pointer to the array be stored on the stack? I am trying to decide whether to use Structures or Pointers. I know that M'soft
24
3462
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
5
3800
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
0
9670
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
10430
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...
0
10211
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10159
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
10000
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...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4111
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
3719
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2917
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.