Click to See Complete Forum and Search --> : Very frustrating dllimport code..


moodilicious
October 6th, 2004, 12:43 PM
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

NoHero
October 6th, 2004, 01:48 PM
does "Byte *data" ... not solve your problem?

moodilicious
October 7th, 2004, 10:30 AM
Byte* won't compile... it complains about using a pointer in a managed class. I ended up pinning (__pin) my Byte array and assigning it to an IntPtr. This works great!

david