DllImport属性应用于方法,要求最少要提供包含入口点的dll的名称。
DllImport的定义如下:
用法示例:[AttributeUsage(AttributeTargets.Method)] public class DllImportAttribute: System.Attribute { public DllImportAttribute(string dllName) {…} //定位参数为dllName public CallingConvention CallingConvention; //入口点调用约定 public CharSet CharSet; //入口点采用的字符接 public string EntryPoint; //入口点名称 public bool ExactSpelling; //是否必须与指示的入口点拼写完全一致,默认false public bool PreserveSig; //方法的签名是被保留还是被转换 public bool SetLastError; //FindLastError方法的返回值保存在这里 public string Value { get {…} } }
[DllImport("kernel32")] private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
以上是用来写入ini文件的一个win32api,因为C++和C#的数据结构不尽相同,所以申明函数时可能需要更改参数类型。用此方式调用Win32API的数据类型对应:DWORD=int或uint,BOOL=bool,预定义常量=enum,结构=struct等等。
DllImport会按照顺序自动去寻找dll的地方: 1、exe所在目录 2、System32目录 3、环境变量目录。
所以只需要你把引用的DLL 拷贝到这三个目录下 就可以不用写路径了,或者用[DllImport(@"C:\OJ\Bin\Judge.dll")]这样指定DLL的绝对路径就可以正常装载。这个问题最常出现在使用第三方非托管DLL组件的时候。
附:ASP.NET网站中如何使用非托管dll
Asp.Net Team的官方解决方案如下: 首先需要确认你引用了哪些组件,那些是托管的,哪些是非托管的.托管的很好办,直接被使用的需要引用,间接使用的需要拷贝到bin目录下.非托管的处理会比较麻烦.实际上,你拷贝到bin没有任何帮助,因为CLR会把文件拷贝到一个临时目录下,然后在那运行web,而CLR只会拷贝托管文件,这就是为什么我们明明把非托管的dll放在了bin下却依然提示不能加载模块了.
具体做法如下:
首先我们在服务器上随便找个地方新建一个目录,假如为C:\DLL。
然后,在环境变量中,给Path变量添加这个目录。
最后,把所有的非托管文件都拷贝到C:\DLL中.或者更干脆的把DLL放到system32目录
对于可以自己部署的应用程序,这样未偿不是一个解决办法,然而,如果我们用的是虚拟空间,我们是没办法把注册PATH变量或者把我们自己的DLL拷到system32目录的。同时我们也不一定知道我们的Dll的物理路径。DllImport里面只能用字符串常量,而不能够用Server.MapPath(@"~/Bin/Judge.dll")来确定物理路径。ASP.NET中要使用DllImport的,必须在先“using System.Runtime.InteropServices;”不过,我发现,调用这种"非托管Dll”相当的慢,可能是因为我的方法需要远程验证吧,但是实在是太慢了。经过一翻研究,终于想到了一个完美的解决办法----动态加载dll
[DllImport("kernel32.dll")] private extern static IntPtr LoadLibrary(String path);[DllImport("kernel32.dll")] private extern static IntPtr GetProcAddress(IntPtr lib, String funcName); [DllImport("kernel32.dll")] private extern static bool FreeLibrary(IntPtr lib);
分别取得了LoadLibrary和GetProcAddress函数的地址,再通过这两个函数来取得我们的DLL里面的函数。
我们可以先用Server.MapPath(@"~/Bin/Judge.dll")来取得我们的DLL的物理路径,然后再用LoadLibrary进行载入,最后用GetProcAddress取得要用的函数地址
以下自定义类的代码完成LoadLibrary的装载和函数调用:
public class DllInvoke { [DllImport("kernel32.dll")] private extern static IntPtr LoadLibrary(String path); [DllImport("kernel32.dll")] private extern static IntPtr GetProcAddress(IntPtr lib, String funcName); [DllImport("kernel32.dll")] private extern static bool FreeLibrary(IntPtr lib); private IntPtr hLib; public DllInvoke(String DLLPath) { hLib = LoadLibrary(DLLPath); } ~DllInvoke() { FreeLibrary(hLib); } //将要执行的函数转换为委托 public Delegate Invoke(String APIName,Type t) { IntPtr api = GetProcAddress(hLib, APIName); return (Delegate)Marshal.GetDelegateForFunctionPointer(api,t); } }
下面代码进行调用
public delegate int Compile(String command, StringBuilder inf); //编译 DllInvoke dll = new DllInvoke(Server.MapPath(@"~/Bin/Judge.dll")); Compile compile = (Compile)dll.Invoke("Compile", typeof(Compile)); StringBuilder inf; compile(@“gcc a.c -o a.exe“,inf);//这里就是调用我的DLL里定义的Compile函数