创建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
def定义文件
EXPORTS
ExportFunc
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")
typedef void (*PfnFun)(LPCTSTR);
int _tmain(int argc, _TCHAR* argv[])
{
HMODULE hModule = LoadLibrary("DLLstudy.dll");
if (NULL != hModule)
{
PfnFun Fun = (PfnFun)(GetProcAddress(hModule, "ExportFunc"));
if (NULL != Fun)
{
Fun("Hello");
}
FreeLibrary(hModule);
}
return 0;
}