Hi,
I have converted a couple of C++ structures to C#.
/*
typedef ULONGLONG BTH_ADDR;
typedef struct _BLUETOOTH_ADDRESS {
union {
BTH_ADDR ullLong; // easier to compare again BLUETOOTH_NULL_ADDRESS
BYTE rgBytes[ 6 ]; // easier to format when broken out
};
} BLUETOOTH_ADDRESS_STRUCT;
#define BLUETOOTH_ADDRESS BLUETOOTH_ADDRESS_STRUCT
#define BLUETOOTH_NULL_ADDRESS ( (ULONGLONG) 0x0 )
*/
[StructLayout(LayoutKind.Explicit)]
internal struct BluetoothAddress
{
[MarshalAs(UnmanagedType.U8)]
[FieldOffset(0)] internal ulong ullLong;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
[FieldOffset(0)] internal byte[] rgBytes;
internal static BluetoothAddress Create()
{
BluetoothAddress ba = new BluetoothAddress();
ba.ullLong = 0;
ba.rgBytes = new byte[6];
return ba;
}
}
/*
typedef struct
{
DWORD dwSize;
BLUETOOTH_ADDRESS address;
WCHAR szName[BLUETOOTH_MAX_NAME_SIZE];
ULONG ulClassofDevice;
USHORT lmpSubversion;
USHORT manufacturer;
} BLUETOOTH_RADIO_INFO;
*/
[StructLayout(LayoutKind.Sequential)]
internal struct BluetoothRadioInfo
{
[MarshalAs(UnmanagedType.U4)]
internal uint dwSize;
BluetoothAddress address;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
char[] szName;
ulong ulClassofDevice;
ushort lmpSubversion;
ushort manufacturer;
internal static BluetoothRadioInfo Create()
{
BluetoothRadioInfo bri = new BluetoothRadioInfo();
bri.address = BluetoothAddress.Create();
bri.dwSize = (uint)Marshal.SizeOf(typeof(BluetoothRadioInfo));
bri.szName = new char[32];
return bri;
}
}
However, when I use these in the function, I get an error.
public void SomeFunction()
{
BluetoothRadioInfo bri = BluetoothRadioInfo.Create();
int retCode = BluetoothGetRadioInfo(phRadio, ref bri);
}
The execution doesn't even come to the above function. When I run the app, I
straight away get the following error:
/*##################################################################################
An unhandled exception of type 'System.TypeLoadException' occurred in
System.Net.Bluetooth.exe
Additional information: Could not load type
System.Net.Bluetooth.BluetoothAddress from assembly System.Net.Bluetooth,
Version=1.0.1787.22198, Culture=neutral, PublicKeyToken=null because it
contains an object field at offset 0 that is incorrectly aligned or
overlapped by a non-object field.
##################################################################################*/
If I change the BluetoothAddress structure to
[StructLayout(LayoutKind.Sequential)], I get error with the function saying
"The dwSize member of the BLUETOOTH_RADIO_INFO structure pointed to by
pRadioInfo is invalid."
I feel I'm doing something wrong with the union structure BluetoothAddress.
Can someone help me please?
TIA.
B Vidyadhar Joshi.
liggett78 - 13 Dec 2004 14:15 GMT
The BLUETOOTH_ADDRESS_STRUCT contains 6 bytes INSIDE it, not as a pointer to
some location, so better:
internal struct BluetoothAddress
{
[MarshalAs(UnmanagedType.U8)]
[FieldOffset(0)] internal ulong ullLong;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
[FieldOffset(0)] internal byte[6] rgBytes;
}
The second structure contains WCHARs (again IN the structure as embedded
char buffer, not as a pointer). I am not sure which type you should choose,
so you better try it out.
Regards, Joerg
> Hi,
>
[quoted text clipped - 96 lines]
>
> B Vidyadhar Joshi.