c++写的dll中的函数:
public static extern Int16 dc_read(Int16 icdev, byte _Adr,byte *_Data);
我在c#中的声明:
[DllImport("test.dll")]
byte[] readata = new byte[16];
private static extern Int16 dc_read(icdev, 0, readata);
编译,错误 参数“3”: 无法从“byte[]”转换为“byte*”
在C中这么写是可以的!
请问大家,在C#中怎么解决这样的指针问题!
怎么休息上面的这段代码?
错误 参数“3”: 无法从“out byte[]”转换为“byte*”
继续在线等各位大虾的正解!
System.IntPtr ptr = Marshal.AllocHGlobal( size);
System.IntPtr ptr = Marshal.AllocHGlobal( size);什么意思?没看明白!
在C中声明了一些带指针的参数,在C#中如何才能调用!
在线等正解!
byte[] bytes = new bytes[16];
System.IntPtr bp = Marshal.UnsafeAddrOfPinnedArrayElement( bytes , 0 );
dc_read( ... , .. , bp );
System.IntPtr bp = Marshal.UnsafeAddrOfPinnedArrayElement( bytes , 0 );
dc_read( ... , .. , bp );----
厉害!
byte[] readata = new byte[16];
System.IntPtr bp = Marshal.UnsafeAddrOfPinnedArrayElement(readata, 0);
Int16 readnum = dc_read(icdev, 0, bp);错误 1 与“mifareone.Form1.dc_read(short, byte, byte*)”最匹配的重载方法具有一些无效参数
2. 参数“3”: 无法从“System.IntPtr”转换为“byte*”
[DllImport("test.dll")]
private static extern Int16 dc_read(Int16 icdev, byte _Adr, IntPtr _Data);
返 回:成功则返回 0
例:int st;
unsigned char data[16];
st=dc_read(icdev,4,data);//data 需要得到的数据
C#中声明:[DllImport("test.dll")]
private static extern Int16 dc_read(Int16 icdev, byte _Adr, IntPtr _Data);
调用如下:byte[] readata = new byte[16];
System.IntPtr bp = Marshal.UnsafeAddrOfPinnedArrayElement(readata,0);
Int16 readnum = dc_read(icdev,4,bp);//readnum!=0 大家看看哪里还有错误!谢谢!在线等!
C#默认是unicode
c++ 可能会是ansiusing char* 可以考虑用stringbuilder 而非byte*
private static extern Int16 dc_read(Int16 icdev, byte _Adr, [MarshalAs(UnmanagedType.U4)] out UInt32 _Data);//UInt32 要换成 byte
[MarshalAs(UnmanagedType.U4)] out UInt32
public static extern short dc_read ( int icdev, int adr,[Out] byte[] sdata ); 谢谢大家,大家以后遇到这样的问题就有经验了!
中执行
通过unsafe 关键字 fixed()申明指针
public unsafe void aa(int *p){
*p=4566;
} class test{
public unsafe void bb(){ fixed(int *p){
//代码}
}
}