need help translating one certain line of code from c++ to c#
-
Hi all, i didn't know which forum to post this on, so i hope this is the right one (i know i wrote a lot of stuff here, if you should be to lazy to read all of this, i understand :-D , but please read the last codeblock) I am making a program in c# which detects usb devices and does stuff with the info i collect. I don't have a lot of experience with the usb stuff, so i have googled a lot of info together. Most of my code i wrote i translated from a c++ program. But my knowledge of c++ is minimum. The problem is that i'm not able to translate a certain line of code to c# in c++ i have something like this (i just copied a small piece but i think you get the idea)
typedef struct DEV_BROADCAST_DEVICEINTERFACE {
DWORD dbcc_size;
DWORD dbcc_devicetype;
DWORD dbcc_reserved;
GUID dbcc_classguid;
char dbcc_name[1];
}
struct DEV_BROADCAST_HDR {
DWORD dbch_size;
DWORD dbch_devicetype;
DWORD dbch_reserved;
};PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR)lParam;
PDEV_BROADCAST_DEVICEINTERFACE pDevInf;pDevInf = (PDEV_BROADCAST_DEVICEINTERFACE)pHdr;
and in c# i have the following pieces of code
[StructLayout(LayoutKind.Sequential)]
public struct DEV_BROADCAST_HDR
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct
DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
public short dbcc_name;
}DEV_BROADCAST_HDR pHDR = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(msg.LParam, typeof(DEV_BROADCAST_HDR));
DEV_BROADCAST_DEVICEINTERFACE pDevInf;now basicly i just want to translate THIS LINE from c++ to c#
pDevInf = (PDEV_BROADCAST_DEVICEINTERFACE)pHdr;
note: i have no idea what i'm doing, but i will be very happy if someone can give me a clue on how to translate this single line of code to c# thanks in advance
-
Hi all, i didn't know which forum to post this on, so i hope this is the right one (i know i wrote a lot of stuff here, if you should be to lazy to read all of this, i understand :-D , but please read the last codeblock) I am making a program in c# which detects usb devices and does stuff with the info i collect. I don't have a lot of experience with the usb stuff, so i have googled a lot of info together. Most of my code i wrote i translated from a c++ program. But my knowledge of c++ is minimum. The problem is that i'm not able to translate a certain line of code to c# in c++ i have something like this (i just copied a small piece but i think you get the idea)
typedef struct DEV_BROADCAST_DEVICEINTERFACE {
DWORD dbcc_size;
DWORD dbcc_devicetype;
DWORD dbcc_reserved;
GUID dbcc_classguid;
char dbcc_name[1];
}
struct DEV_BROADCAST_HDR {
DWORD dbch_size;
DWORD dbch_devicetype;
DWORD dbch_reserved;
};PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR)lParam;
PDEV_BROADCAST_DEVICEINTERFACE pDevInf;pDevInf = (PDEV_BROADCAST_DEVICEINTERFACE)pHdr;
and in c# i have the following pieces of code
[StructLayout(LayoutKind.Sequential)]
public struct DEV_BROADCAST_HDR
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct
DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
public short dbcc_name;
}DEV_BROADCAST_HDR pHDR = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(msg.LParam, typeof(DEV_BROADCAST_HDR));
DEV_BROADCAST_DEVICEINTERFACE pDevInf;now basicly i just want to translate THIS LINE from c++ to c#
pDevInf = (PDEV_BROADCAST_DEVICEINTERFACE)pHdr;
note: i have no idea what i'm doing, but i will be very happy if someone can give me a clue on how to translate this single line of code to c# thanks in advance
I think this should work:
pDevInf = (PDEV_BROADCAST_DEVICEINTERFACE)Marshal.PtrToStructure(msg.LParam, typeof(PDEV_BROADCAST_DEVICEINTERFACE));
It seems to me that the second struct is merely part of the first one. It's probably just a way of "viewing" the data in memory.
-
I think this should work:
pDevInf = (PDEV_BROADCAST_DEVICEINTERFACE)Marshal.PtrToStructure(msg.LParam, typeof(PDEV_BROADCAST_DEVICEINTERFACE));
It seems to me that the second struct is merely part of the first one. It's probably just a way of "viewing" the data in memory.
thanks for the quick reply this does indeed work, but when comparing the results from the c++ program with my c# program, a few parameters (like the dbcc size) have different values.. (maybe i just screwed up the rest of my code :laugh: ) But to my knowledge i don't need these parameters, and i have the information i need from my usb devices, so i think my problems are solved (for now ;P ) thanks again
-
thanks for the quick reply this does indeed work, but when comparing the results from the c++ program with my c# program, a few parameters (like the dbcc size) have different values.. (maybe i just screwed up the rest of my code :laugh: ) But to my knowledge i don't need these parameters, and i have the information i need from my usb devices, so i think my problems are solved (for now ;P ) thanks again
Glad it worked. I just noticed that you're defining dbcc_name in C# to be
short
, even though it'schar
in C++. This probably means that you may access memory that you shouldn't, and might cause you an access violation. I think the most accurate representation in C# would besbyte
.