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

P/Invoke with struct array, failed

Hi group,

I'v got something wrong with P/Invoke. If I have two structs defined like
this:
typedef struct{
DWORD F1;
}A, *PA;

typedef struct{
A Data[8];
}B, *PB;

Then I define two structs in C# to interop:

[StructLayout(LayoutKind.Sequential)]
struct A{
public uint F1;
}

[StructLayout(LayoutKind.Sequential)]
struct B{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
public A[] Data;
}

It seems very easy but it does not works. The following is my test code.

in VC:

extern "C" DLLTEST_API void StructTest(B b)
{
A* a = b.Data;
for(int i = 0;i < 8;i++)
{
cout << a[i].F1 << endl;
}
}

in C#:

[DllImport(TestDllPath)]
static extern void StructTest(B b);

[STAThread]
static void Main()
{
StructTest(new B());
}

The exception is:
Can not marshal field Data of type B: This type can not be marshaled as a
structure field.

I don't know why. Any one can help me? Thank you very much.

Regards,
Sunmast
Nov 17 '05 #1
1 2060

"Xia Wei" <ip*****@gmail.com> wrote in message
news:d4***********@mail.cn99.com...
Hi group,

I'v got something wrong with P/Invoke. If I have two structs defined like
this:
typedef struct{
DWORD F1;
}A, *PA;

typedef struct{
A Data[8];
}B, *PB;

Then I define two structs in C# to interop:

[StructLayout(LayoutKind.Sequential)]
struct A{
public uint F1;
}

[StructLayout(LayoutKind.Sequential)]
struct B{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
public A[] Data;
}

It seems very easy but it does not works. The following is my test code.

in VC:

extern "C" DLLTEST_API void StructTest(B b)
{
A* a = b.Data;
for(int i = 0;i < 8;i++)
{
cout << a[i].F1 << endl;
}
}

in C#:

[DllImport(TestDllPath)]
static extern void StructTest(B b);

[STAThread]
static void Main()
{
StructTest(new B());
}

The exception is:
Can not marshal field Data of type B: This type can not be marshaled as a
structure field.

I don't know why. Any one can help me? Thank you very much.


hmm... because the v1.x marshaler can't handle structures that contain
array's of struct's.
One way to solve this is to flatten your structure like:

[StructLayout(LayoutKind.Sequential)]
struct A{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
public uint[] F1;
}

Willy.
Nov 17 '05 #2

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

Similar topics

4
by: Angus Comber | last post by:
Hello I have received a lot of help on my little project here. Many thanks. I have a struct with a string and a long member. I have worked out how to qsort the struct on both members. I can...
1
by: David Resnick | last post by:
I had a problem going from gcc 2.96 to gcc 3.2.3 and narrowed it down to the following code. The question is whether the problem is undefined behavior on the code's part or a compiler bug. ...
16
by: Duncan Mole | last post by:
Hi, This is probably an easy one but it iy first bit of p/invoke. I am trying to use the following C struct in a call: typedef struct { BYTE SRB_Cmd; BYTE SRB_Status, BYTE ...
3
by: Charles Denny | last post by:
I'm trying to invoke CertFindCertificateInStore to find all certificates that have the Code Signing enhanced key attribute. I'd just like to find 1 cert (not even all at this point), however, I...
5
by: Yourko | last post by:
Hi there! I`me currently trying to write some simple programs in C. For one such program i created globals.h file. In that file i defined a structure of type _client, and a pointer of that type: ...
19
by: Sharath A.V | last post by:
I had an argument with someone on wheather this piece of code can invoke undefined bahaviour. I think it does not invoke any undefined behaviour since there is sufficient memory space of 9...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
2
by: rdilipk | last post by:
I am posting at the end of this post some code that P/Invoke's SetSystemTime to set the local system time. This call fails -- i.e the time is not set and the API returns false. However calling...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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.