zoukankan      html  css  js  c++  java
  • vs2010创建和使用动态链接库(dll)

    本文将创建一个简单的动态链接库,并编写一个应用台控制程序使用该动态链接库,并提出了与实现相关的几个问题,供初学者交流。

    本文包含以下内容:

    创建动态链接库项目

    向动态链接库添加类

    创建引用动态链接库的应用程序

    在控制台应用程序中使用类库的功能

    更丰富的simpledll类和相关问题

    参考资料

    创建动态链接库项目:

    1、打开Microsoft Visual Studio 2010,选择File->New->Project

    2、在New Project中选择Installed Templates->Visual C++->Win32

    3、选择Win32 Console Application,设置名称:simpledll,设置解决方案名:zdddll

    4、单击OK,在出现的Win32 Application WizardOverview对话框中点击Next

    5、在Application Settings中,选择Application type下的DLL

    6、勾选Additional options下的Empty project

    7、单击Finish创建项目。

    向动态链接库添加类:

    1、添加新类头文件。右键单击simpledll项目,Add->New Item,选择Header File(.h),设置名称为simpledll,单击Add

    2、添加新类源文件。右键单击simpledll项目,Add->New Item,选择C++ File(.cpp),设置名称为simpledll,单击Add

    3、为新类添加内容。内容如下:

    头文件simpledll.h:

    [html] view plaincopy
     
    1. //------------------ simpledll.h ----------------  
    2.   
    3. #pragma once;  
    4.   
    5. //该宏完成在dll项目内部使用__declspec(dllexport)导出  
    6. //在dll项目外部使用时,用__declspec(dllimport)导入  
    7. //宏DLL_IMPLEMENT在simpledll.cpp中定义  
    8. #ifdef DLL_IMPLEMENT  
    9. #define DLL_API __declspec(dllexport)  
    10. #else  
    11. #define DLL_API __declspec(dllimport)  
    12. #endif  
    13.   
    14. namespace zdd  
    15. {  
    16.     //导出类  
    17.     class DLL_API SimpleDll  
    18.     {  
    19.     public:  
    20.         SimpleDll();  
    21.         ~SimpleDll();  
    22.   
    23.         int add(int x, int y); //简单方法  
    24.     };  
    25. }  

    源文件simpledll.cpp:
    [cpp] view plaincopy
     
    1. //------------------ simpledll.cpp ----------------  
    2.   
    3. //注意此处的宏定义需要写在#include "simpledll.h"之前  
    4. //以完成在dll项目内部使用__declspec(dllexport)导出  
    5. //在dll项目外部使用时,用__declspec(dllimport)导入  
    6. #define DLL_IMPLEMENT   
    7.   
    8. #include "simpledll.h"  
    9.   
    10. namespace zdd  
    11. {  
    12.     SimpleDll::SimpleDll()  
    13.     {  
    14.   
    15.     }  
    16.   
    17.     SimpleDll::~SimpleDll()  
    18.     {  
    19.   
    20.     }  
    21.   
    22.     int SimpleDll::add(int x, int y)  
    23.     {  
    24.         return x+y;  
    25.     }  
    26. }  

    4、完成后点击Build->Build Solution,生成解决方案。可在~zdddllDebug下查看生成的simpledll.libsimpledll.dll.文件。

    创建引用动态链接库的应用程序:

    1、选择File->New->Project

    2、在New Project中选择Installed Templates->Visual C++->Win32

    3、选择Win32 Console Application,设置名称:usesimpledll。选择Add to solution

    4、单击OK,在出现的Win32 Application WizardOverview对话框中点击Next

    5、在Application Settings中,选择Application type下的Console application

    6、取消Additional options下的Precompiled header,勾选Empty project

    7、单击Finish创建项目。

    在控制台应用程序中使用类库的功能:

    1、为控制台应用程序添加main.cpp。右键单击usesimpledll项目,Add->New Item,选择C++ File(.cpp),设置名称为main,单击Add

    2、为main.cpp添加内容。如下所示:

    [cpp] view plaincopy
     
    1. //------------------ main.cpp -------------------  
    2. #include "simpledll.h"  
    3. using namespace zdd;  
    4.   
    5. #include <iostream>  
    6. using namespace std;  
    7.   
    8. int main(char argc, char**argv)  
    9. {  
    10.     //  
    11.     cout << "----------------------" <<endl;  
    12.     SimpleDll sd;  
    13.     cout << "sd.add: 3+5=" << sd.add(3, 5)<<endl;  
    14.     cout << "sd.getConst(): "<<sd.getConst()<<endl;  
    15.   
    16.     SimpleDll *psd = new SimpleDll;  
    17.     cout << "psd->add: 5+5=" << psd->add(5, 5)<<endl;  
    18.     cout << "psd->getConst(): "<<endl;  
    19.   
    20.     cout << "----------------------" <<endl;  
    21.     cout << "please press Enter exit."<<endl;  
    22.     getchar();  
    23.     return 0;  
    24. }  

    3、引用simpledll项目。右键单击usesimpledll项目,选择Properties->Common Properties->Framework and References。点击Add New Reference,选择simpledll项目,单击OK

    4、设置头文件路径。选择Properties->Configuration Properties->VC++ Directories。在Include Directories项添加$(SolutionDir)simpledll,选择应用,确定。

    5、设置usesimpledll项目为活动项目。右键单击usesimpledll项目,选择Set up StartUp Project

    6、生成解决方案。Debug运行结果如下:

    [cpp] view plaincopy
     
    1. 3+5=8  
    2. 5+5=10  

    更丰富的simpledll类和相关问题:

    simpledll.h文件:
    [cpp] view plaincopy
     
    1. //------------------ simpledll.h ----------------  
    2.   
    3. #pragma once;  
    4.   
    5. //该宏完成在dll项目内部使用__declspec(dllexport)导出  
    6. //在dll项目外部使用时,用__declspec(dllimport)导入  
    7. //宏DLL_IMPLEMENT在simpledll.cpp中定义  
    8. #ifdef DLL_IMPLEMENT  
    9. #define DLL_API __declspec(dllexport)  
    10. #else  
    11. #define DLL_API __declspec(dllimport)  
    12. #endif  
    13.   
    14. namespace zdd  
    15. {  
    16.     //导出类  
    17.     class DLL_API SimpleDll  
    18.     {  
    19.     public:  
    20.         SimpleDll();  
    21.         ~SimpleDll();  
    22.   
    23.         int add(int x, int y); //简单方法  
    24.   
    25.         static int sub(int x, int y);//静态方法  
    26.   
    27.         int getConst(); //  
    28.   
    29.         int getNum();  
    30.   
    31.     private:  
    32.         void setNum(int n);  
    33.         int num;  
    34.     };  
    35.   
    36.     //全局变量  
    37.     int DLL_API number;   
    38.     SimpleDll DLL_API sdd;  
    39.   
    40.     //对于指针,下面两种用法没区别?  
    41.     SimpleDll DLL_API *psdd;  
    42.     SimpleDll *psdd1;  
    43.   
    44.     //方法  
    45.     int DLL_API Add(int a, int b);  
    46.   
    47.     SimpleDll *createClass()  
    48.     {  
    49.         return new SimpleDll;  
    50.     }  
    51.   
    52. /* 
    53.     //问题1:若这样使用,则出现如下错误: 
    54.     // error C2059: syntax error : '__declspec(dllexport)' 
    55.     // error C2143: syntax error : missing ';' before '{' 
    56.     // error : '__declspec(dllimport)' 
    57.     // error C2143: syntax error : missing ';' before '{' 
    58.     // error C2447: '{' : missing function header (old-style formal list?) 
    59.     //为什么? 
    60.     SimpleDll* DLL_API createClass1() 
    61.     { 
    62.         return new SimpleDll; 
    63.     } 
    64. */  
    65.   
    66. /* 
    67.     //问题2:若这样使用,则出现如下错误: 
    68.     //Error 1   error C2491: 'zdd::createClass1' : definition of dllimport function not allowed usesimpledll 
    69.     //为什么? 
    70.     SimpleDll DLL_API * createClass2() 
    71.     { 
    72.         return new SimpleDll; 
    73.     } 
    74. */  
    75.     //问题3:这样使用(实现在.cpp中),编译没有问题。  
    76.     //但在main中应用时回出现以下错误:  
    77.     // error LNK2019: unresolved external symbol "class zdd::SimpleDll * __cdecl zdd::createClass3(void)" (?createClass3@zdd@@YAPAVSimpleDll@1@XZ) referenced in function _main  
    78.     //该如何解决?  
    79.     SimpleDll *createClass3(); //先别这样用  
    80.   
    81.     //全局方法加DLL_API和不加DLL_API时的区别  
    82.     int DLL_API getConst1();  
    83.     //int getConst2(); //不要这样使用  
    84.   
    85.     //也不要这样用  
    86.     //否则当程序发布之后,如果只想通过更新.dll  
    87.     //来达到更新程序数据的目的,比如将此处的100更新成10.  
    88.     //通过下面的方法你是做不到的  
    89.     int getConst2()  
    90.     {  
    91.         return 100;  
    92. //      return 10;  
    93.     }  
    94.   
    95.     //也不要这样用,否则将出现如下错误  
    96.     //error C2491: 'zdd::getConst3' : definition of dllimport function not allowed  
    97. /* 
    98.     int DLL_API getConst3() 
    99.     { 
    100.         return 100; 
    101.     } 
    102. */  
    103.     //不要这样用,同理  
    104.     int others(int a)  
    105.     {  
    106.         return a+10;  
    107.     }  
    108. }  

    simpledll.cpp文件:
    [cpp] view plaincopy
     
    1. //------------------ simpledll.cpp ----------------  
    2.   
    3. //注意此处的宏定义需要写在#include "simpledll.h"之前  
    4. //以完成在dll项目内部使用__declspec(dllexport)导出  
    5. //在dll项目外部使用时,用__declspec(dllimport)导入  
    6. #define DLL_IMPLEMENT   
    7.   
    8. #include "simpledll.h"  
    9.   
    10. namespace zdd  
    11. {  
    12.     SimpleDll::SimpleDll()  
    13.     {  
    14.   
    15.     }  
    16.   
    17.     SimpleDll::~SimpleDll()  
    18.     {  
    19.   
    20.     }  
    21.   
    22.     int SimpleDll::add(int x, int y)  
    23.     {  
    24.         return x+y;  
    25.     }  
    26.   
    27.     int SimpleDll::sub(int x, int y)  
    28.     {  
    29.         return x-y;  
    30.     }  
    31.   
    32.     int SimpleDll::getConst()  
    33.     {  
    34.         return 10; //  
    35.     }  
    36.   
    37.     void SimpleDll::setNum(int n)  
    38.     {  
    39.         num = n;  
    40.     }  
    41.   
    42.     int SimpleDll::getNum()  
    43.     {  
    44.         return num;  
    45.     }  
    46.   
    47.     extern int number = 5;  
    48.   
    49.     int Add(int a, int b)  
    50.     {  
    51.         return a+b;  
    52.     }  
    53.   
    54.     SimpleDll *createClass3()  
    55.     {  
    56.         return new SimpleDll;  
    57.     }  
    58.   
    59.     int getConst1()  
    60.     {  
    61.         return 100;  
    62.         //return 10;  
    63.     }  
    64. /* 
    65.     //error 
    66.     extern int getConst2() 
    67.     { 
    68.         return 100; 
    69.     } 
    70. */  
    71. }  

    main.cpp文件:
    [cpp] view plaincopy
     
    1. //------------------ main.cpp -------------------  
    2. #include "simpledll.h"  
    3. using namespace zdd;  
    4.   
    5. #include <iostream>  
    6. using namespace std;  
    7.   
    8. int main(char argc, char**argv)  
    9. {  
    10.     //  
    11.     cout << "----------------------" <<endl;  
    12.     SimpleDll sd;  
    13.     cout << "sd.add: 3+5=" << sd.add(3, 5)<<endl;  
    14.     cout << "sd.getConst(): "<<sd.getConst()<<endl;  
    15.   
    16.     SimpleDll *psd = new SimpleDll;  
    17.     cout << "psd->add: 5+5=" << psd->add(5, 5)<<endl;  
    18.     cout << "psd->getConst(): "<<endl;  
    19.   
    20.     cout << "----------------------" <<endl;  
    21.     cout << "SimpleDll::sub: 2-1=" << SimpleDll::sub(2,1)<<endl;  
    22.   
    23.     cout << "----------------------" <<endl;  
    24.     cout << "zdd::number: "<<number<<endl;  
    25.     number = 10;  
    26.     cout << "changed zdd::number: "<<number<<endl;  
    27.       
    28.     cout << "----------------------" <<endl;  
    29.     cout << "sdd.add: 6+8=" << sdd.add(6,8)<<endl;  
    30.     cout << "psdd->add: 6+8=" <<psdd->add(6,8)<<endl;  
    31.     cout << "psdd1->add: 6+8=" <<psdd1->add(6,8)<<endl;  
    32.   
    33.     cout <<endl;  
    34.     cout << "sdd.getConst(): "<<sd.getConst()<<endl;  
    35.     cout << "psdd.getConst(): "<<psdd->getConst()<<endl;  
    36.     cout << "psdd1.getConst(): "<<psdd1->getConst()<<endl;  
    37.   
    38.     cout << "----------------------" <<endl;  
    39.     cout << "zdd::Add: 7+8="<<Add(7,8)<<endl;  
    40.   
    41.     cout << "----------------------" <<endl;  
    42.     SimpleDll *p = createClass();  
    43.     cout << "create->add: 2+3=" <<p->add(3,2)<<endl;  
    44.     cout << "create->getConst(): "<<p->getConst()<<endl;  
    45.     cout << "----------------------" <<endl;  
    46.   
    47. //  SimpleDll *p3 = createClass3();  
    48. //  cout << "create3->add: 2+3=" <<p3->add(3,2)<<endl;  
    49. //  cout << "create3->getConst(): "<<p3->getConst()<<endl;  
    50. //  cout << "----------------------" <<endl;  
    51.   
    52.     cout << "----------------------" <<endl;  
    53.     //请别在你的应用程序中使用getConst2  
    54.     cout << "DLL_API getConst1: "<<getConst1()<<endl;  
    55.     cout << "        getConst2: "<<getConst2()<<endl;  
    56. //  cout << "DLL_API getConst3: "<<getConst3()<<endl;  
    57.     cout << "----------------------" <<endl;  
    58.   
    59. //  cout << "others: " << others(6)<<endl;  
    60. //  cout << "others: " << others(16)<<endl;  
    61. //  cout << "----------------------" <<endl;  
    62.   
    63.     cout << "please press Enter exit."<<endl;  
    64.     getchar();  
    65.     return 0;  
    66. }  

    运行结果为:
    [html] view plaincopy
     
    1. ----------------------  
    2. sd.add: 3+5=8  
    3. sd.getConst(): 10  
    4. psd->add: 5+5=10  
    5. psd->getConst():   
    6. ----------------------  
    7. SimpleDll::sub: 2-1=1  
    8. ----------------------  
    9. zdd::number: 5  
    10. changed zdd::number: 10  
    11. ----------------------  
    12. sdd.add: 6+8=14  
    13. psdd->add: 6+8=14  
    14. psdd1->add: 6+8=14  
    15.   
    16. sdd.getConst(): 10  
    17. psdd.getConst(): 10  
    18. psdd1.getConst(): 10  
    19. ----------------------  
    20. zdd::Add: 7+8=15  
    21. ----------------------  
    22. create->add: 2+3=5  
    23. create->getConst(): 10  
    24. ----------------------  
    25. ----------------------  
    26. DLL_API getConst1: 100  
    27.         getConst2: 100  
    28. ----------------------  
    29. please press Enter exit.  

    可将生成的可执行文件和应用程序拷出到同一目录下,通过更改方法getConst1,getConst2,体会dllexport和dllimport的作用。
    代码中提出的几个问题方法,暂时无解,暂不使用。
     
    源码:
     
    参考资料:
  • 相关阅读:
    pycharm 2016.2注册码
    python selenium2
    webdriver.py--解说
    Sahi ---实现 Web 自动化测试
    性能测试
    看云-git类的书籍写作
    IntelliJ IDEA 对于generated source的处理
    各种常用的序列化性能的对比
    rpc框架--grpc-java
    grpc mvn protobuf:compile 过程
  • 原文地址:https://www.cnblogs.com/springbarley/p/3360776.html
Copyright © 2011-2022 走看看