创建dll的文件
头文件
#define _DLL_MAIN_H_
#ifdef DLLDEM_EXPORTS
#define DLLDEMO_API _declspec(dllexport)
#else
#define DLLDEMO_API _declspec(dllimport)
#endif
DLLDEMO_API void ExportFunc(LPCTSTR pszCountent);
#endif
CPP文件
#include "stdafx.h"
#include "dllmain.h"
#include <stdio.h>
HMODULE g_hModule;
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
g_hModule = (HMODULE)hModule;
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
void ExportFunc(LPCTSTR pszCountent)
{
char sz[MAX_PATH];
GetModuleFileName(g_hModule, sz, MAX_PATH);
MessageBox(NULL, pszCountent, sz,MB_OK);
}
dll的使用
#include "stdafx.h"
#include <Windows.h>
#include "../DLLstudy/dllmain.h"
//#pragma comment(lib, "DLLstudy") 这里可以把lib直接加载到工程之中
int _tmain(int argc, _TCHAR* argv[])
{
ExportFunc("useDll");
return 0;
}