dll中的main.cpp
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "stdafx.h"
HMODULE m_gModule;
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
m_gModule = (HMODULE)hModule;
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
_declspec(dllexport) void ExportFunc(LPCTSTR pszContent)
{
char sz[MAX_PATH];
::GetModuleFileNameA(m_gModule, sz, MAX_PATH);
::MessageBoxA(NULL, pszContent, strrchr(sz, '\\')+1, MB_OK);
}
调用 时main.cpp
#include <Windows.h>
#include "DllDemo.h"
#pragma comment(lib, "DllDemo.lib")
void main()
{
ExportFunc("aaa");
}
动态调用方法
在生成DLL上写xx.def
EXPORTS
ExportFunc
#include <Windows.h>
//声明函数原型
typedef void (*PFNEXPORTFUNC)(LPCTSTR);
int main(int argc, char* argv[])
{
//加载DLL
HMODULE hModule = ::LoadLibrary("DllDemo.dll");
if (hModule != NULL)
{
//取得ProcFunc的地址
PFNEXPORTFUNC mExportFunc = (PFNEXPORTFUNC)GetProcAddress(hModule, "ExportFunc");
if (mExportFunc != NULL)
{
mExportFunc("DFA");
}
//卸载库
::FreeLibrary(hModule);
}
return 0;
}