473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

C++ structure to C# structure - help needed

I have converted a few C++ structures to C# structures. However, when I use
them in the code, I get errors in "internal static BluetoothDeviceInfo
Create()". I feel I'm doing something wrong while converting the union.
Could someone help please?

/*
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 class BluetoothAddress
{
[MarshalAs(UnmanagedType.U8)]
[FieldOffset(0)] internal ulong ullLong;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
[FieldOffset(0)] internal byte[] rgBytes;

internal BluetoothAddress()
{
ullLong = 0;
rgBytes = new byte[6];
}
}
/*
typedef struct _BLUETOOTH_DEVICE_INFO {
DWORD dwSize; // size, in bytes, of this structure - must be the
sizeof(BLUETOOTH_DEVICE_INFO)
BLUETOOTH_ADDRESS Address; // Bluetooth address
ULONG ulClassofDevice; // Bluetooth "Class of Device"
BOOL fConnected; // Device connected/in use
BOOL fRemembered; // Device remembered
BOOL fAuthenticated; // Device authenticated/paired/bonded
SYSTEMTIME stLastSeen; // Last time the device was seen
SYSTEMTIME stLastUsed; // Last time the device was used for other than
RNR, inquiry, or SDP
WCHAR szName[ BLUETOOTH_MAX_NAME_SIZE ]; // Name of the device
} BLUETOOTH_DEVICE_INFO_STRUCT;
#define BLUETOOTH_DEVICE_INFO BLUETOOTH_DEVICE_INFO_STRUCT
typedef BLUETOOTH_DEVICE_INFO * PBLUETOOTH_DEVICE_INFO;
*/
internal struct SystemTime
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}

[StructLayout(LayoutKind.Sequential)]
internal struct BluetoothDeviceInfo
{
[MarshalAs(UnmanagedType.U4)]
internal int dwSize;
[MarshalAs(UnmanagedType.Struct)]
internal BluetoothAddress Address;
internal ulong ulClassOfDevice;
internal bool fConnected;
internal bool fRemembered;
internal bool fAuthenticated;
internal SystemTime stLastSeen;
internal SystemTime stLastUsed;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]
internal char[] szName;

internal static BluetoothDeviceInfo Create()
{
BluetoothDeviceInfo bdi = new BluetoothDeviceInfo();
//Throws an error on the next line
//"Type System.Net.Bluetooth.BluetoothDeviceInfo can not be
marshaled as an unmanaged structure; no meaningful size or offset can be
computed."
bdi.dwSize = Marshal.SizeOf(typeof(BluetoothDeviceInfo));
bdi.szName = new char[32];
return bdi;
}
}
/*
typedef struct _BLUETOOTH_DEVICE_SEARCH_PARAMS {
DWORD dwSize; // IN sizeof this structure
BOOL fReturnAuthenticated; // IN return authenticated devices
BOOL fReturnRemembered; // IN return remembered devices
BOOL fReturnUnknown; // IN return unknown devices
BOOL fReturnConnected; // IN return connected devices
BOOL fIssueInquiry; // IN issue a new inquiry
UCHAR cTimeoutMultiplier; // IN timeout for the inquiry
HANDLE hRadio; // IN handle to radio to enumerate - NULL == all radios
will be searched
} BLUETOOTH_DEVICE_SEARCH_PARAMS;
*/
[StructLayout(LayoutKind.Sequential)]
internal struct BluetoothDeviceSearchParams
{
[MarshalAs(UnmanagedType.U4)]
internal int dwSize;
internal bool fReturnAuthenticated;
internal bool fReturnRemembered;
internal bool fReturnUnknown;
internal bool fReturnConnected;
internal bool fIssueInquiry;
internal ushort cTimeoutMultiplier;
internal IntPtr hRadio;

internal static BluetoothDeviceSearchParams Create(IntPtr hRadio)
{
BluetoothDeviceSearchParams dsp = new BluetoothDeviceSearchParams();
dsp.dwSize = Marshal.SizeOf(typeof(BluetoothDeviceSearchParams) );
dsp.hRadio = hRadio;
return dsp;
}
}

