Very frustrating dllimport code..
-
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
-
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
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[^][^]
-
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[^][^]
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