赞
踩
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)
- #endif
-
- extern 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,
- LPVOID 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模块文件导出函数:
- LIBRARY
- EXPORTS
- seedkey @1
- _key @2
- CalSeedKey1 @3
- CalSeedKey2 @4
- CalSeedKey3 @5
生成dll:
2、dll加载调用
主要是指针函数的使用,通过dll导出函数的地址。
- typedef int(*CalseedKey)(int a);
- typedef int(*CalseedKey1)(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;
- }
测试:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。