日期:2014-05-19  浏览次数:20926 次

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