读书人

c#指针有点难度的有关问题

发布时间: 2012-02-04 15:43:09 作者: rapoo

c#指针,有点难度的问题!

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#中怎么解决这样的指针问题!
怎么休息上面的这段代码?


[解决办法]
use System.IntPtr
System.IntPtr ptr = Marshal.AllocHGlobal( size);

[解决办法]
byte[] bytes = new bytes[16];
System.IntPtr bp = Marshal.UnsafeAddrOfPinnedArrayElement( bytes , 0 );
dc_read( ... , .. , bp );

----
厉害!


[解决办法]
[DllImport( "test.dll ")]
private static extern Int16 dc_read(Int16 icdev, byte _Adr, [MarshalAs(UnmanagedType.U4)] out UInt32 _Data);

//UInt32 要换成 byte

读书人网 >C#

热点推荐