zoukankan      html  css  js  c++  java
  • LoadLibrary

    Win32 Dyanmic-Link Library的调用LoadLibrary    
    关键点

    LoadLibrary

    The LoadLibrary function maps the specified executable module into the address space of the calling process.

    For additional load options, use the LoadLibraryEx function.

    HMODULE LoadLibrary(

      LPCTSTR lpFileName   // file name of module

    );

    GetProcAddress

    The GetProcAddress function retrieves the address of the specified exported dynamic-link library (DLL) function.

    FARPROC GetProcAddress(

      HMODULE hModule,   // handle to DLL module

      LPCSTR lpProcName   // function name

    );

    FreeLibrary

    The FreeLibrary function decrements the reference count of the loaded dynamic-link library (DLL). When the reference count reaches zero, 

    the module is unmapped from the address space of the calling process and the handle is no longer valid.

     

    BOOL FreeLibrary(

      HMODULE hModule   // handle to DLL module

    );

     

     

    实现过程

     

    1.新建1个 MFC AppWizard(exe)项目,Project name:MFC01

    2.选中Dialog Base对话框类型的程序。

    3.删除多余的控件,添加1个按钮,编译一下。

    4.将project01 Debug里面的project01.dll复制到MFC01的Debug目录下

    5.调用dll代码如下

    void CMfc01Dlg::OnButton1() 
    {
        HMODULE hModule=LoadLibrary("project01.dll");
        typedef int (*Function)(int x,int y);
        if (hModule)
        {
            Function Fuc=(Function)GetProcAddress(hModule,"add");
            if (Fuc)
            {
                CString s;
                s.Format("3+1=%d",Fuc(3,1));
                MessageBox(s);
            }
            FreeLibrary(hModule);   
        }  
     

       

     

    备注

     
    注:LoadLibrary与只能调用 DLL 里面带 extern "C"的
    ADDPROC是一个变量
    ADDPROC SUB= (ADDPROC)GetProcAddress(hModule,"sub");
     
    如果调用Windows的Dll
    typedef int (*Function)(int x,int y);修改为
    typedef int (WINAPI *Function)(int x,int y);
     
    本例的DLL代码
     
    1.创建1个 Win32 Dyanmic-Link Library ,Project name:project01
    2.选中 An empty DLL project.
    3.Ctrl+N 新建一个 C++ Source File ,File:project01
    Win32 Dyanmic-Link Library的创建

    extern "C" __declspec(dllexport) int add(int x,int y)
    {
        return x+y;
    }
    extern "C" __declspec(dllexport) int sub(int x,int y)
    {
        return x-y;
    }

     

    相关链接

    部分API无法直接使用需要调用系统DLL里面的API                           

     

     




    附件列表

    • 相关阅读:
      php的语句
      php
      git分支
      git安装及git命令的用法
      git命令
      dos命令及github介绍
      无缝轮播的案例 及css3无缝轮播案例
      ACWING 031 表示数值的字符串
      Acwing 282. 石子合并 区间dp
      Leetcode 841. 钥匙和房间 dfs bfs
    • 原文地址:https://www.cnblogs.com/xe2011/p/2923671.html
    Copyright © 2011-2022 走看看