Exception from PtrToStructure argument

Can someone explain the following exception to the argument: I don't like the structure class . This is the reason for the following line of code in my program:

Marshal.PtrToStructure(m.LParam, dbh); 

given that dbh is of type:

 [StructLayout(LayoutKind.Sequential)] public struct Device_Broadcast_Header { public int dbch_size; public int dbch_devicetype; public int dbch_reserved; } 

thanks

+4
source share
3 answers

You cannot call this Marshal.PtrToStructure overload with a value type (i.e. a struct ).

If you call this overload , you can get an instance of your type.

+6
source

Sorry for the lack of sample code, but here is a link that may help you.

Here is the key text from the link above:

The problem does nothing with the RegisterTraceGuids API.

According to Marshal.PtrToStructure (IntPtr, Object) http://msdn.microsoft.com/en-us/library/30ex8z62.aspx , it throws an ArgumentException, you saw when the structure of the structure is not sequential or an explicit or structural type with a short value.

In this case, the structure however, as sequential, the elements in the array (traceGuidReg [i]) are placed on the managed heap due to the array of the object, so you get the error "structure should not be a class of values.

You will need to use the overload Method Marshal.PtrToStructure (IntPtr, Type) http://msdn.microsoft.com/en-us/library/4ca6d5z7.aspx and assign the result PtrToStructure to the elements of the array.

+2
source
 [StructLayout(LayoutKind.Sequential)] public class Device_Broadcast_Header_Wrapper{ public Device_Broadcast_Header Header } Device_Broadcast_Header_Wrapper wapper = new Device_Broadcast_Header_Wrapper(); Marshal.PtrToStructure(m.LParam, wapper); 
0
source

All Articles