Thanks & Regards,

B Vidyadhar Joshi.
Nov 16 '05 #1
9 4119
I think you're going to have to add an attribute that tells it the size of
the BluetoothAddress class explicitly so that when you call sizeof against
the BluetoothDeviceInfo it knows how to add things up (since you referenced
the BluetoothAddress class internally)

Since you're telling it that you'll explicitly manage the layout of a
BluetoothAddress, and you're starting different fields at the same offset, it
really has no way to know how big the thing is. If you tag it as being a
constanct size of 8, then it will know.

Nov 16 '05 #2
Hi Scott,

Thanks for the reply.

Now, I got the thing working but I get a new 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.23495, Culture=neutral, PublicKeyToken=null because it
contains an object field at offset 0 that is incorrectly aligned or
overlapped by a non-object field.

Any idea? I guess it is the error with the BluetoothAddress struct (union).
But can't figure out what is wrong.

TIA.

B Vidyadhar Joshi

"Scott" <Sc***@discussions.microsoft.com> wrote in message
news:63**********************************@microsof t.com...
I think you're going to have to add an attribute that tells it the size of
the BluetoothAddress class explicitly so that when you call sizeof against
the BluetoothDeviceInfo it knows how to add things up (since you
referenced
the BluetoothAddress class internally)

Since you're telling it that you'll explicitly manage the layout of a
BluetoothAddress, and you're starting different fields at the same offset,
it
really has no way to know how big the thing is. If you tag it as being a
constanct size of 8, then it will know.

Nov 16 '05 #3

"B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi Scott,

Thanks for the reply.

Now, I got the thing working but I get a new 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.23495, Culture=neutral, PublicKeyToken=null because it
contains an object field at offset 0 that is incorrectly aligned or
overlapped by a non-object field.

Any idea? I guess it is the error with the BluetoothAddress struct
(union). But can't figure out what is wrong.


This won't work, you can't overlap a non-object field (a value) with an
object field in .NET, they are allocated differently.
One common mistake people make is to think .NET structs are the same as C
(or C++) structs, they are NOT .

To solve your problem you don't need a struct, just declare your address as
a byte[8] and use BitConverter.ToUInt64 to convert it to a ulong.

Willy.


Nov 16 '05 #4
Hi Willy,

Sorry for the ignorance; but I couldn't understand what you meant. Can you
bea bit more verbose or just convert the following struct?

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 )

typedef struct _BLUETOOTH_RADIO_INFO {
DWORD dwSize;
BLUETOOTH_ADDRESS address;
WCHAR szName[ BLUETOOTH_MAX_NAME_SIZE ];
ULONG ulClassofDevice;
USHORT lmpSubversion;
USHORT manufacturer;
} BLUETOOTH_RADIO_INFO, *PBLUETOOTH_RADIO_INFO;

Thanks for the help.

B Vidyadhar Joshi

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...

"B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi Scott,

Thanks for the reply.

Now, I got the thing working but I get a new 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.23495, Culture=neutral, PublicKeyToken=null because it
contains an object field at offset 0 that is incorrectly aligned or
overlapped by a non-object field.

Any idea? I guess it is the error with the BluetoothAddress struct
(union). But can't figure out what is wrong.


This won't work, you can't overlap a non-object field (a value) with an
object field in .NET, they are allocated differently.
One common mistake people make is to think .NET structs are the same as C
(or C++) structs, they are NOT .

To solve your problem you don't need a struct, just declare your address
as a byte[8] and use BitConverter.ToUInt64 to convert it to a ulong.

Willy.

Nov 16 '05 #5
const int BLUETOOTH_MAX_NAME_SIZE = 255;

