2
我需要使用非託管VC++ dll。非託管dll函數字節*參數在C#中返回
它需要C#包裝以下兩個功能:
bool ReadData(byte* byteData, byte dataSize);
bool WriteData(byte* byteData, byte dataSize);
,如果他們成功,他們都返回true,否則爲false。
目前在C#中我有一個類(WRAP)與功能:
[DllImport("kirf.dll", EntryPoint = "ReadData", SetLastError=true)]
[return: MarshalAs(UnmanagedType.VariantBool)]
public static extern Boolean ReadData([Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)]out Byte[] bytesData, Byte dataSize);
[DllImport("kirf.dll", EntryPoint = "WriteRFCard", SetLastError = true)]
[return: MarshalAs(UnmanagedType.VariantBool)]
public static extern Boolean WriteRFCard[In]Byte[] bytesData, [In]Byte dataSize);
我再有
byte[] newData = new byte[17];
byte dataSize = 17;
bool result = WRAP.ReadData(out newData, dataSize);
這總是給我一個結果=假,newData = null,而沒有錯誤拋出(或者總是回來成功)。
而且
byte[] bytesData = new Byte[] { (byte)0x9B, (byte)0x80, (byte)0x12, (byte)0x38, (byte)0x5E, (byte)0x0A, (byte)0x74, (byte)0x6E, (byte)0xE6, (byte)0xC0, (byte)0x68, (byte)0xCB, (byte)0xD3, (byte)0xE6, (byte)0xAB, (byte)0x9C, (byte)0x00 };
byte dataSize = 17;
bool actual = WRAP.WriteData(bytesData, dataSize);
任何想法,我可能做錯了什麼?
編輯
這是怎麼了我最終設法它:
[DllImport("kirf.dll", EntryPoint = "ReadData", SetLastError=true)]
[return: MarshalAs(UnmanagedType.VariantBool)]
public static extern Boolean ReadData([Out] Byte[] bytesData, Byte dataSize);
[DllImport("kirf.dll", EntryPoint = "WriteData", SetLastError = true)]
[return: MarshalAs(UnmanagedType.VariantBool)]
public static extern Boolean WriteData([In] Byte[] bytesData, Byte dataSize);
和:
Byte[] newData=new byte[17];
bool result = KABA_KIRF.ReadData(newData, (Byte)newData.Length);
和
bool result = KABA_KIRF.WriteData(data, datasize);
其中數據是傳遞給函數的Byte[]
參數。
您是否確認函數在非託管代碼中工作?一個簡單的C線束? – 2010-11-01 09:36:23