我正在尝试使用 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# 代码
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
我有另一个函数返回一个 int 值就好了,我猜它与 LPBYTE 本身有关。感谢任何帮助。