[StructLayout(LayoutKind.Sequential)]
struct _BLUETOOTH_RADIO_INFO {
internal uint dwSize;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
internal byte[] address;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
BLUETOOTH_MAX_NAME_SIZE )]
internal string szName;
internal uint ulClassofDevice;
internal ushort lmpSubversion;
internal ushort manufacturer;
// Return a ulong from the 6 byte array, respecting the byte order,
don't know why one would ever need this in ulong form though..
// If byte order is not important (but I guess it is) you could use
BitConverter.ToUInt64(address, 0);
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);
}
}
}
Usage:

_BLUETOOTH_RADIO_INFO bi = new _BLUETOOTH_RADIO_INFO();
bi.address = new byte[] {34, 56, 56, 234, 12, 34};
Console.WriteLine("{0:x}", bi.BTH_ADDR);

Willy.

"B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in message
news:eD**************@TK2MSFTNGP11.phx.gbl...
Hi Willy,

Sorry for the ignorance; but I couldn't understand what you meant. Can you
bea bit more verbose or just convert the following struct?

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 )

typedef struct _BLUETOOTH_RADIO_INFO {
DWORD dwSize;
BLUETOOTH_ADDRESS address;
WCHAR szName[ BLUETOOTH_MAX_NAME_SIZE ];
ULONG ulClassofDevice;
USHORT lmpSubversion;
USHORT manufacturer;
} BLUETOOTH_RADIO_INFO, *PBLUETOOTH_RADIO_INFO;

Thanks for the help.

B Vidyadhar Joshi

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...

"B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi Scott,

Thanks for the reply.

Now, I got the thing working but I get a new 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.23495, Culture=neutral,
PublicKeyToken=null because it contains an object field at offset 0 that
is incorrectly aligned or overlapped by a non-object field.

Any idea? I guess it is the error with the BluetoothAddress struct
(union). But can't figure out what is wrong.


This won't work, you can't overlap a non-object field (a value) with an
object field in .NET, they are allocated differently.
One common mistake people make is to think .NET structs are the same as C
(or C++) structs, they are NOT .

To solve your problem you don't need a struct, just declare your address
as a byte[8] and use BitConverter.ToUInt64 to convert it to a ulong.

Willy.


Nov 16 '05 #6
Hi Willy,

Thanks for the help.

When I implemented your suggested code, I get a compile error on the line "
BitConverter.ToUInt64(address, 0);":

Invalid token '(' in class, struct, or interface member declaration

TIA

B Vidyadhar Joshi
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
const int BLUETOOTH_MAX_NAME_SIZE = 255;

[StructLayout(LayoutKind.Sequential)]
struct _BLUETOOTH_RADIO_INFO {
internal uint dwSize;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
internal byte[] address;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
BLUETOOTH_MAX_NAME_SIZE )]
internal string szName;
internal uint ulClassofDevice;
internal ushort lmpSubversion;
internal ushort manufacturer;
// Return a ulong from the 6 byte array, respecting the byte order,
don't know why one would ever need this in ulong form though..
// If byte order is not important (but I guess it is) you could use
BitConverter.ToUInt64(address, 0);
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);
}
}
}
Usage:

_BLUETOOTH_RADIO_INFO bi = new _BLUETOOTH_RADIO_INFO();
bi.address = new byte[] {34, 56, 56, 234, 12, 34};
Console.WriteLine("{0:x}", bi.BTH_ADDR);

Willy.

"B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in message
news:eD**************@TK2MSFTNGP11.phx.gbl...
Hi Willy,

Sorry for the ignorance; but I couldn't understand what you meant. Can
you bea bit more verbose or just convert the following struct?

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 )

typedef struct _BLUETOOTH_RADIO_INFO {
DWORD dwSize;
BLUETOOTH_ADDRESS address;
WCHAR szName[ BLUETOOTH_MAX_NAME_SIZE ];
ULONG ulClassofDevice;
USHORT lmpSubversion;
USHORT manufacturer;
} BLUETOOTH_RADIO_INFO, *PBLUETOOTH_RADIO_INFO;

