zoukankan      html  css  js  c++  java
  • DLLimport 虚拟路径用法

    1.创建类库应用帮助类DLLWrapper

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Runtime.InteropServices;
    
    /// <summary>
    /// Summary description for Class1
    /// </summary>
    public class DLLWrapper
    {
        [DllImport("Kernel32")]
        public static extern int LoadLibrary(String funcname);
        [DllImport("Kernel32")]
        public static extern int GetProcAddress(int handle, String funcname);
        [DllImport("Kernel32")]
        public static extern int FreeLibrary(int handle);
    
        public static Delegate GetFunctionAddress(int dllModule, String functionName, Type t)
        {
            int address = GetProcAddress(dllModule, functionName);
            if (address == 0)
            {
                return null;
            }
            else
                return Marshal.GetDelegateForFunctionPointer(new IntPtr(address), t);
        }
    
        public static Delegate GetDelegateFromIntPtr(IntPtr address, Type t)
        {
            if (address == IntPtr.Zero)
            {
                return null;
            }
            else
                return Marshal.GetDelegateForFunctionPointer(address, t);
        }
    
        public static Delegate GetDelegateFromIntPtr(int address, Type t)
        {
            if (address == 0)
            {
                return null;
            }
            else
                return Marshal.GetDelegateForFunctionPointer(new IntPtr(address), t);
        }
    
    }

    2.引用后台

      1.创建委托delegate

      public delegate bool SaveXxjToXml(string cnnStr, string xxjfile); 参数返回值,对应要引用的方法

      2.引用类库方法

         string msg = string.Empty;
         //加载类库 int hModule = DLLWrapper.LoadLibrary(Server.MapPath("~/dll/SaveToXml.dll")); if (hModule == 0) { msg = "导入信息价类库不存在,或者路径错误,请联系管理员。"; } //注册委托 SaveXxjToXml sxtx = (SaveXxjToXml)DLLWrapper.GetFunctionAddress(hModule, "SaveXxjToXml", typeof(SaveXxjToXml)); if (sxtx==null) { DLLWrapper.FreeLibrary(hModule); msg = "导入信息价类库的方法不存在,请联系管理员。"; } //调用注册后的委托 bool result = sxtx(cnnStr, Server.MapPath(path)); if (result) msg = "导入成功。"; else msg = "导入失败。";
  • 相关阅读:
    玩转MySQL之Linux下的简单操作(服务启动与关闭、启动与关闭、查看版本)
    玩转MySQL之Linux下修改默认编码
    机器学习算法及应用领域相关的中国大牛
    [转载]Python 包管理工具解惑
    Vim常用操作和快捷键技巧总结
    [转载]那些C++牛人的博客
    [转载]学习c/c++的好网站
    [转载]C++内存管理
    [转载]SQL数据库如何加快查询速度
    [转载]Python3.x和Python2.x的区别
  • 原文地址:https://www.cnblogs.com/ruanyifeng/p/2846349.html
Copyright © 2011-2022 走看看