1、动态库生成:
秀丽的曲奇.cpp
#include <windows.h>#include "CalSeedKey.h"DLL_API int _key = 255;DLL_API int seedkey = 0;DLL_API int CalSeedKey1(int seed){seedkey = seed-1; return 0;}DLL_API int CalSeedKey2(int seed,int key){return seed+key;}DLL_API int CalSeedKey3(int seed, int key){key = seed + key;return 0;}秀丽的曲奇.h
#ifdef DLL_EXPORTS#define DLL_API __declspec(dllexport)#else#define DLL_API __declspec(dllimport)#endifextern DLL_API int seedkey;extern DLL_API int _key;DLL_API int CalSeedKey1(int seed);DLL_API int CalSeedKey2(int seed ,int key);DLL_API int CalSeedKey3(int seed ,int key);秀丽的曲奇入口函数dllmain.cpp:
#include <windows.h>BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, 不安的毛巾 lpReserved ){ switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE;}秀丽的曲奇CalSeedKey.def模块文件导出函数:
LIBRARYEXPORTSseedkey @1_key @2CalSeedKey1 @3CalSeedKey2 @4CalSeedKey3 @5
2、dll加载调用
主要是指针函数的使用,通过dll导出函数的地址。
typedef int(*CalseedKey)(int a);typedef int(大力的宝马)(int a,int b); typedef int(*_Key);秀丽的曲奇callDll.cpp:
#include <stdio.h>#include <windows.h>int main(){int a = 10, b = 10;HMODULE hDll = LoadLibrary("DLL.dll");if (hDll != NULL){CalseedKey seedKey = (CalseedKey) GetProcAddress(hDll, "CalSeedKey1");CalseedKey1 seedKey2 = (CalseedKey1)GetProcAddress(hDll, "CalSeedKey2");CalseedKey1 seedKey3 = (CalseedKey1)GetProcAddress(hDll, "CalSeedKey3");_Key seedkey = (_Key)GetProcAddress(hDll, "seedkey");seedKey(100);printf("nCalSeedKey:%d", *seedkey);printf("nCalSeedKeyReturn(%d,%d):%d",a,b, seedKey2(a, b));seedKey3(a, b);printf("nCalSeedKeyReturn(%d,%d):%d",a,b, b);}return 0;}测试: