函数如下
extern "C" _declspec(dllexport) char* __stdcall myfunction(const char* keyfile,const char* in_str, int* out_len);我在C#工程里添加
using System.Runtime.InteropServices;
[DllImport("mydll.dll",CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)] IntPtr len=new IntPtr(10);
System.String ret=DecPn("test1", "test2", len);
编译报错:尝试读取或写入受保护的内存。这通常指示其他内存已损坏
extern "C" _declspec(dllexport) char* __stdcall myfunction(const char* keyfile,const char* in_str, int* out_len);我在C#工程里添加
using System.Runtime.InteropServices;
[DllImport("mydll.dll",CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)] IntPtr len=new IntPtr(10);
System.String ret=DecPn("test1", "test2", len);
编译报错:尝试读取或写入受保护的内存。这通常指示其他内存已损坏
public static extern StringBuilder myfunction(String keyfile, String in_str, out int out_len);int len=0;
StringBuilder ret = new StringBuilder();
ret = DecPn("test1", "test2", out len);最后一个参数改成ref int 或IntPtr都没用...
public static extern IntPtr myfunction(string keyfile, string inStr, ref int len);int len;
IntPtr ret = myfunction("test1", "test2", ref len);string result = Marshal.PtrToStringAnsi(ret);可能有的问题是不能显式释放内存。