c# 引用非托管代码的DLL库时,代码如下:
//引用 [DllImport("AspriseOCR.dll", EntryPoint = "OCR")] public static extern IntPtr OCR(string file, int type); private void btnGetCode_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(_strPicPath)) { txtResult.Text = Marshal.PtrToStringAnsi(OCR(_strPicPath, -1)); } }
当运行到txtResult.Text = Marshal.PtrToStringAnsi(OCR(_strPicPath, -1));时报错:
PInvoke 签名与非托管的目标签名不匹配,原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。
解决方法:把[DllImport("AspriseOCR.dll", EntryPoint = "OCR")],改为[DllImport("AspriseOCR.dll", EntryPoint = "OCR",CallingConvention=CallingConvention.Cdecl)]
本文介绍在C#中调用非托管代码DLL库时遇到的PInvoke签名不匹配错误,并提供了解决方案。通过修改DllImport属性的调用约定,确保托管代码与非托管目标签名一致,从而避免运行时错误。
9149

被折叠的 条评论
为什么被折叠?



