vs Dynamic Library Debugging

Source: Internet
Author: User

In the program development, often use to the dynamic library, the dynamic library cannot run directly, needs to depend on other program calls, then how should we debug our dynamic library? This article is illustrated by a simple example.

Use tool: VS2008

Languages used: C + +

Development steps:

1. Invoking the application of the Dynamic Library 1.1 New dialog box application
1.2 Designing a dynamic library interface This interface is used to display some basic information about a dynamic library when loading a dynamic library DllTestAppDlg.h
struct Tdll_information{lpctstrsdllname; LPCTSTRSFUNCDESCR; Lpctstrsauthorname;}; typedef BOOL (*lpfnregister_createobject) (void**); typedef bool (*lpfnregister_getdllinformation) (void**); typedef BOOL (*lpfnregister_releaseobject) (void*);
DllTestAppDlg.cpp
void Cdlltestappdlg::onbnclickedbtnload () {CString S1 = _t (""), s=_t (""); s = _t ("DLL files (*.dll) |*.dll| All Files (*. *) |*.*| |"); CFileDialog de (True, S1, NULL, Ofn_hidereadonly | Ofn_overwriteprompt | Ofn_nochangedir,s, NULL); if (DE. DoModal () = = IDOK) {m_sdllpathname = de. GetPathName ();//path and Filename}else{return;} if (M_sdllpathname.isempty ()) {M_btnrundll.enablewindow (FALSE);} Else{m_btnrundll.enablewindow (TRUE);} CString str = _t (""); int cnt = 0,i = 0;char p[256]={0}; Lpfnregister_getdllinformation Lpfnregister; HInstance Hindll = Null;hindll = LoadLibrary (m_sdllpathname); str = _t ("getdllinformation"); cnt = str. GetLength (); for (i=0; i<cnt; i++) {p[i] = str. GetAt (i);} Lpfnregister= (lpfnregister_getdllinformation) GetProcAddress (hindll,p); tdll_information* pInfo = NULL; (* Lpfnregister) ((void**) &pinfo); str = Pinfo->sdllname;m_sdllname = _t (""); cnt = str. GetLength (); for (i=0;i<cnt;i++) {m_sdllname = M_sdllname + str. GetAt (i);} str = PINFO-&GT;SFUNCDESCR;M_SFUNCDESCR = _t (""); cnt = str. Getlength (); for (i=0;i<cnt;i++) {M_SFUNCDESCR = M_sfuncdescr + str. GetAt (i);} str = Pinfo->sauthorname;m_sauthorname = _t (""); cnt = str. GetLength (); for (i=0;i<cnt;i++) {m_sauthorname = M_sauthorname + str. GetAt (i);} FreeLibrary (Hindll); UpdateData (0);}
Effect 1.3 Design Call Interface class

MyDllService.h

#pragma once#define err_ser_ok             0x0000#define err_ser_run_failed     0x0001#define err_ser_run_cancel     0x0002#define err_ser_load_param     0x0003class cmydllservice{public:cmydllservice (void) {};~CMyDllService (void) {};p ublic:virtual DWORD runservice () {return err_ser_run_failed;};};
Call

void Cdlltestappdlg::onbnclickedbtnrun () {if (M_sdllpathname.isempty ()) {MessageBox (_t ("DLL path is empty. "); return;} cmydllservice* pser = NULL; hinstance hin = Null;hin = LoadLibrary (M_sdllpathname), if (hIn! = invalid_handle_value) {}else{cstring info = _t ("load") + m_s Dllpathname + _t ("failure"); MessageBox (info); return;} CString str = _t (""); int cnt = 0,i = 0;char P[256]={0};str = _t ("createobject"); cnt = str. GetLength (); for (i=0; i<cnt; i++) {p[i] = str. GetAt (i);} Lpfnregister_createobject lpfnregister;lpfnregister= (Lpfnregister_createobject) GetProcAddress (hIn,p); bool Result = False;if (lpfnregister) {result = (*lpfnregister) ((void**) & (Pser));} Else{freelibrary (HIn); CString info = _t ("load") + M_sdllpathname + _t ("failed"); MessageBox (info); return;} if (pser && result) {DWORD dwerror = 0;dwerror = Pser->runservice ();} Else{freelibrary (HIn); CString info = _t ("Run") + M_sdllpathname + _t ("failed"); MessageBox (info); return;} Char p2[256]={0};str = _t ("Releaseobject"); cnt = str. GetLength (); for (i=0; i<cnt; i++) {P2[I] = str. GetAt (i);} Lpfnregister_releaseobject lpfnregister1;lpfnregister1= (Lpfnregister_releaseobject) GetProcAddress (HIN,P2); LpfnRegister1) {result = (*lpfnregister1) ((void*) pser);p ser = NULL;} FreeLibrary (hIn);}
Effect

1.4 File Structure diagram

2. dialog box Dynamic Library

2.1 New dialog box Dynamic Library

Set properties to use in a static library


2.2 Adding an interface class
2.3 Derive your own class from the interface class and re-MyDlgDllService.h the virtual function
#pragma once#include "mydllservice.h" class Cmydlgdllservice:public cmydllservice{public:cmydlgdllservice (void); ~ Cmydlgdllservice (void);D word runservice (void);};
MyDlgDllService.cpp
#include "StdAfx.h" #include "MyDlgDllService.h" #include "TestDlg.h" cmydlgdllservice::cmydlgdllservice (void) {} Cmydlgdllservice::~cmydlgdllservice (void) {}dword cmydlgdllservice::runservice (void) {Ctestdlg dlg;dlg. DoModal (); return ERR_SER_OK;}
2.4 Designing the Dynamic Library interface
struct Tdll_information{lpctstrsdllname; LPCTSTRSFUNCDESCR; Lpctstrsauthorname;}; extern "C" __declspec (dllexport) bool CreateObject (void** POBJ) {*pobj  = new Cmydlgdllservice;return true;} extern "C" __declspec (dllexport) bool Getdllinformation (void** pInfo) {*pinfo = new Tdll_information; ((tdll_information *) (*pinfo))->sdllname = _t ("DLL");((tdll_information*) (*pinfo))->SFUNCDESCR = _t ("DLL test");((Tdll_ information*) (*pinfo))->sauthorname = _t ("tester"); return true;} extern "C" __declspec (dllexport) bool Releaseobject (void* pObj) {cmydlgdllservice* ptempobj = Null;ptempobj = ( cmydlgdllservice*) Pobj;delete Ptempobj;return true;}
2.5 File Structure diagram

SOURCE download



vs Dynamic Library Debugging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.