By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,836 Members | 2,059 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,836 IT Pros & Developers. It's quick & easy.

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

P: 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:

Expand|Select|Wrap|Line Numbers
  1. struct MyStruct
  2. {
  3.     int     siOrder;
  4.     char     aaszNames[6][25];
  5.     int     siId[6];
  6.     int     siTones[6];        
  7. };
I've defined the managed structure as following:

Expand|Select|Wrap|Line Numbers
  1. <StructLayoutAttribute(LayoutKind.Sequential, CharSet:=CharSet.[Ansi])> _
  2. Public Structure MyStruct
  3.     Public siOrder As Integer
  4.  
  5.     <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=150)> _
  6.     Public aaszNames As String
  7.  
  8.     <MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst:=6, ArraySubType:=UnmanagedType.I4)> _
  9.     Public siId() As Integer
  10.  
  11.     <MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst:=6, ArraySubType:=UnmanagedType.I4)> _
  12.     Public siTones() As Integer
  13. 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
Share this Question
Share on Google+
6 Replies


CroCrew
Expert 100+
P: 563
Is this a C++ or VB question? If it a C++ question you would get a faster reply to your question if it were posted in the C++ section and not the VB section.

Hope you get your answer,
CroCrew~
Feb 12 '10 #2

P: 6
Well, I could also try the C++ section... ;)
Feb 12 '10 #3

CroCrew
Expert 100+
P: 563
Lets see if we can get this moved to the C++ section.

Good Luck in getting your answer,
CroCrew~
Feb 12 '10 #4

P: 6
Thanx CroCrew :)

Hoping for some hints myself..
Feb 12 '10 #5

tlhintoq
Expert 2.5K+
P: 3,525
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Feb 12 '10 #6

Curtis Rutland
Expert 2.5K+
P: 3,256
Thread moved. Hope you find your answers.
Feb 12 '10 #7

Post your reply

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