最简单DLL导出加载示例
为了方便DLL API导出及加载方法理解,本例提供最简单的C++ DLL示例。
导出方式两种:
用def文件方式导出MyAPI1。用__declspec (dllexport)方式导出MyAPI2。__declspec参见http://technet.microsoft.com/zh-cn/subscriptions/dabb5z75。加载方式两种,采用隐式+显式链接方式。开发工具VS2005。
1、创建DLL模块创建DLL模块PureDLL。// PureDLL_Test.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <Windows.h>//隐式链接#pragma comment(lib,"PureDLL.lib") //隐式链接函数声明void MyAPI1();extern "C" __declspec(dllimport) void MyAPI2();//隐式调用void ImplicitCall();//显式调用void ExplicitCall();int _tmain(int argc, _TCHAR* argv[]){ ImplicitCall(); ExplicitCall();return 0;}void ImplicitCall(){ MyAPI1(); MyAPI2();}void ExplicitCall(){ HINSTANCE hDllInstance = LoadLibrary(_T("PureDLL.dll")); if (!hDllInstance) { return; } typedef void (*LPFunc)(void); LPFunc pFuncPtr1 = (LPFunc)GetProcAddress(hDllInstance, "MyAPI1"); pFuncPtr1(); LPFunc pFuncPtr2 = (LPFunc)GetProcAddress(hDllInstance, "MyAPI2"); pFuncPtr2(); FreeLibrary(hDllInstance);}