资源描述:
《Coperator整理_棉猴论坛VIP之DLL程序编写系列教程_笔记》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、第1课DLL基础一、静态链接库(StaticLinkLibrary)程序员们把常用的代码集合放进独立的文件里,这样的文件就叫做库。在写程序的时候,把这个库文件加入编译器,就能够使用这个库包含的所有功能而不必自己再去写一大堆代码。但是这种方法会把库里所有的东西都包含进去,造成程序体积的增大。制作静态链接库StaticLibTestStdAfx.h//stdafx.h:includefileforstandardsystemincludefiles,//orprojectspecificincludefilesthatareusedfrequ
2、ently,but//arechangedinfrequently//#if!defined(AFX_STDAFX_H__796D49AA_9488_4F2F_AEB1_C8F28E516D92__INCLUDED_)#defineAFX_STDAFX_H__796D49AA_9488_4F2F_AEB1_C8F28E516D92__INCLUDED_#if_MSC_VER>1000#pragmaonce#endif//_MSC_VER>1000#defineWIN32_LEAN_AND_MEAN//Excluderarely-useds
3、tufffromWindowsheadersextern"C"voidShowInfo();//TODO:referenceadditionalheadersyourprogramrequireshere//{{AFX_INSERT_LOCATION}}//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.#endif//!defined(AFX_STDAFX_H__796D49AA_9488_4F2F_AEB1_C8F28
4、E516D92__INCLUDED_)StdAfx.cpp//stdafx.cpp:sourcefilethatincludesjustthestandardincludes//01_001.pchwillbethepre-compiledheader//stdafx.objwillcontainthepre-compiledtypeinformation#include"stdafx.h"#include//Message()voidShowInfo(){MessageBox(NULL,"ShowInfo()tes
5、t","MQ",MB_OK);}调用静态链接库ExecuteStaticLib.cpp//ExecuteStaticLib.cpp:Definestheentrypointfortheapplication.//#include"stdafx.h"#pragmacomment(lib,"StaticLibTest.lib")extern"C"voidShowInfo();intAPIENTRYWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,LPSTRlpCmdLine,intnCmdSh
6、ow){//TODO:Placecodehere.ShowInfo();return0;}二、动态链接库DynamicLinkLibrary(DLL)DLL的格式和EXE文件是一样的,但是不能直接执行。它把代码封装到自己的内部,只是提供函数接口让外面的EXE程序调用。在编译的时候不会将所包含的动态链接库编译到程序中。制作静态链接库DynamicLibTestDynamicLibTest.cpp//DynamicLibTest.cpp:DefinestheentrypointfortheDLLapplication.//#include"s
7、tdafx.h"BOOLAPIENTRYDllMain(HANDLEhModule,DWORDul_reason_for_call,LPVOIDlpReserved){returnTRUE;}__declspec(dllexport)voidShowInfo(){MessageBox(NULL,"DynammicLib","MQ",MB_OK);}调用动态链接库ExecuteDynamicLib.cpp//ExecuteDynamicLib.cpp:Definestheentrypointfortheconsoleapplication.
8、//#include"stdafx.h"#pragmacomment(lib,"DynamicLibTest.lib")voidShowInfo();intmain(intargc,char*