zoukankan      html  css  js  c++  java
  • VC:动态链接库

    MFC

    一、创建DLL

    1、创建MFC动态链接库,在第4部选择“Regular DLL With MFC shared linked”类型,即创建与MFC动态链接的常规DLL,可以被Win32应用程序和MFC应用程序使用。

    2、添加函数。在生成的工程的test.h文件中添加语句如下:

    extern "C" void _declspec(dllexport)print();

    上面添加的是函数的声明语句,然后在.cpp文件中编写函数体。

    extern "C" void _declspec(dllexport)print()

    {

           CString str="DLL";

           AfxMessageBox(str);

    }

    上面的代码加入到语句:

    CTestApp theApp;的后面。

    3、编译、连接。在当前文件的debug文件夹下生成DLL及LIB文件。

    二、使用DLL

    1、新建一个基于对话框的应用程序。添加一个按钮。

    2、按钮的单击响应函数:

           hdll=LoadLibrary("..\\Debug\\MFCDll.dll");//导入动态链接库

           if(hdll)

           {

                  typedef void(*PROCTYPE)();

                  PROCTYPE myprint=(PROCTYPE)GetProcAddress(hdll,"print");//得到函数地址

                  (* myprint)();

           }

           FreeLibrary(hdll);//释放库

    3、把生成的Dll文件复制到指定的目录下。

    Win32

    一、创建Win32 DLL

    1、创建空的Win32动态链接库工程;

    2、添加C++源文件。

    3、在代码区编写函数fun(int a,int b);

    int fun(int a,int b)

    {

           int num1=a;

           int num2=b;

           int temp=0;

           if(a<b){temp=a;a=b;b=temp;}

           while(b!=0)/*利用辗除法,直到b为0为止*/

           {

                  temp=a%b;

                  a=b;

                  b=temp;

           }

           return num1*num2/a;

    }

    4、添加头文件。

    int fun(int a, int b);

    5、添加模块定义文件(.def)。新建记事本文件,将后缀改为.def.

    内容如下:

    ; test.def : Declares the module parameters for the DLL.

    LIBRARY      "Win32Dll"

    DESCRIPTION  'test Windows Dynamic Link Library'

    EXPORTS

    fun; Explicit exports can go here

    6、将模块文件加入到工程。FileView面板-->右击test files-->Add File ToProject。。。

    7、编译、连接。

    二、使用Win32 DLL

    1、将生成的DLL文件、Lib文件和test.h文件,复制到指定的目录下。并且在应用程序的.cpp文件中添加:#include "test.h"

    2、隐式加载Win32 Dll文件。选择Project|Setting命令,在弹出的对话框中选择Link标签,在“Object/Library module”文本框中输入需要导入的库文件lib的文件名。注意lib的路径。

     动态链接库的加载

    1、隐式连接
    (1)"Project"|"Settings"命令,弹出Project Setting对话框
    (2)Link-->Objects/Library modules 添加库模板路径。
    (3)将DLL放到相应的路径下。
    2、显示连接:
    (1)调用LoadLibrary();
    (2)调用GetProAddress();
    (3)调用FreeLibrary();

    动态链接库中数据和函数的导出

    1、创建模块定义文件:*.def

    内容如下:

    LIBRARY filename

    DESCRIPTION description content

    EXPORTS

    functionname

    2、使用关键字_declspec(dllexport)导出函数、变量、类、类的成员

    例如:利用_declspec(dllexport)导出动态链接库中的函数

    int _declspec(dllexport) _cdecl functionname(int a, int b)

    _declspec(dllexport)导出类的成员:

    Class _declspec(dllexport) MyCLASS

    {

    }

  • 相关阅读:
    LINUX下用PHPIZE安装PHP GD扩展
    LNMP下使用Phabricator(一)
    关于学习
    PHP EXCEL相关
    curl_setopt 注意
    JS代码运行延迟
    ajax跨域
    Bootstrap-下拉菜单
    Bootstrap-基本的按钮组
    Bootstrap-基本的输入框组
  • 原文地址:https://www.cnblogs.com/shenchao/p/2941106.html
Copyright © 2011-2022 走看看