Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. need help translating one certain line of code from c++ to c#

need help translating one certain line of code from c++ to c#

Scheduled Pinned Locked Moved C#
helpcsharpc++tutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mSh1985
    wrote on last edited by
    #1

    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

    H 1 Reply Last reply
    0
    • M mSh1985

      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

      H Offline
      H Offline
      HosamAly
      wrote on last edited by
      #2

      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.

      My LinkedIn Profile

      M 1 Reply Last reply
      0
      • H HosamAly

        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.

        My LinkedIn Profile

        M Offline
        M Offline
        mSh1985
        wrote on last edited by
        #3

        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

        H 1 Reply Last reply
        0
        • M mSh1985

          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

          H Offline
          H Offline
          HosamAly
          wrote on last edited by
          #4

          Glad it worked. I just noticed that you're defining dbcc_name in C# to be short, even though it's char 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 be sbyte.

          My LinkedIn Profile

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups