博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Dll入门范例
阅读量:4696 次
发布时间:2019-06-09

本文共 2411 字,大约阅读时间需要 8 分钟。

范例程序下载:

Dll1.h

View Code
#ifndef _DLL1_H_#define _DLL1_H_#ifndef DLL1_API#define DLL1_API __declspec(dllimport)#endif// 导出函数DLL1_API int add(int a, int b);// 导出类class DLL1_API A{public:    int add(int a, int b);};// 导出部分类成员函数 class B{public:    DLL1_API int add(int a, int b);    void Hello();};#endif

Dll1.cpp

View Code
#define DLL1_API __declspec(dllexport)#include "Dll1.h"int add(int a, int b){    return a+b;}int A::add(int a, int b){    return a+b;}int B::add(int a, int b){    return a+b;}void B::Hello(){    return;}

使用范例

View Code
#include "..\\Dll1\\Dll1.h"void CTestDlg::OnBnClickedButton1(){    // TODO: Add your control notification handler code here    // 测试导出函数    CString cstr;    cstr.Format(TEXT("1+3=%d"), add(1, 3));    AfxMessageBox(cstr);    // 测试导出类    A a;    cstr.Format(TEXT("1+4=%d"), a.add(1, 4));    AfxMessageBox(cstr);    // 测试到处部分类成员函数    B b;    cstr.Format(TEXT("4+5=%d"), b.add(4, 5));    AfxMessageBox(cstr);}

说明一

***************************************************************

1.在Dll中抛出异常,在调用Dll的程式中是可以catch的.
2.C++编写的动态链接库给C调用,为了不发生名字改编,需加上extern "C"
如:#define DLL_API extern "C" __declspec(dllexport)
但注意,加了extern "C"之后,不发生名字改编的前提是函数采用的是默认的C调用约定,
即__cdecl.若采用__stdcall调用约定,仍然会发生名字改编.
eg. int __stdcall add(int a, int b);
3.int add(int a, int b);
等价于 int __cdecl add(int a, int b);
4.利用dumpbin查看.exe或DLL的导入导出状况
dumpbin -imports xxx.exe或xxx.dll
dumobin -exports xxx.exe或xxx.dll
dumpbin.exe所在的目录: Visual Studio安装目录的VC\bin目录下.
当然更好的方式是使用可视化的工具Depends.exe查看
***************************************************************

说明二

***************************************************************

利用模块文件xxx.def定义DLL导出函数的名字
1.建立xxx.def文件加入工程
2.xxx.def
LIBRARY xxx
EXPORTS
Add=add @ 1
3.cpp File
int add(int a, int b)
{
    return a+b;
}
***************************************************************

如何动态加载Dll

View Code
// 1.LoadLibraryHINSTANCE hinstLib = LoadLibrary(TEXT("xxx.dll"));if (hinstLib == NULL)    // LoadLibrary failed{    // To get extended error information, call GetLastError().    ......}// 2.GetProcAddresstypedef int (*MyProc)(int a, int b);MyProc ProcFunTest = (MyProc)GetProcAddress(hinstLib, "xxx");if (ProcFunTest == NULL)    // GetProcAddress failed{    // To get extended error information, call GetLastError().    ......}// 3.Call functionProcFunTest(1, 2);// 4.FreeLibraryFreeLibrary(hinstLib);

 

转载于:https://www.cnblogs.com/Hisin/archive/2012/12/22/2829546.html

你可能感兴趣的文章
fatal: remote origin already exists.
查看>>
gridview 自定义value值
查看>>
2018二月实现计划成果及其三月规划
查看>>
类名.class和getClass()区别
查看>>
12/17面试题
查看>>
LeetCode 242. Valid Anagram
查看>>
JSP表单提交乱码
查看>>
如何适应现代雇佣关系
查看>>
团队项目(第五周)
查看>>
SQL 优化经验总结34条
查看>>
开源 视频会议 收藏
查看>>
核心J2EE模式 - 截取过滤器
查看>>
.net开源CMS
查看>>
JdbcTemplate
查看>>
第一次使用maven记录
查看>>
SharePoint服务器端对象模型 之 使用CAML进展数据查询
查看>>
Building Tablet PC Applications ROB JARRETT
查看>>
Adobe® Reader®.插件开发
查看>>
【POJ 3461】Oulipo
查看>>
Alpha 冲刺 (5/10)
查看>>