• 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                           

     

     




    附件列表

    • 相关阅读:
      查询已存入数据库中的图片,并显示出来
      图像插值的缺点
      windows网络服务之配置网络负载均衡(NLB)群集
      QR 码的位置检测符
      二维条码识别系统设计原理
      教你看懂Code128条形码
      DataMatrix二维条码源码分析检测识别图像位置
      C#条形码生成(五)----Web下的测试
      C# 生产成条形码3种方法
      屏蔽弹出对话框
    • 原文地址:https://www.cnblogs.com/xe2011/p/2923671.html
    Copyright © 2020-2023  润新知