您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 市场营销 > VC++(dll)编程
VC++(DLL)VC++(DLL)e-mail21cnbao@21cn.comPconlineDLLDLLDLLWINRARVC++6.0DLLCC++MFC1234MFCDLL5MFCDLL6MFCDLLVC++(DLL)1.DLL(DynamicLinkableLibrary)DLLlibEXEDLLDLLEXEEXEEXEDLL1DLLDLLDLLWindowsDLLWindowsAPIVisualBasicVisualC++Delphi2Windowssystem32kernel32.dlluser32.dllgdi32.dllwindowsAPIDLLkernel32.dlluser32.dllgdi32.dllMessageBoxuser32.dllDLL(3)VCVisualC++DLLNon-MFCDLLMFCMFCRegularDLLMFCDLLMFCExtensionDLLMFCDLLMFCMFCCMFCMFCMFCDLLCWinAppMFCDLLMFCMFCVC++(DLL)2DLL11VC++6.0newlibTeststaticlibrarylibTest.ziplib.hlib.cpplib.hlib.cpp#includestdio.h#include..\lib.h#pragmacomment(lib,..\\debug\\libTest.lib)//intmain(intargc,char*argv[]){printf(2+3=%d,add(2,3));}.libadd.libaddTurboC2.0CscanfprintfmemcpystrcpyVC++(DLL)3DLLF5debugCTRL+F53EXE3VCF112libTestlibCall44VisualC++DependsVC++(DLL)Dependsuser32.dllMessageBox5DependsDLLDependsDLLDLLDLLMFCDLLVC++(DLL)4.MFCDLL4.1DLL2addadd6VC++newWin32Dynamic-LinkLibrarydllTestMFCAppWizard(dll)MFCAppWizard(dll)56MFC6MFCDLLlib.hlib.cpp/*lib.h*/#ifndefLIB_H#defineLIB_HexternCint__declspec(dllexport)add(intx,inty);#endif/*lib.cpp*/VC++(DLL)#includelib.hintadd(intx,inty){returnx+y;}2DLLdllCallDLLadd#includestdio.h#includewindows.htypedefint(*lpAddFun)(int,int);//intmain(intargc,char*argv[]){HINSTANCEhDll;//DLLlpAddFunaddFun;//hDll=LoadLibrary(..\\Debug\\dllTest.dll);if(hDll!=NULL){addFun=(lpAddFun)GetProcAddress(hDll,add);if(addFun!=NULL){intresult=addFun(2,3);printf(%d,result);VC++(DLL)}FreeLibrary(hDll);}return0;}dllTestlib.cpp2lib.hadd__declspec(dllexport)addDLLDLL(1)DLL(2)DLLDLLDLL2typedefint(*lpAddFun)(int,int)addmainlpAddFunaddFunmainDLLHINSTANCEhDllWin32ApiLoadLibraryDLLDLLhDllmainWin32ApiGetProcAddressDLLaddaddFunaddFunDLLaddDLLmainWin32ApiFreeLibraryDLLDLL(1)DLL(2)DLLVC++(DLL)4.2DLL4.1__declspec(dllexport)(.def).def.defaddDLLdllTestlib.def;lib.def:DLLLIBRARYdllTestEXPORTSadd@1.def(1)LIBRARY.defDLL(2)EXPORTS.def@nn(3).def(;)lib.defdllTestaddadd14.3DLL4.1LoadLibrary-GetProcAddress-FreeLibraryApiDLL-DLL-DLLDLLAPIDLLDLLDLLIPDHCPDLLVC++(DLL)DLLDLLDLLDLLDLLWindowsDLL1DLLdllTest.lib.dlldllCalldllCall#pragmacomment(lib,dllTest.lib)//.libDLLexternC__declspec(dllimport)add(intx,inty);intmain(intargc,char*argv[]){intresult=add(2,3);printf(%d,result);return0;}(1)DLL.lib#pragmacomment(lib,dllTest.lib)DLL.libDLL.libDLL(2)externC__declspec(dllimport)add(intx,inty)__declspec(dllimport)APIDLLDLLVC++(DLL).libEXE.libDLLEXEDLLWindowsDLLDLLEXEDLL4.4DllMainWindowsDLLDOSmainWIN32WinMainDLLDllMainDLLWindowsDllMainDllMainDLLDllMainWindowsDLLDllMainDLLDLLDLLDllMainDllMainDllMainBOOLAPIENTRYDllMain(HANDLEhModule,DWORDul_reason_for_call,LPVOIDlpReserved){switch(ul_reason_for_call){caseDLL_PROCESS_ATTACH:printf(\nprocessattachofdll);break;caseDLL_THREAD_ATTACH:printf(\nthreadattachofdll);VC++(DLL)break;caseDLL_THREAD_DETACH:printf(\nthreaddetachofdll);break;caseDLL_PROCESS_DETACH:printf(\nprocessdetachofdll);break;}returnTRUE;}DllMainDLLDLLMainul_reason_for_call4PROCESS_ATTACHPROCESS_DETACHTHREAD_ATTACHTHREAD_DETACHswitchDllMainBOOLAPIENTRYDllMain(HANDLEhModule,WORDul_reason_for_call,LPVOIDlpReserved)APIENTRY__stdcallPascalWINAPIDLL32HINSTANCEDLLWin32HINSTANCEHMODULEhModulehDll=LoadLibrary(..\\Debug\\dllTest.dll);if(hDll!=NULL){VC++(DLL)addFun=(lpAddFun)GetProcAddress(hDll,MAKEINTRESOURCE(1));//MAKEINTRESOURCEif(addFun!=NULL){intresult=addFun(2,3);printf(\ncalladdindll:%d,result);}FreeLibrary(hDll);}processattachofdllcalladdindll:5processdetachofdllDllMainGetProcAddress(hDll,MAKEINTRESOURCE(1)).defaddaddMAKEINTRESOURCE(1)MAKEINTRESOURCEwinuser.h#defineMAKEINTRESOURCEA(i)(LPSTR)((DWORD)((WORD)(i)))#defineMAKEINTRESOURCEW(i)(LPWSTR)((DWORD)((WORD)(i)))#ifdefUNICODE#defineMAKEINTRESOURCEMAKEINTRESOURCEW#else#defineMAKEINTRESOURCEMAKEINTRESOURCEAVC++(DLL)4.5__stdcallVC++DLL__stdcallWINAPIC/C++__cdecl__stdcall__cdeclC(C++externC)__stdcall@_functionname@number__cdecl_functionnameWindows__stdcall__cdeclwindef.h#defineCALLBACK__stdcall//#defineWINAPI__stdcall//WINAPI#defineWINAPIV__cdecl#defineAPIENTRYWINAPI//DllMain#defineAPIPRIVATE__stdcall#definePASCAL__stdcalllib.haddint__stdcalladd(intx,inty);typedefint(__stdcall*lpAddFun)(int,int);lib.h__stdcalltypedefint(*lpAddFun)(int,int)__cdecl7VC++(DLL)78Thisisusuallyaresultof__stdcall4.6DLLDLLDLLDLL/*lib.h*/#ifndefLIB_H#defineLIB_HexternintdllGlobalVar;#endif/*lib.cpp*/#includelib.h#includewindows.hintdllGlobalVar;VC++(DLL)BOOLAPIENTRYDllMain(HANDLEhModule,DWORDul_reason_for_call,LPVOIDlpReserved){switch(ul_reason_for_call){caseDLL_PROCESS_ATTACH:dllGlobalVar=100;//dll100break;caseDLL_THREAD_ATTACH:caseDLL_THREAD_DETACH:caseDLL_PROCESS_DETACH:break;}returnTRUE;};lib.def;DLLLIBRARYdllTestEXPORTSdllGlobalVarCONSTANT;dllGlobalVarDATAGetGlobalVarVC++(DLL)lib.hlib.cppDLL.defEXPORTSCONSTANT//DATA//VC++DLL#includestdio.h#pragmacomment(lib,dllTest.lib)externintdllGlobalVar;intmain(intargc,char*argv[]){printf(%d,*(int*)dllGlobalVar);*(int*)dllGlobalVar=1;printf(%d,*(int*)dllGlobalVar);return0;}externintdllGlobalVarDLLDLL*(int*)dllGlobalVarDLLdllGlobalVar=1;VC++(DLL)dllGlo
本文标题:VC++(dll)编程
链接地址:https://www.777doc.com/doc-5100878 .html