动态链接库dll的 静态加载 与 动态加载
来源:互联网 发布:mysql教程视频下载 编辑:程序博客网 时间:2024/05/16 16:21
dll 两种链接方式 : 动态链接和静态链接(链接亦称加载)
动态链接是指在生成可执行文件时不将所有程序用到的函数链接到一个文件,因为有许多函数在操作系统带的dll文件中,当程序运行时直接从操作系统中找。
而静态链接就是把所有用到的函数全部链接到exe文件中。
动态链接是只建立一个引用的接口,而真正的代码和数据存放在另外的可执行模块中,在运行时再装入;
而静态链接是把所有的代码和数据都复制到本模块中,运行时就不再需要库了。
1.生成 静态链接库 lib 和动态链接库 dll
新建工程 (newdll) win32项目 -> dll
添加.h文件 betabinlib.h
- #ifndef BETABINLIB_H
- #define BETABINLIB_H
- #ifdef NEWDLL_EXPORTS //自动添加的宏 右键工程-属性-配置属性-预处理器-..定义
- #define MYDLL_API extern “C” __declspec(dllexport)
- #else
- #define MYDLL_API extern “C” __declspec(dllimport)
- #endif
- MYDLL_API int add(int x, int y); // 必须加前缀
- #endif
添加.cpp文件 betabinlib.cpp
- #include “stdafx.h”
- #include “betabinlib.h”
- int add(int x, int y)
- {
- return x + y;
- }
编译生成 .dll 和 .lib文件
2.使用
(1)dll的静态加载–将整个dll文件 加载到 .exe文件中
特点:程序较大,占用内存较大,但速度较快(免去 调用函数LOADLIB等)
测试:
需要 .lib 和 .dll两个文件 (.lib 做 引导用),.h文件
main.cpp
- #include <stdio.h>
- #include “betabinlib.h”
- #include <Windows.h>
- #pragma comment(lib, “newdll.lib”)
- int main()
- {
- printf(”2 + 3 = %d \n”, add(2, 3));
- return 0;
- }
(2) dll的动态加载–根据需要加载响应函数,随时可卸载。不会因为找不到dll, 导致程序不能运行(需要自己做判断处理)。
只需要 .lib文件,不需要 .h文件
main.cpp
- #include <stdio.h>
- #include <Windows.h>
- int main()
- {
- HINSTANCE h=LoadLibraryA(“newdll.dll”);
- typedef int (* FunPtr)(int a,int b);//定义函数指针
- if(h == NULL)
- {
- FreeLibrary(h);
- printf(”load lib error\n”);
- }
- else
- {
- FunPtr funPtr = (FunPtr)GetProcAddress(h,”add”);
- if(funPtr != NULL)
- {
- int result = funPtr(3, 3);
- printf(”3 + 3 = %d \n”, result);
- }
- else
- {
- printf(”get process error\n”);
- printf(”%d”,GetLastError());
- }
- FreeLibrary(h);
- }
- return 0;
- }
http://blog.csdn.net/youxin2012/article/details/11538491#comments
main.cpp
- #include <stdio.h>
- #include <Windows.h>
- int main()
- {
- HINSTANCE h=LoadLibraryA(“newdll.dll”);
- typedef int (* FunPtr)(int a,int b);//定义函数指针
- if(h == NULL)
- {
- FreeLibrary(h);
- printf(”load lib error\n”);
- }
- else
- {
- FunPtr funPtr = (FunPtr)GetProcAddress(h,”add”);
- if(funPtr != NULL)
- {
- int result = funPtr(3, 3);
- printf(”3 + 3 = %d \n”, result);
- }
- else
- {
- printf(”get process error\n”);
- printf(”%d”,GetLastError());
- }
- FreeLibrary(h);
- }
- return 0;
- }
阅读全文
0 0
- 动态链接库dll的 静态加载 与 动态加载
- 动态链接库dll的 静态加载 与 动态加载
- 动态链接库dll的 静态加载 与 动态加载
- 动态链接库dll的静态加载与动态加载
- 动态链接库dll的 静态加载 与 动态加载
- 静态链接与动态加载DLL
- 【Windows】 静态链接库和动态链接库,DLL的静态加载和动态加载,两种LIB文件。
- VC++动态链接库(DLL)的加载
- DLL的静态加载和动态加载
- 动态库DLL加载方式-静态加载和动态加载
- 动态库DLL加载方式-静态加载和动态加载
- 动态库DLL加载方式-静态加载和动态加载
- 静态链接库(LIB)和动态链接库(DLL),DLL的静态加载和动态加载,两种LIB文件。
- 静态链接库(LIB)和动态链接库(DLL),DLL的静态加载和动态加载,两种LIB文件。
- 静态链接库(LIB)和动态链接库(DLL),DLL的静态加载和动态加载,两种LIB文件。
- 静态链接库(LIB)和动态链接库(DLL),DLL的静态加载和动态加载,两种LIB文件。
- 静态与动态加载Dll [示例代码]
- 静态加载dll和动态加载dll
- Recycler的适配器和holder
- 提高MD5安全性
- [DesignPattern]StatePattern
- poj 2762
- 命令模式
- 动态链接库dll的 静态加载 与 动态加载
- 【现代前端】HTML基础 DOCTYPE AMP HTML
- 图片上传
- HTTP Status 500
- Spring的Ioc和Di
- Javascript入门一
- [Spring Boot实战系列]
- 最优化问题综述
- 第八章.对象的容纳 -----Thinking in java 更新中……