当前位置:
文档之家› VS2008静态库和动态库的创建和调用
VS2008静态库和动态库的创建和调用
int AddFun(int x, int y) {
return x + y; } 然后编译生成静态库 libTest.lib.(该文件默认生成在 debug 中)
(2)调用动态库
创建一个 Win32 Console 控制台工程.工程名叫 TestLib 如下:
创建完成后,在 TestLib.cpp 文件中代码如下: #include "stdafx.h" #include "libTest.h" //调用静态库中的头文件 #include <iostream> #pragma comment (lib, "libTest.lib") //连接.lib文件 using namespace std;
{ fPrintFun();
cout << "输入两个参数, 15" << endl;
cout << "AddFun(12, 15):" << fAddFunc(12, 15) << endl;
cout << "MaxFun(12, 15):" << fMaxFunc(12, 15) << endl;
HINSTANCE hInstance = LoadLibraryA("dllTest.dll");//加载
动态库 if(NULL == hInstance) { cout << "Error" << endl; FreeLibrary(hInstance); } else { fAddFunc = (pAddFunc)GetProcAddress(hInstance,
typedef int (*pAddFunc)(int, int);//定义函数指针 typedef int (*pMaxFunc)(int, int); typedef void (*pPrintFun)(void);
int _tmain(int argc, _TCHAR* argv[]) {
pAddFunc fAddFunc; pMaxFunc fMaxFunc; pPrintFun fPrintFun;
点击 Next> 进入下一页面,选择 DLL 和 Export symbols(集成工具提供的部 分例子代码)
点击 Finish.工程建立成功. 现在查看 dllTest.h 中的代码 // This class is exported from the dllTest.dll class DLLTEST_API CdllTest { public:
//求最大的 DLLTEST_API int MaxFun(int x, int y) {
return ((x)>(y))?(x):(y);
} //求和 DLLTEST_API int AddFun(int x, int y) {
return x + y; } //输出 DLLTEST_API void PrintFun(void) {
int _tmain(int argc, _TCHAR* argv[]) {
PrintFun();;//调用静态库函数
cout << "输入两个参数, 15" << endl; cout << "AddFun(10, 15):" << AddFun(10, 15) << endl; ;// 调用静态库函数 getchar(); return 0; }
#endif //LIB_TEST_H 在 libTest.cpp 添加代码如下: #include "stdafx.h" #include "libTest.h" #include <iostream> using namespace std;
void PrintFun(void) {
cout << "调用静态库函数" << endl; }
态库> 中指定的路径有关
然后编译运行.运行结果:
这些是自己学习的一部分总结,但还有很多知识需要学习.
总结:
GetProcAddress 返回错误代码 127
采用“运行期间动态链接”自己的 dll 文件,LoadLibrary 成功获取 dll 模块句 柄,但是 GetProcAddress(hModule, "ExportFunc")却返回 NULL,GetLastError 获取错误代码 127,意思是“找不到指定程序”。 问题所在:用 Depends 工具(VS2010 默认没有,需另行下载: /),查看 dll 的导出函数名称。发现导出函 数名不再是“ExportFunc”,而根据函数的返回类型和参数进行了“decorate”, 变为了“?ExportFunc@@YAXPB_W@Z”。 解决方法: (1)修改 GetProcAddress 的第二个参数为真正的导出函数名称即可; (2)在 dll 工程中添加 DEF 文件,写入如下内容: EXPORTS ExportFunc
然后分别在 HeaderFiles 和 SourceFiles 下分别添加 libTest.h 和 libTest.cpp 文件.
在 libTest.h 添加代码如下: #ifndef LIB_TEST_H
#define LIB_TEST_H
void PrintFun(void); int AddFun(int x, int y);
return 42; } // This is the constructor of a class that has been exported. // see dllTest.h for the class definition CdllTest::CdllTest() {
return; } 这部分代码是自动提供的导出类,导出变量,导出函数定义和实现.
} else {
printf("Error:%d\n",GetLastError()); } } getchar(); return 0; }
在编译运行之前,要把dllTest工程中生成的动态库dllTest.dll文件拷贝到
该工程的文件路径下.和代码
<HINSTANCE hInstance = LoadLibraryA("dllTest.dll");//加载动
VS2008 静态库和动态库的创建和调用
1 静态库的创建和调用
(1)创建 libTest 静态库工程
创建一个 Win32Project 命名为 libTest,点击 OK, 弹出下一页面.
点击 Next 进入下一页面.
选 择 Static Library, 和 默 认 的 Precompiled hearder. 然 后 点 击 Finish.创建完成如下:
说明:extern "C" 防止 GetProcAddress 返回错误代码 127
(但这样可能会不能实现 C++的函数重载-个人理解)
在 dllTest.cpp 添加代码 #include "windows.h" #include <iostream> using namespace std; //自己添加的函数调用
上面这部分代码是提供的模板.现在要写自己的函数调用方法.
在 dllTest.h 添加代码
//自己添加的函数调用 extern "C" DLLTEST_API int MaxFun(int x, int y); extern "C" DLLTEST_API int AddFun(int x, int y); extern "C" DLLTEST_API void PrintFun(void);
CdllTest(void); // TODO: add your methods here. }; extern DLLTEST_API int ndllTest; DLLTEST_API int fndllTest(void); 这部分代码是自动提供的导出类,导出变量,导出函数声明.
查看 dllTest.cpp 中代码 // This is an example of an exported variable DLLTEST_API int ndllTest=0; // This is an example of an exported function. DLLTEST_API int fndllTest(void) {
重新编译 dll 工程。再次用 Depends 工具查看导出函数名称,即为“ExportFunc”。
参考:
/breaksoftware/article/details/362384 7 /violetwangy/item/c35b3b95ecf5374cf042 152a /link?url=CeEbebC7yYb1iS5pptVf6NMsw 41Lt6dUj4-R2klDxlrTnrCjE_WItxgyD9viDQLozcJ9F65qxVPtNPsXs5 vcuytQH8Nx96CN4tvsqU10LVu
TestDll.cpp 中全部代码 // TestDll.cpp : Defines the entry point for the console application. //
#include "stdafx.h" #include "windows.h" #include <iostream> using namespace std;