我想用C#調用非託管代碼。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
}
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值就好了另一個函數的C#代碼,我想這是對自身LPBYTE相關。感謝任何幫助。
得到它的工作,輝煌。謝謝。 – unmanageddude 2010-12-09 00:30:53