使用Boost的DLL库管理动态链接库

Boost 1.61新增了一个DLL库,跟Qt中的QLibrary类似,提供了跨平台的动态库链接库加载、调用等功能。
http://www.boost.org/users/history/version_1_61_0.html

编写一个Test.dll,导出方法Add

 

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. INT WINAPI Add(INT x, INT y)    
  2. {    
  3.     return x + y;    
  4. }  


加载、检查导出方法是否存在、调用方法、卸载应该是最常用的功能了。

 

 

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. int main()    
  2. {    
  3.     auto libPath = "D:\\Test.dll";    
  4.     
  5.     boost::dll::shared_library lib(libPath);    
  6.     lib.has("add");  // false。符号名称是大小写敏感的    
  7.     if (lib.has("Add"))    
  8.     {    
  9.         auto& symbol = lib.get<int __stdcall(int, int)>("Add");    
  10.         std::cout << symbol(5, 10) << std::endl;    
  11.     }    
  12.     
  13.     boost::dll::shared_library lib2;    
  14.     lib2.load(libPath);    
  15.     if (lib2.is_loaded())    
  16.     {    
  17.         auto& symbol = lib.get<int __stdcall(int, int)>("Add");    
  18.         std::cout << symbol(3, 5) << std::endl;    
  19.         lib2.unload();    
  20.     }    
  21.     
  22.     system("pause");    
  23.     return 0;    
  24. }   

 

http://blog.csdn.net/aqtata/article/details/51780423

posted @ 2017-02-23 00:17  findumars  Views(7995)  Comments(1)    收藏  举报