473,847 Members | 2,463 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 3777

"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
3415
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
7117
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
3048
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
13416
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
3900
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
1810
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
3472
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
3806
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
9879
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
10981
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
10643
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...
0
9477
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7879
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
7050
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5907
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4521
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
3
3158
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.