After trying out various methods, I couldn't get the below structure to
work. I read on some forum that combining value to type and reference type
in a [StructLayout(LayoutKind.Explicit)] is not valid. And to implement a
union struct, we need to use [StructLayout(LayoutKind.Explicit)]. If I
combine both the structures, the size of the structure rturned by
Marshal.SizeOf() is wrong. Does someone has an answer? Or is C# not capable
to handle this?
typedef ULONGLONG ADDR;
typedef struct _ADDRESS {
union {
ADDR ullLong;
BYTE rgBytes[ 6 ];
};
} ADDRESS_STRUCT;
typedef struct _DEVICE_INFO {
DWORD dwSize;
ADDR Address;
ULONG ulClassofDevice;
BOOL fConnected;
BOOL fRemembered;
BOOL fAuthenticated;
SYSTEMTIME stLastSeen;
SYSTEMTIME stLastUsed;
WCHAR szName[ MAX_NAME_SIZE ];
} DEVICE_INFO_STRUCT;
[StructLayout(LayoutKind.Sequential)]
internal struct DeviceInfo
{
internal uint dwSize;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
internal byte[] address;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 248)]
internal string szName;
internal uint ulClassofDevice;
internal ushort lmpSubversion;
internal ushort manufacturer;
internal ulong Addr
{
get
{
return (ulong)address[5] + ((ulong)address[4] << 8)
+ ((ulong)address[3] << 16) + ((ulong)address[2] << 24)
+ ((ulong)address[1] << 32) + ((ulong)address[0] << 40);
}
}
internal static DeviceInfo Create()
{
DeviceInfo di = new DeviceInfo();
di.address = new byte[] {34, 56, 56, 234, 12, 34};
//Returns a wrong size.
//This error is returned by GetDeviceInfo function
(ERROR_REVISION_MISMATCH)
//The dwSize member of the DEVICE_INFO structure pointed to by
pDeviceInfo is invalid.
di.dwSize = (uint)Marshal.SizeOf(typeof(DeviceInfo));
return di;
}
}
DWORD GetDeviceInfo(
HANDLE hDevice,
PDEVICE_INFO pDeviceInfo
);