473,396 Members | 1,940 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,396 software developers and data experts.

Marshaling a C++ two-dimensional fixed length char array as a structure member

6
Hallo everyone.

I am trying to call an unmanaged C++ function, that has a structure as an input parameter.
The structure is defined in the header file like this:

struct MyStruct
{
int siOrder;
char aaszNames[6][25];
int siId[6];
int siTones[6];
};

I've defined the managed structure as following:

<StructLayoutAttribute(LayoutKind.Sequential, CharSet:=CharSet.[Ansi])> _
Public Structure MyStruct
Public siOrder As Integer

<MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=150)> _
Public aaszNames As String

<MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst:=6, ArraySubType:=UnmanagedType.I4)> _
Public siId() As Integer

<MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst:=6, ArraySubType:=UnmanagedType.I4)> _
Public siTones() As Integer
End Structure

The C++ dll writes to a log file when a function is called. From this log I can see that the structure cannot be resolved.

My issues are the following:

a) Marshal.SizeOf(MyStruct) is computed as 204, but as I can see, the size seems to be: 4 + 150 + 6*4 + 6*4 = 202 bytes. What are these two extra bytes, and where are they supposed to be padded?

b) How should I populate the aaszNames field? The C++ expects 6 rows of 25 character long strings, but my individual values are of less length (eg "John", "Robert", etc). How should I concatenate the string?

Could anyone give me a hint about these issues?
Feb 12 '10 #1
3 1746
Banfa
9,065 Expert Mod 8TB
a) I suspect you are getting 2 bytes of padding after the array aaszNames so that siId appears on a 4 byte boundary. Note that 150 + 4 = 154 is not a 4 byte boundary. A 32 bit machine is going to prefer having its 32bit values on 4 byte (32 bit) boundaries.

b) Since aaszNames looks like it is supposed to be an array of C style zero terminated strings you should copy your strings into the array and write a zero terminate (character value 0) at the end of every string. If you get a string > 24 characters you will need to truncate it to 24 characters to leave room for the zero terminator.
Feb 12 '10 #2
TTheot
6
Thnx for answering Banfa.
a)Yes, these two bytes make sense to me now! So, I suppose I should not alter anything in my declaration, and leave .NET do the trick, correct?

b) Do you suggest I should declare aaszNames as one dimensional char array with 150 length, including a null terminator after each string? Although I have tried that, with no success, I'll give it a try again. Maybe I did something wrong ;)
Feb 12 '10 #3
TTheot
6
Well, I tried once more declaring aaszNames as one dimensional char array with 150 length, but again, no luck...
I'm stuck...
Feb 13 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: S Mulgund | last post by:
I have some legacy data that I want to convert to XML. I've designed a schema to represent the data in XML form, and I'm trying to figure out how best to perform the raw->XML conversion. The Java...
0
by: Ash | last post by:
Hi, Has anyone ever tried using "COM marshaling" for cross- process communication? In particular, I am interested in cross-process marshaling between Managed Server and Unmanaged Client. ...
4
by: Vadym Stetsyak | last post by:
Hi there!!! I'm looking for any resources on the subject. Any help will be appreciated! -- Vadym Stetsyak ICQ 161730125 He, who commands the past - commands the future
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...
1
by: Nadav | last post by:
Hi I am about to write a performance crutial system, I am considering writing this system based on native COM or unmanaged C++ exposed as CLI, Now, I Wonder... does exposing a native code through...
4
by: MSDousti | last post by:
Hi all, I read some Q&As in the Net, discussing Marshalization of nested structs in C# (or VB.NET). Some guys stated that .NET framework does not support this feature yet. Are they right? ...
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: chowsaikeet | last post by:
Dear All, I have trouble doing marshaling for polymorphism. Please look at the code below: //C++ code, sample.dll Class A {
0
by: jpogorman | last post by:
Hello, I am trying to get c# custom marshaling working in a particular scenario but it does not appear to be working or not jumping into my marshaling class when I try to debug it. I am try to...
5
by: michelqa | last post by:
Hi, I need to call a lot of different native SendMessage to retreive informations from non managed application. Some win32 messages use struct pointer for lparam....how to create and...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...
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.