Thanks for the help.

B Vidyadhar Joshi

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...

"B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi Scott,

Thanks for the reply.

Now, I got the thing working but I get a new 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.23495, Culture=neutral,
PublicKeyToken=null because it contains an object field at offset 0
that is incorrectly aligned or overlapped by a non-object field.

Any idea? I guess it is the error with the BluetoothAddress struct
(union). But can't figure out what is wrong.
This won't work, you can't overlap a non-object field (a value) with an
object field in .NET, they are allocated differently.
One common mistake people make is to think .NET structs are the same as
C (or C++) structs, they are NOT .

To solve your problem you don't need a struct, just declare your address
as a byte[8] and use BitConverter.ToUInt64 to convert it to a ulong.

Willy.



Nov 16 '05 #7
Dont use the BitConverter it won't work, you should use the following:

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

Willy.

"B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Hi Willy,

Thanks for the help.

When I implemented your suggested code, I get a compile error on the line
" BitConverter.ToUInt64(address, 0);":

Invalid token '(' in class, struct, or interface member declaration

TIA

B Vidyadhar Joshi
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
const int BLUETOOTH_MAX_NAME_SIZE = 255;

[StructLayout(LayoutKind.Sequential)]
struct _BLUETOOTH_RADIO_INFO {
internal uint dwSize;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
internal byte[] address;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
BLUETOOTH_MAX_NAME_SIZE )]
internal string szName;
internal uint ulClassofDevice;
internal ushort lmpSubversion;
internal ushort manufacturer;
// Return a ulong from the 6 byte array, respecting the byte order,
don't know why one would ever need this in ulong form though..
// If byte order is not important (but I guess it is) you could use
BitConverter.ToUInt64(address, 0);
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);
}
}
}
Usage:

_BLUETOOTH_RADIO_INFO bi = new _BLUETOOTH_RADIO_INFO();
bi.address = new byte[] {34, 56, 56, 234, 12, 34};
Console.WriteLine("{0:x}", bi.BTH_ADDR);

Willy.

"B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in
message news:eD**************@TK2MSFTNGP11.phx.gbl...
Hi Willy,

Sorry for the ignorance; but I couldn't understand what you meant. Can
you bea bit more verbose or just convert the following struct?

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 )

typedef struct _BLUETOOTH_RADIO_INFO {
DWORD dwSize;
BLUETOOTH_ADDRESS address;
WCHAR szName[ BLUETOOTH_MAX_NAME_SIZE ];
ULONG ulClassofDevice;
USHORT lmpSubversion;
USHORT manufacturer;
} BLUETOOTH_RADIO_INFO, *PBLUETOOTH_RADIO_INFO;

Thanks for the help.

B Vidyadhar Joshi

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...

"B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...
> Hi Scott,
>
> Thanks for the reply.
>
> Now, I got the thing working but I get a new 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.23495, Culture=neutral,
> PublicKeyToken=null because it contains an object field at offset 0
> that is incorrectly aligned or overlapped by a non-object field.
>
> Any idea? I guess it is the error with the BluetoothAddress struct
> (union). But can't figure out what is wrong.
>

This won't work, you can't overlap a non-object field (a value) with an
object field in .NET, they are allocated differently.
One common mistake people make is to think .NET structs are the same as
C (or C++) structs, they are NOT .

To solve your problem you don't need a struct, just declare your
address as a byte[8] and use BitConverter.ToUInt64 to convert it to a
ulong.

Willy.




Nov 16 '05 #8
Hi Willy,

Thanks for the help. I understood it later that BitConverter should not be
used.

When I use this struct in the function, it compiles fine and runs. But the
function returns an error ERROR_REVISION_MISMATCH which is documented in
Platform SDK documentation as "The dwSize member of the BLUETOOTH_RADIO_INFO
structure pointed to by pRadioInfo is invalid.". Any idea on this one?

B Vidyadhar Joshi.
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:u8**************@TK2MSFTNGP14.phx.gbl...
Dont use the BitConverter it won't work, you should use the following:

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

