调用C++动态链接库出现错误

时间:2021-12-28 15:49:50
解决方式:将托管 System.String 中的内容复制到非托管内存(Marshal.StringToHGlobalAnsi)

class
HttpsSend {
[DllImport(
"httpsSend.dll")]
public static extern IntPtr markInvoice(IntPtr url, IntPtr input);


public static string Send(string url, string content)
{
IntPtr urlPtr
= Marshal.StringToHGlobalAnsi(url);
IntPtr contentPtr
= Marshal.StringToHGlobalAnsi(content);

IntPtr outPutIntPtr
= markInvoice(urlPtr, contentPtr);

string outPut = Marshal.PtrToStringAnsi(outPutIntPtr);

return outPut;
}
}