.net mvc5 调用非托管类型的DLL
Global.asax 中 Application_Start添加代码:
var nativePath = Server.MapPath("~/dllReference/ReadCard"); String _path = String.Concat(System.Environment.GetEnvironmentVariable("PATH"), ";", nativePath); System.Environment.SetEnvironmentVariable("PATH", _path, EnvironmentVariableTarget.Process);
创建临时环境变量
要引用得Dll 放入 :/dllReference/ReadCard 项目文件夹中
创建引用类:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Web; namespace YiBaoGongJu.Web.Application.Helpers { public class ThirdHepler { [DllImport("HeaSecReadInfo.dll", EntryPoint = "Init", CharSet = CharSet.Ansi)] public extern static int Init(StringBuilder indata, StringBuilder outdata); [DllImport("HeaSecReadInfo.dll", EntryPoint = "GetClientID", CharSet = CharSet.Ansi)] public extern static int GetClientID(StringBuilder outdata); [DllImport("HeaSecReadInfo.dll", EntryPoint = "ReadSFZ", CharSet = CharSet.Ansi)] public extern static int ReadSFZ(StringBuilder ppath, StringBuilder poutbuffer, StringBuilder poutbusibuff); [DllImport("HeaSecReadInfo.dll", EntryPoint = "ReadCardBas", CharSet = CharSet.Ansi)] public extern static int ReadCardBas(StringBuilder pCardinfo, StringBuilder pbusicardinfo); } }
另外会出现两种错误:
我遇到了其中一种调试了N久(Unable to load DLL),
翻到了 stackoverflow 其中有人提出:
"Unable to load DLL 'xxx.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)" means the file CAN be found BUT it's not able to load it. Try to copy the DLL file to the root folder of your application, some DLL libraries need to be available in the root folder of the application in order for it to work. Or check if there are any other depending DLL files required by it.
"Cannot find DLL 'xxx.dll': ..." means the file CANNOT be found.
最终发现引用得dll,还有其他依赖,把所有dll都拉到文件目录中,ok了....
stackoverflow 地址:
https://stackoverflow.com/questions/9003072/unable-to-load-dll-module-could-not-be-found-hresult-0x8007007e
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南