Willy.

"B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Hi Willy,

Thanks for the help.

When I implemented your suggested code, I get a compile error on the line
" BitConverter.ToUInt64(address, 0);":

Invalid token '(' in class, struct, or interface member declaration

TIA

B Vidyadhar Joshi
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
const int BLUETOOTH_MAX_NAME_SIZE = 255;

[StructLayout(LayoutKind.Sequential)]
struct _BLUETOOTH_RADIO_INFO {
internal uint dwSize;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
internal byte[] address;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
BLUETOOTH_MAX_NAME_SIZE )]
internal string szName;
internal uint ulClassofDevice;
internal ushort lmpSubversion;
internal ushort manufacturer;
// Return a ulong from the 6 byte array, respecting the byte order,
don't know why one would ever need this in ulong form though..
// If byte order is not important (but I guess it is) you could use
BitConverter.ToUInt64(address, 0);
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);
}
}
}
Usage:

_BLUETOOTH_RADIO_INFO bi = new _BLUETOOTH_RADIO_INFO();
bi.address = new byte[] {34, 56, 56, 234, 12, 34};
Console.WriteLine("{0:x}", bi.BTH_ADDR);

Willy.

"B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in
message news:eD**************@TK2MSFTNGP11.phx.gbl...
Hi Willy,

Sorry for the ignorance; but I couldn't understand what you meant. Can
you bea bit more verbose or just convert the following struct?

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 )

typedef struct _BLUETOOTH_RADIO_INFO {
DWORD dwSize;
BLUETOOTH_ADDRESS address;
WCHAR szName[ BLUETOOTH_MAX_NAME_SIZE ];
ULONG ulClassofDevice;
USHORT lmpSubversion;
USHORT manufacturer;
} BLUETOOTH_RADIO_INFO, *PBLUETOOTH_RADIO_INFO;

Thanks for the help.

B Vidyadhar Joshi

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
>
> "B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in
> message news:%2****************@TK2MSFTNGP14.phx.gbl...
>> Hi Scott,
>>
>> Thanks for the reply.
>>
>> Now, I got the thing working but I get a new 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.23495, Culture=neutral,
>> PublicKeyToken=null because it contains an object field at offset 0
>> that is incorrectly aligned or overlapped by a non-object field.
>>
>> Any idea? I guess it is the error with the BluetoothAddress struct
>> (union). But can't figure out what is wrong.
>>
>
> This won't work, you can't overlap a non-object field (a value) with
> an object field in .NET, they are allocated differently.
> One common mistake people make is to think .NET structs are the same
> as C (or C++) structs, they are NOT .
>
> To solve your problem you don't need a struct, just declare your
> address as a byte[8] and use BitConverter.ToUInt64 to convert it to a
> ulong.
>
> Willy.
>
>
>
>



Nov 16 '05 #9
Hi Willy,

You have clubbed the BluetoothAddress structure into BluetoothRadioInfo
which adds one more member to the structure. Will this not report a wrong
structure size when Marshal.SizeOf() is used?

B Vidyadhar Joshi

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:u8**************@TK2MSFTNGP14.phx.gbl...
Dont use the BitConverter it won't work, you should use the following:

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

Willy.

"B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Hi Willy,

Thanks for the help.

When I implemented your suggested code, I get a compile error on the line
" BitConverter.ToUInt64(address, 0);":

Invalid token '(' in class, struct, or interface member declaration

TIA

B Vidyadhar Joshi
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
const int BLUETOOTH_MAX_NAME_SIZE = 255;

[StructLayout(LayoutKind.Sequential)]
struct _BLUETOOTH_RADIO_INFO {
internal uint dwSize;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
internal byte[] address;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
BLUETOOTH_MAX_NAME_SIZE )]
internal string szName;
internal uint ulClassofDevice;
internal ushort lmpSubversion;
internal ushort manufacturer;
// Return a ulong from the 6 byte array, respecting the byte order,
don't know why one would ever need this in ulong form though..
// If byte order is not important (but I guess it is) you could use
BitConverter.ToUInt64(address, 0);
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);
}
}
}
Usage:

