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

C# 调用DLL动态库返回值的有关问题,请问~

2013-09-05 
C# 调用DLL动态库返回值的问题,请教~~~急!!!c++里的函数int GetInfo(long handle,unsigned char *tmp/*这

C# 调用DLL动态库返回值的问题,请教~~~急!!!
c++里的函数
int GetInfo(long handle,unsigned char *tmp/*这个值要返回出来*/)
{
       ....................
       retnum = classA->ReadData(tmp,1024);
}

int ClassA::ReadData(unsigned char* buffer/*void * buffer会报Invalid access to memory location错误*/, int limit )
{
    ...................
}

c#调用
[DllImport("CLib.dll", CharSet = CharSet.Ansi,EntryPoint = "GetInfo", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetInfo(long handle,StringBuilder sb/*这里的值用ref,byte[] 都用过了,还不行*/)

请教~~~急!!!



[解决办法]
[System.Runtime.InteropServices.DllImportAttribute("CLib.dll", EntryPoint="GetInfo")]
public static extern  int GetInfo(int handle, System.IntPtr tmp) ;


string str = new string(' ', 1024);
IntPtr tmp = Marshal.StringToHGlobalAnsi(str); 
int i = GetInfo(10, tmp);
string returnStr = Marshal.PtrToStringAnsi(tmp);


[解决办法]
这是DLL的标准方式,只要修改 DllImport 里的就好了,如下:
CallingConvention.StdCall

热点排行