Non-bittable data in struct

S

Soren S. Jorgensen

Hi,

I got a structure defined in C as:

typedef struct _M4W_SERVICE_MESSAGE
{
ULONG Command;
ULONG Reserved; // Alignment on IA64
LPWSTR FileName;
} M4W_SERVICE_MESSAGE, *PM4W_SERVICE_MESSAGE;

and in C# as:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct SERVICE_MESSAGE
{
public uint Command;
public uint Reserved;
[MarshalAs(UnmanagedType.LPWStr)] public string FileName;
}

but when trying to create a GChandle allocated buffer of type
SERVICE_MESSAGE (to recieve data through a native overlapped operation),
GCHandle.Alloc throws exception:

System.ArgumentException: Object contains non-primitive or non-blittable
data.
at System.Runtime.InteropServices.GCHandle.InternalAlloc(Object value,
GCHandleType type)
at System.Runtime.InteropServices.GCHandle.Alloc(Object value,
GCHandleType type)

Obviously the problem is the string data in the buffer, but why???
I have used other structures (a least i think to remember so) that contained
string data.

When removing the FileName variable from struct no exception occurs!!

Thanks, SSJ
 
C

christery

Think of anything but C, this is what I wrote a couple of weeks ago...

StringBuilder sb1 = new StringBuilder("Hello");
StringBuilder sb2 = new StringBuilder(" world");
sb1.Append(sb2);
Console.WriteLine(sb1); // Hello world
sb1 = sb2;
Console.WriteLine(sb1); // world

And then... it hit me... that time

StringBuilder sb1 = new StringBuilder("Hello");
StringBuilder sb2 = new StringBuilder(" world");
sb1.Append(sb2);
Console.WriteLine(sb1); // Hello world
sb1 = sb2;
Console.WriteLine(sb1); // world
Console.WriteLine(sb2); // world

A string is not always an reference... ;)

Hope this helps...

//CY
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top