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

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.Pinned )
Dim ret As Integer = GC.AddrOfPinnedObject.ToInt64
GC.Free()
Return ret
End Function

...
Dim InputBuffer(2) As Long ' inbuf
Dim CommandBuffer As Command ' cbuf
ReDim CommandBuffer.Commands(2)
CommandBuffer.Commands(0) = 4
CommandBuffer.Commands(1) = 4
CommandBuffer.Command2 = 0
InputBuffer(0) = 2
InputBuffer(1) = VarPtr(CommandBuffer) ' inbuf[1] = (long)&cbuf;

When I run it, I get the following error:

An unhandled exception of type 'System.ArgumentException' 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 3751

"Jonathan Amend" <ce*******@hotmail.com> wrote in message
news:%_********************@news20.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(LayoutKind.Sequential)> _
Public Structure Command
<MarshalAs(UnmanagedType.ByValArray, 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(LayoutKind.Sequential)> _
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
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...
0
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...
26
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...
17
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}",...
4
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...
4
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...
14
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...
24
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...
5
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);"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...
0
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,...

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.