zoukankan      html  css  js  c++  java
  • C#调用非托管代码(C++方法)的2种方式

    第一种:静态调用  

    使用using System.Runtime.InteropServices命名空间下的DllImport标签指定非托管的Dll及其细节

    方法必须是静态的即必须加上static关键字,还有extern关键字

      [DllImport("kernel32.dll", EntryPoint = "GetSystemDefaultLCID")]
            static extern ushort GetSystemDefaultLCID();//OS 当前选择的默认语言(区域-管理)
            [DllImport("kernel32.dll", EntryPoint = "GetSystemDefaultLangID")]
            static extern ushort GetSystemDefaultLangID();//OS 当前选择的默认语言(区域-管理)

    调用时与普通的静态方法一致;

    第二种:动态加载

    动态加载时需要使用的一些非托管代码

            //装载动态库
            [DllImport("kernel32.dll")]
            static extern IntPtr LoadLibrary(string lpFileName);
            //获取要引入的函数
            [DllImport("kernel32.dll")]
            static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
            //释放动态链接库
            [DllImport("kernel32.dll", EntryPoint = "FreeLibrary", SetLastError = true)]
            static extern bool FreeLibrary(IntPtr hModule);

    首先定义相应的代理对应的非托管代码(dll)中的相关方法(我们需要的方法)

        public delegate int dicCreateHDICT(int dwWordLangID, string lpcszIdxFileName, string lpcszDatFileName);
        public delegate bool dicFreeHDICT(int hdict);

    再动态加载非托管代码

      public class DLLHandler
        {
            [DllImport("kernel32.dll")]
            static extern IntPtr LoadLibrary(string lpFileName);
            //获取要引入的函数
            [DllImport("kernel32.dll")]
            static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
            //释放动态链接库
            [DllImport("kernel32.dll", EntryPoint = "FreeLibrary", SetLastError = true)]
            static extern bool FreeLibrary(IntPtr hModule);
            //DrDicEnt.dll
            public static dicCreateHDICT dictCreateDICT = null;
            public static dicFreeHDICT dictFreeDict = null;
            public static void Init()
           {
             IntPtr hLib=LoadLibrary(@"D:\Dll\DrDicEnt.dll");
             IntPtr api = GetProcAddress(hLib,"dicCreateHDICT");
         dictCreateDICT = (dicCreateHDICT)Marshal.GetDelegateForFunctionPointer(api,typeof(dicCreateHDICT));
     IntPtr api2 = GetProcAddress(hLib,"dicFreeHDICT");
         dicFreeHDICT= (dicFreeHDICT)Marshal.GetDelegateForFunctionPointer(api2,typeof(dicFreeHDICT));
    
    }

    调用时先DLLHandler.Init();
    再使用DLLHandler.dicCreateHDICT()等方法

  • 相关阅读:
    trailRenderer
    通过sysobjects快速查找SQLServer中是否有某个表、视图、存储过程等对象实操
    浅谈信息系统(IT)项目管理-序幕
    使用open xml 判断sharepoint文档是否损坏
    Sharepoint the file is locked for use domainuser edit.文件被锁定,解锁方式
    sharepoint 列表库指定序号规则
    Biztalk 宏
    Biztalk 在流程中定义将消息保存为文件的文件名
    Biztalk 2013 新特性简介(英)
    devexpress gridview,selectedrowchanged
  • 原文地址:https://www.cnblogs.com/wxj111/p/3077943.html
Copyright © 2011-2022 走看看