.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

 

posted @ 2023-01-09 23:07  流氓大菠萝  阅读(242)  评论(4编辑  收藏  举报