I’m importing a function from an unmanaged DLL into C#. The C++ function signature is as follows
int RF_PowerOnEx(int nDev, int nCardType, DWORD* pdwRXSize, BYTE* lpbRXData)
I am importing it as follows
[DllImport("TP9000.dll")]
public static extern int RF_PowerOnEx(int nDev, int nCardType, out int pdwRXSize, out byte[] lpbRXData);
However doing so gives me a System.AccessViolationException. I have successfully imported the other functions except this particular one. Both pdwRXSize and lpbRXData are treated as output. The integer and buffer is initialized, then passed into the function which then fills up the buffer. Help!!!! I seem to be able to pass input parameters into the DLL but can’t get output parameters. I’ve tried passing a Stringbuilder object to no avail. Can anybody help me? Thanks!
Edit: Typo
I would suggest you declare the managed signature like this
and then marshall the byte array “by hand” directly from unmanaged memory, using the length information which should be set in
pdwRXSize.You really need to know more about the function implementation: in particular, is the caller supposed to do something to release the memory containing the data buffer?