1.当我们在写dll的时候,如何查看我们的动态链接库导出了哪些函数呢?
过程1
(1)打开命令提示符
(2)在命令提示符窗口下cd到动态链接库所在的位置
(3)用dunmpbin -exports Dll1.dll查看过程2
(1)打开命令提示符
(2)在命令提示符窗口下cd到testdll所在的位置
(3)用dunmpbin -imports TestDll1.dll查看
2.代码说明
- Dll1.dll的生成
(1)Dll1.h
#ifdef DLL_API
#else
#define DLL_API /*extern "C"*/ _declspec(dllimport)
#endif
DLL_API int _stdcall add(int a, int b);//导出函数
DLL_API int _stdcall subtract(int a, int b);
//用extern "C"不能导出类的成员函数 这是一个遗憾
class DLL_API ppoint//此时 加上DLL_API 就是导出整个类 和整个类中的成员函数
{
public:
void output(int x, int y);
//若不想导出整个类中的成员函数 就把类前边的DLL_API给注释掉
//在成员函数声明前 加上 DLL_API 就可以导出相应的成员函数而不是整个类中的成员函数
void outputStr(char*str);
};
(2)Dll1.cpp
#define DLL_API /*extern "C"*/ _declspec(dllexport)
#include "Dll1.h"
#include <iostream>
using namespace std;
int _stdcall add(int a, int b)
{
return a + b;
}
int _stdcall subtract(int a, int b)
{
return a - b;
}
void ppoint::output(int x, int y)
{
/* HWND hWdn = GetForegroundWindow();
HDC hDc = GetDC(hWdn);
char buf[20];
memset(buf,0,20);
sprintf(buf, ("x=%d,y=%d"), x, y);
TextOut(hDc,0,0,(LPCWSTR)buf,strlen(buf));
ReleaseDC(hWdn,hDc);*/
cout << "(x,y) = " << "(" << x << "," << y << ")" << endl;
}
void ppoint::outputStr(char*str)
{
cout << "这个字符串是:" << str << endl;
}
- TestDll1的生成
把Dll1.dll和Dll1.lib导入到工程中
把Dll.h加到项目中#include “Dll.h”
(1)TestDll1.cpp
#include "Dll1.h"
#include <iostream>
using namespace std;
#define OK 0
#define ERR -1
int main()
{
cout << "add(12,34) = " << add(12, 34) << endl;
cout << "subtract(12,34) = " << subtract(12, 34) << endl;
ppoint pt;
pt.output(3,55);
pt.outputStr("Hello world!");
return OK;
}
- 查看Dll1.dll的导出函数
- 查看TestDll.exe的导入函数