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