I am trying to call unmanaged code using C#.
extern "C" __declspec(dllexport) LPBYTE DataReceived(LPBYTE signals)
{
LPBYTE test;
*(WORD*)(test) = 0x0C;
*(WORD*)(test + 2) = 0x1000;
return test;
// I even tried returning 0x00 only; and I was still getting the exception
}
C# code
internal sealed class Test
{
[DllImport("testlib.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern byte[] DataReceived(byte[] signals);
}
// signals is byte[] too
byte[] foo = Test.DataReceived(signals);
//exception that occurs
A first chance exception of type 'System.Runtime.InteropServices.MarshalDirectiveException
I am having another function which returns an int value just fine, I guess it's related to LPBYTE itself. Appreciate any help.
I believe you want to use
Note that when you call it, you will need to use
Marshall.Copyto get the data out, but this will require that you know the length of the data.