环境:VS2005
过程:
1、File --> New --> Project --> Win32 project --> 输入工程名(如:TestDll) --> Dll;
2、File --> New --> File --> Header File(.h) ,选择保存到同一根目录下,TestDll.h;
3、在TestDll.h头文件内定义MYLIBAPI标识符,DLL中应用程序使用的函数必须是“导出”的。
- #ifdef MYLIBAPI
- #define MYLIBAPI _declspec(dllimport)
- #else
- #define MYLIBAPI _declspec(dllexport)
- #endif
- void MYLIBAPI ShowText(HDC hdc, char * Buffer, RECT rect);
- void MYLIBAPI MYFUNCTION(HDC hdc, RECT rect);
- void MYLIBAPI StartEvent();
4、在.cpp文件内把函数写好。
如://test.cpp
void ShowTesxt(HDC hdc, char * Buffer, RECT rect)
{
//just for test
MessageBox(NULL, "Test OK", "test", MB_OK);
}
5、编译运行,将会生成一个TestDll.lib和TestDll.dll文件。
引用DLL:
环境:VS2005
过程:
1、File --> New --> Project --> Win32 project --> 输入工程名(如:CallTestDll) --> Windows应用程序;
2、使用前把TestDll.h头文件引用进来,
#include "../../TestDll/TestDll/TestDll.h" //路径要设置好
3、直接使用函数;
4、在“解决方案管理器”内右键CallTestDll工程,选择属性-->链接器--> 输入--> .. --> 输入“TestDll.lib“;
5、把生成的.lib和.dll文件拷贝到该工程文件同一目录下。
6、编译运行。