473,396 Members | 2,036 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,396 software developers and data experts.

I don't seem to find a solution to this

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
);
Nov 16 '05 #1
2 2772
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" <jo*****@knowledgepointsolutions.com> wrote in message
news:OQ**************@tk2msftngp13.phx.gbl...
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
);

Nov 16 '05 #2
Hi Willy,

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

B Vidyadhar Joshi.

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
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" <jo*****@knowledgepointsolutions.com> wrote in message
news:OQ**************@tk2msftngp13.phx.gbl...
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
);


Nov 16 '05 #3

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

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
8
by: middletree | last post by:
I had some text links at the top of all my pages (in one include file), which worked just fine. But I was asked to make it so that people in a certain department, (this is an Intranet app) would...
19
by: LP | last post by:
I am using (trying to) CR version XI, cascading parameters feature works it asks user to enter params. But if page is resubmitted. It prompts for params again. I did set...
1
by: J Rieggle | last post by:
Hi, Having been flamed for my previous HEEEELP! post and called childish, I decided to reframe this problem - I have to have something ready for tomorrow morning, and my server has stopped...
12
by: Nalaka | last post by:
Hi, I suddenly started getting a lot of errors from html validation (some CSS) so I followed the following instructions to disable it. If you'd rather not have these types of HTML validation...
20
by: Tony | last post by:
I have a situation where I want to send data, but I have no need for a response. It seems to me that XMLHTTPRequest is the best way to send the data, but I don't need any response back from the...
10
by: Frank | last post by:
I've done this a few times. In a solution I have a project, Say P1, and need another project that will contain much code that is similar to that of P1. I hope no one gets hung up on why I...
1
by: oneski | last post by:
help --- 403 You don't have permission Im trying to get a basic search to work on my website, but i keep getting a forbidden error come up. Im using WAMP5 server on a vista machine. The error file...
11
by: Jan | last post by:
Hi: Here's a problem I've had for a long time. The client is really running out of patience, and I have no answers. Access2003, front- and back-end. Single form with 4 subforms (each...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.