I don't seem to find a solution to this

B

B Vidyadhar Joshi

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
);
 
W

Willy Denoyette [MVP]

Sure C# can handle it, but it takes some effort to define the structs
correctly in C#, that's why I prefer C++ (MC++) for this kind of native
interop.
However, here your problem is that both structures _DEVICE_INFO and
DeviceInfo do not match (they are different structs!).
Also, GetDeviceInfo is unknown to me, I guess you meant
BluetoothGetDeviceInfo which takes BLUETOOTH_DEVICE_INFO as second
argument?

Here's the BLUETOOTH_DEVICE_INFO struct definition:

[StructLayout(LayoutKind.Sequential)]
struct BLUETOOTH_DEVICE_INFO {
internal uint dwSize;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
internal byte[] address;
uint ulClassofDevice;
bool fConnected;
bool fRemembered;
bool fAuthenticated;
SystemTime stLastSeen;
SystemTime stLastUsed;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 248 )]
internal string szName;
internal ulong BTH_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);
}
}
}
[StructLayout(LayoutKind.Sequential)]
public class SystemTime {
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}

Willy.



B Vidyadhar Joshi said:
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
);
 
B

B Vidyadhar Joshi

Hi Willy,

Thanks. I was able to get this working with help from Grapecity and your
previous replies.

B Vidyadhar Joshi.

Willy Denoyette said:
Sure C# can handle it, but it takes some effort to define the structs
correctly in C#, that's why I prefer C++ (MC++) for this kind of native
interop.
However, here your problem is that both structures _DEVICE_INFO and
DeviceInfo do not match (they are different structs!).
Also, GetDeviceInfo is unknown to me, I guess you meant
BluetoothGetDeviceInfo which takes BLUETOOTH_DEVICE_INFO as second
argument?

Here's the BLUETOOTH_DEVICE_INFO struct definition:

[StructLayout(LayoutKind.Sequential)]
struct BLUETOOTH_DEVICE_INFO {
internal uint dwSize;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
internal byte[] address;
uint ulClassofDevice;
bool fConnected;
bool fRemembered;
bool fAuthenticated;
SystemTime stLastSeen;
SystemTime stLastUsed;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 248 )]
internal string szName;
internal ulong BTH_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);
}
}
}
[StructLayout(LayoutKind.Sequential)]
public class SystemTime {
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}

Willy.



B Vidyadhar Joshi said:
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
);
 

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