标签:
创建dll工程:
1 #include"CustomDll.h" 2 #include<windows.h> 3 BOOL APIENTRY DLLMAIN(HMODULE hModule, DWORD reason_for_call, LPVOID lpReserved) 4 { 5 switch (reason_for_call) 6 { 7 case DLL_PROCESS_ATTACH: 8 case DLL_PROCESS_DETACH: 9 case DLL_THREAD_ATTACH: 10 case DLL_THREAD_DETACH: 11 break; 12 } 13 return TRUE; 14 } 15 int DLLFuncAdd(int a, int b) 16 { 17 return a + b; 18 }
创建useDll工程,隐式动态链接:将dll工程编译好后的复制到useDll工程根目录下,然后使用如下代码
1 #include<windows.h> 2 #include"CustomDll.h" 3 #include<iostream> 4 using namespace std; 5 #pragma comment (lib,"CustomDll") 6 int main() 7 { 8 int c = 1, b = 2; 9 cout << DLLFuncAdd(c, b) << endl; 10 return 0; 11 }
显示动态链接:在原dll工程中点击项目,添加新项,选中模块定义文件(.def),然后名称输入dll,确定
在dll.def文件中添加如下代码:
1 EXPORTS 2 DLLFuncAdd
然后在usedll工程中进行编辑,代码如下:
1 #include<windows.h> 2 #include<iostream> 3 using namespace std; 4 typedef int(*PFNEXPORTFUNC)(int a, int b); 5 int main() 6 { 7 int c = 1, b = 2; 8 HMODULE hModule = LoadLibrary(L"CustomDll.dll"); 9 if (hModule != NULL) 10 { 11 PFNEXPORTFUNC mDLLFuncAdd = (PFNEXPORTFUNC)GetProcAddress(hModule, "DLLFuncAdd"); 12 if (mDLLFuncAdd != NULL) 13 { 14 cout << mDLLFuncAdd(c, b) << endl; 15 } 16 FreeLibrary(hModule); 17 } 18 19 return 0; 20 }
就行了。
标签:
原文地址:http://www.cnblogs.com/newworldcom/p/4332845.html