日期:2014-05-17  浏览次数:21072 次

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