_BLUETOOTH_RADIO_INFO bi = new _BLUETOOTH_RADIO_INFO();
bi.address = new byte[] {34, 56, 56, 234, 12, 34};
Console.WriteLine("{0:x}", bi.BTH_ADDR);

Willy.

"B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in
message news:eD**************@TK2MSFTNGP11.phx.gbl...
Hi Willy,

Sorry for the ignorance; but I couldn't understand what you meant. Can
you bea bit more verbose or just convert the following struct?

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 )

typedef struct _BLUETOOTH_RADIO_INFO {
DWORD dwSize;
BLUETOOTH_ADDRESS address;
WCHAR szName[ BLUETOOTH_MAX_NAME_SIZE ];
ULONG ulClassofDevice;
USHORT lmpSubversion;
USHORT manufacturer;
} BLUETOOTH_RADIO_INFO, *PBLUETOOTH_RADIO_INFO;

Thanks for the help.

B Vidyadhar Joshi

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
>
> "B Vidyadhar Joshi" <jo*****@knowledgepointsolutions.com> wrote in
> message news:%2****************@TK2MSFTNGP14.phx.gbl...
>> Hi Scott,
>>
>> Thanks for the reply.
>>
>> Now, I got the thing working but I get a new 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.23495, Culture=neutral,
>> PublicKeyToken=null because it contains an object field at offset 0
>> that is incorrectly aligned or overlapped by a non-object field.
>>
>> Any idea? I guess it is the error with the BluetoothAddress struct
>> (union). But can't figure out what is wrong.
>>
>
> This won't work, you can't overlap a non-object field (a value) with
> an object field in .NET, they are allocated differently.
> One common mistake people make is to think .NET structs are the same
> as C (or C++) structs, they are NOT .
>
> To solve your problem you don't need a struct, just declare your
> address as a byte[8] and use BitConverter.ToUInt64 to convert it to a
> ulong.
>
> Willy.
>
>
>
>



Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: brett valjalo | last post by:
Hey Gang! SORRY ABOUT THE LENGTH! Nice to see some of the same faces around this place from way back when ... Whatta buncha CDMA addicts some o' y'all are ;) Don't get me wrong, I...
4
by: Excluded_Middle | last post by:
how to convert these two notation in pointer form: 1 - list = value; (explaination) - list is an array of structure of type M. - count is integer index - value is a structure of type M. 2 -...
4
by: Christian Kaiser | last post by:
Good evening (here in Germany at least), I have a structure, expressed in C++ as: clsByte2 m_Sensor; clsByte1 m_Output; clsByte1 m_PowerReduction; clsByte1 m_AnalogOutput; clsByte1...
6
by: zacks | last post by:
I recently wrote a VB.NET application using VS2005. I needed to be able to build linked lists in memory of items that were represented by a group of variables. For example, Friend Structure foo...
14
by: zoltan | last post by:
Hi, Consider a structure as follows : struct dummy { int a; int b; int c; };
11
by: Lance | last post by:
Hi all, I've got a some structures defined as ////// <StructLayout(LayoutKind.Sequential)Public Structure GM_LayerInfo_t Public mDescription As String Public mNativeRect As GM_Rectangle_t...
12
by: Kislay | last post by:
case 1 : struct s { char c1; // 8 double d; // 8 int i1; // 4 char c2; // 4 int i2; // 4 };
3
by: larry | last post by:
I have been working on a rather large project. And I have been thinking about the structure of how I organize my project apps and libraries. It's intended as a framework type system where you can...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
8
by: Andrew Smallshaw | last post by:
I'm working on a data structure that began life as a skip list derivative, but has evolved to the point that it now only has a passing resemblance to them. Each node of this structure has a few...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.