




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
IntroductionI have seen quite a lot of code explaining how to use classes exported from a DLL in an application. However, all these describe the usage of the exported classes by linking implicitly to the DLL. Refreshing our DLL concepts, there are two ways for an application to use a function written in a DLL. The first way is to have your applications source code simply reference symbols contained in the DLL. This causes the loader to implicitly load (and link) the required DLL when the application is invoked. This is known as implicit linking.The second way is for the application to explicitly load the required DLL (using a LoadLibrary() call) and explicitly link to the desired exported symbol while the application is running. In other words, if the application decides that it wants to call a function in a DLL, it can explicitly load the DLL into the process address space, get the virtual memory address of the function contained within the DLL, and then call the function using this memory address. The beauty of this technique is that everything is done while the application is running and the application can unload the DLL from its process address space when it has finished its work with the DLL. As you might have guessed, this technique is known as explicit linking.BackgroundSo far, I spoke of using functions, but hey, what about using classes exported from a DLL? Well, in the case of implicitly linked DLLs, there is no difference at all. But what about loading DLLs explicitly and using the exported classes? Well, under normal circumstances, it cannot be done, but I wrote this article not to explain to you why it cannot be done, but to give you an idea as to how you can do it. Thats right! Using exported classes by loading a DLL using a LoadLibrary() call.But before proceeding further, I warn you that the method given below is sort of a hack, and if for any reason you plan to use it in your project, please take the prior approval of your boss . (if by any chance you do manage to get his/her approval on this technique!). However, this column is basically for your understanding and also for extreme cases when you just cant do without this hack.Using the codeIf you look at the sample code, you can see that I have created a Calculator DLL called Calc.DLL and I am using the calculating powers present in the DLL in my console application called UserOfCalc (I couldnt think of a better name!).Collapse/ Calc.DLL contains an exported class/ called CCalc that contains 3 methods called Add,Sub and GetLastFunc (). It is as follows:/ CALC.H - declares the CCalc class/ that is exported from the DLL/ and is imported in the EXE#include #ifdef CALC_EXPORTS#define CALC_API _declspec (dllexport)#else#define CALC_API _declspec (dllimport)#endif#define SOME_INSTN_BUF 260class CALC_API CCalcprivate:TCHAR m_szLastUsedFuncSOME_INSTN_BUF;public: CCalc (); int Add (int i, int j); int Sub (int i, int j); TCHAR* GetLastUsedFunc ();The implementation of this DLL is as shown in the file Calc.cpp:Collapse#include Calc.h#include BOOL APIENTRY DllMain (HANDLE, DWORD, LPVOID) return TRUE;/ Ctor, initializes the m_szLastFuncCalled arrayCCalc:CCalc () memset (m_szLastUsedFunc, 0, sizeof (m_szLastUsedFunc); strcpy (m_szLastUsedFunc, No function used yet);int CCalc:Add (int i, int j) strcpy (m_szLastUsedFunc, Add used); return (i + j);int CCalc:Sub (int i, int j) strcpy (m_szLastUsedFunc, Sub used); return (i - j);Now, how do we use the functions present in this Calc class by explicitly loading the DLL? The steps are as follows:1. The first step is to load the Calc.DLL library in your application using LoadLibrary. CollapseHMODULE hMod = LoadLibrary (Calc.dll);if (NULL = hMod) printf (LoadLibrary failedn); return 1;2. Since you have the header file of Calc.DLL, the next step is to allocate a block of memory that matches the class layout, and call your constructor code. CollapseCCalc *pCCalc = (CCalc *) malloc (sizeof (CCalc);if (NULL = pCCalc) printf (memory allocation failedn); return 1;But why in the C+ world are we using malloc instead of new! Because the new operator calls CCalcs constructor for which we dont have any access. Remember, we have to load the DLL dynamically and hence there is no definition of CCalcs constructor available to us at build time.Hence we just obtain an uninitialized block of memory whose size equals the CCalc class size.3. If you look up the exported functions in Dumpbin.exe (thats located under your Microsoft Visual StudioVC98Bin directory), and type dumpbin /exports, you will see a list of functions exported by the DLL. (By the way, I have used a DEF file to unmangle the mangled function names.) It is as shown in the figure. The list contains the virtual memory address of the functions Add, Sub, GetLastUsedFunc and the constructor.Since we obtained the block of memory, we have to call the constructor to initialize the block of memory. So, we get the relative virtual address of the constructor in the DLL.CollapsePCTOR pCtor = (PCTOR) GetProcAddress (hMod, CCalc);if (NULL = pCtor) printf (GetProcAddress failedn); return 1;PCTOR is a function pointer and is present at the top of UserOfCalc.cpp. It is defined as follows:Collapsetypedef void (WINAPI * PCTOR) ();4. Since we have the address of the constructor, we have to explicitly call it to initialize the block of memory obtained by malloc. Yes, but how do we associate an object for the constructor? If you remember, when any member function is called, including the constructor, the address of the object gets quietly passed to the called function and this address is stored in the stack. On an Intel based machine, this address of the object is pushed onto the stack via the ECX register. So, if you create a class and call its member function, the ECX register contains the this pointer. This screen shot should make things clearer.If you observe the disassembly window, just after the execution of the line: CollapseLEA ECX, EBP-4you will see that the contents of ECX and &bmw are the same. On a machine having a different processor architecture, it could be another register instead of ECX. We just have to figure that out.5. Coming back to our Calc.dll, since we already have the address of a block of memory (that will in the future be an object), we move this address into the ECX register by using the Visual C+ inline assembler syntax: Collapse_asm MOV ECX, pCCalc 6. Since we have already obtained the address of the constructor, we just say: CollapsepCtor ();7. When your function pointer pCtor() returns from the DLL, it would have initialized the object of the class contained in the DLL. Voila! 8. To call any other member function of the Calc class, once again move pCalc to ECX and obtain the proc address of the exported function and simply
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024成都医学院辅导员招聘笔试真题
- 2025年溶剂型色浆项目合作计划书
- 10的认识和加、减法第3课时 练一练 教案 2025人教版数学一年级上册
- 2024年南通市紫琅第一小学选聘教师真题
- 2025年柳州市公安机关招聘警务辅助人员考试试题【答案】
- 2025年内蒙古自治区司法厅下属事业单位招聘考试笔试试题【答案】
- 2025年TFT-LCD用偏光片项目建议书
- 吉林科技发展计划项目-吉林科技创新服务平台
- 2025年智能变电站自动化系统项目建议书
- 2025年航空用玻璃系列项目建议书
- 2022年湖南省事业编制招聘考试《计算机专业基础知识》真题试卷【1000题】
- 全自动量热仪说明书
- MT 194-1989煤矿用巷道支架试验方法与型式检验规范
- GB/T 6109.2-2008漆包圆绕组线第2部分:155级聚酯漆包铜圆线
- GB/T 5359.1-2019摩托车和轻便摩托车术语第1部分:车辆类型
- 中药学多选题含答案
- GB 11930-1989操作开放型放射性物质的辐射防护规定
- 起重作业吊索具使用安全培训课件
- 育婴员中级近年考试真题汇总(含答案)
- 顺德区国家工作人员因私出国(境)审批表
- 2022泉州实验中学初一新生入学考试语文卷
评论
0/150
提交评论