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. Managed C++/CLI
  4. Very frustrating dllimport code..

Very frustrating dllimport code..

Scheduled Pinned Locked Moved Managed C++/CLI
c++data-structureshelpquestion
3 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.
  • D Offline
    D Offline
    David Moody
    wrote on last edited by
    #1

    I'm trying to call a C dll function passing a structure that looks like this: typedef struct { long lModuleID; DWORD dwAddress; long lType; BOOL fComplete; long lLength; BYTE *pbyData; WORD wTimeoutMS; long lErrors; } MEMACCESS_STRUCT_S; I translate this into the following structure in my managed C++ code: [StructLayout(LayoutKind::Sequential)] public __gc class MEMACCESS_STRUCT_S { public: Int32 devAddr; Int32 memAddr; // DWORD (UInt32) Int32 type; Int16 complete; // BOOL -> TRUE=1, FALSE=0 Int32 length; Byte data[]; Int16 timeout; // WORD (UInt16) Int32 err; }; Everything works great except the variable "data" that holds the byte array. I get garbage when I look at the values for the array. What am I doing wrong? Thank you so much for any help... I'm nearing the point of headbutting my monitor and throwing my mouse out the window over this one! david

    U 1 Reply Last reply
    0
    • D David Moody

      I'm trying to call a C dll function passing a structure that looks like this: typedef struct { long lModuleID; DWORD dwAddress; long lType; BOOL fComplete; long lLength; BYTE *pbyData; WORD wTimeoutMS; long lErrors; } MEMACCESS_STRUCT_S; I translate this into the following structure in my managed C++ code: [StructLayout(LayoutKind::Sequential)] public __gc class MEMACCESS_STRUCT_S { public: Int32 devAddr; Int32 memAddr; // DWORD (UInt32) Int32 type; Int16 complete; // BOOL -> TRUE=1, FALSE=0 Int32 length; Byte data[]; Int16 timeout; // WORD (UInt16) Int32 err; }; Everything works great except the variable "data" that holds the byte array. I get garbage when I look at the values for the array. What am I doing wrong? Thank you so much for any help... I'm nearing the point of headbutting my monitor and throwing my mouse out the window over this one! david

      U Offline
      U Offline
      ursus zeta
      wrote on last edited by
      #2

      I'm guessing here,...but, you're trying to match the signature of the unmanaged struct in your managed code. Probably you need to use the [MarshalAs(UnmanagedType)] attribute on the Byte data[] member. There is an enumeration for this, and I don't know exactly which one it would be. Go over to the MSDN site and search the library for the MarshalAs attribute; there is a section explaining the UnmanagedType enumeration, the default behavior for the various .NET types. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemruntimeinteropservicesunmanagedtypeclasstopic.asp[^][^]

      D 1 Reply Last reply
      0
      • U ursus zeta

        I'm guessing here,...but, you're trying to match the signature of the unmanaged struct in your managed code. Probably you need to use the [MarshalAs(UnmanagedType)] attribute on the Byte data[] member. There is an enumeration for this, and I don't know exactly which one it would be. Go over to the MSDN site and search the library for the MarshalAs attribute; there is a section explaining the UnmanagedType enumeration, the default behavior for the various .NET types. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemruntimeinteropservicesunmanagedtypeclasstopic.asp[^][^]

        D Offline
        D Offline
        David Moody
        wrote on last edited by
        #3

        Thank you for your help. I tried lots of different combinations using the MarshalAs attribute. I eventually ended up pinning (__pin) and assigning my Byte array to an IntPtr in the struct. That worked great! david

        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