首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于C#调用 win32的DLL,怎么调用指针呢

2012-07-18 
关于C#调用 win32的DLL,如何调用指针呢?C++中关于TPalg.dll的这个函数定义为:extern C intINTERFACE_EXP

关于C#调用 win32的DLL,如何调用指针呢?
C++中关于TPalg.dll的这个函数定义为:

extern "C" intINTERFACE_EXPORT GetYC_GD(byte *pData);

我引入C#,

  [DllImport("TPalg.dll", CharSet = CharSet.Auto)] //
  public static extern int GetYC_GD(byte[] pData);

然后我:

byte[] pData=new byte[32];//32个字节的内容
GetYC_GD(pData);

怎么跟我说内存不能为read呢?

这种如何调用呢?不要用unsafe之类的句子啊,大侠

[解决办法]
用IntPtr
 [DllImport("TPalg.dll", CharSet = CharSet.Auto)] //
public static extern int GetYC_GD(IntPtr pData);

IntPtr p = System.Runtime.InteropServices.Marshal.AllocHGlobal(32);
//32个字节的内容
GetYC_GD(p);
//...读p
System.Runtime.InteropServices.Marshal.FreeHGlobal(p);

[解决办法]
定义
[DllImport("TPalg.dll", CharSet = CharSet.Auto)] 
public static extern int GetYC_GD([MarshalAs(UnmanagedType.LPArray)]byte[] pData);

调用

byte[] pData=new byte[32];

GetYC_GD(System.Text.Encoding.ASCII.GetBytes(pData));

热点排行