Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / December 2004

Tip: Looking for answers? Try searching our database.

C# Structure problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
B Vidyadhar Joshi - 22 Nov 2004 06:54 GMT
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.