欢迎来到天天文库
浏览记录
ID:27239897
大小:436.50 KB
页数:12页
时间:2018-12-02
《一步一步教你用VC和VB调用C++ DLL.doc》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、一步一步教你用VC和VB调用C++DLL从VC++应用程序调用C++DLL的函数VisualStudio6使创建包含函数或类的动态连接库(DLL)变得非常容易.第一步打开VisualStudio然后选择File
2、New菜单项:选择Win32DynamicLinkLibrary,输入工程名,敲OK.选择ADLLthatexportssomesymbols并单击Finish.在FileView里你会看到如下的工程文件:第二步在Test.cpp里,你将看到如下代码://Test.cpp:DefinestheentrypointfortheDLLapp
3、lication.//#include"stdafx.h"#include"Test.h"BOOLAPIENTRYDllMain(HANDLEhModule, DWORD ul_reason_for_call, LPVOIDlpReserved){ switch(ul_reason_for_call) { caseDLL_PROCESS_ATTACH: caseDLL_THREAD_ATTACH: case
4、DLL_THREAD_DETACH: caseDLL_PROCESS_DETACH: break; } returnTRUE;}//ThisisanexampleofanexportedvariableTEST_APIintnTest=0;//Thisisanexampleofanexportedfunction.TEST_APIintfnTest(void){ return42;}//Thisistheconstructorofaclassthathasbeenexported.//see
5、Test.hfortheclassdefinitionCTest::CTest(){ return;}Test.cpp包含了fnTest和CTest::CTest.如果你现在编译Test.dll,你将会得到一个可以被其他VC++应用程序直接调用的DLL.允许其他VC++程序调用的关键机制?(keymechanism)就包含在Test.h中://Thefollowingifdefblockisthestandardwayofcreatingmacros//whichmakeexportingfromaDLLsimpler.Allfilesw
6、ithinthisDLL//arecompiledwiththeTEST_EXPORTSsymboldefinedonthecommandline.//ThissymbolshouldnotbedefinedonanyprojectthatusesthisDLL.//Thiswayanyotherprojectwhosesourcefilesincludethisfilesee//TEST_APIfunctionsasbeingimportedfromaDLL,whereasthisDLL//seessymbolsdefinedwiththis
7、macroasbeingexported.#ifdefTEST_EXPORTS#defineTEST_API__declspec(dllexport)#else#defineTEST_API__declspec(dllimport)#endif//ThisclassisexportedfromtheTest.dll共4页 第1页 第2页 第3页 第4页classTEST_APICTest{public: CTest(void); //TODO:addyourmethodshere.};externTEST_APIintnTest;
8、TEST_APIintfnTest(void);这里面发生了什么?#ifdefTEST_EXPORTS是什么意思?TEST_EXPORTS又是在哪定义的?TEST_EXPORTS如果被定义,那么TEST_API将会被定义为__declspec(dllexport)(DLL导出),否则,将会被定义为__declspec(dllimport)(DLL导入).这将影响到后边定义的Ctest类是导出类还是导入类.这意味着如果我们需要导出的时候,我们就得定义TEST_EXPORTS.当一个VC++应用程序要访问这个DLL的时候,可以将Test.lib链接
9、进去,它包含了DLL的导出符号.第三步TEST_EXPORTS在哪里被定义了呢?DLLwizard干了一件我讨厌的事,它把TEST_EXPORTS放到
此文档下载收益归作者所有