• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Codejie's C++ Space

            Using C++

            練習(xí):LoadLibrary調(diào)用DLL中的Class

                使用LoadLibrary函數(shù)調(diào)用DLL中的函數(shù)的方法一般被稱為“顯式”調(diào)用,意義和使用lib的“隱式”調(diào)用相對應(yīng)。
                LoadLibrary調(diào)用DLL中的函數(shù)的方法比較簡單,通過GetProcAddress獲得函數(shù)的在DLL的地址就可以訪問了,但DLL中的Class訪問就相對很復(fù)雜了(目前我就發(fā)現(xiàn)這一種顯式調(diào)用方式,哪位有其他方法么?)。一個(gè)簡單的情況就是Class的函數(shù)在調(diào)用是,其名稱是什么?還有Class的contructor函數(shù)怎么調(diào)用?下面的代碼將演示下這些問題。
            這里是DLL的文件:
                DllMain.h

             1#ifndef __DLLMAIN_H__
             2#define __DLLMAIN_H__
             3
             4#include <string>
             5
             6#define DllExport __declspec(dllexport)
             7
             8extern "C" int DllExport Func(int x);
             9
            10extern "C" class DllExport CA
            11{
            12public:
            13    CA(int x);
            14    ~CA();
            15
            16    int Func0();
            17    int Func(int x);
            18    const std::string& FuncS(int x, const std::string& str) const;
            19protected:
            20    int _x;
            21}
            ;
            22
            23
            24#endif

                DllMain.cpp
             1#include <iostream>
             2
             3#include "DllMain.h"
             4
             5int Func(int x)
             6{
             7    return x * 10;
             8}

             9
            10CA::CA(int x)
            11   : _x(x)
            12{
            13    std::cout << "contructor" << std::endl;
            14}

            15
            16CA::~CA()
            17{
            18    std::cout << "destructor" << std::endl;
            19}

            20
            21int CA::Func0()
            22{
            23    return _x;
            24}

            25
            26int CA::Func(int x)
            27{
            28    return _x * x;
            29}

            30
            31const std::string& CA::FuncS(int x, const std::string &str) const
            32{
            33    return str;
            34}

            35

                這里需要.def文件了,因?yàn)镃lass在DLL中的命名不像函數(shù)命名那么簡單,會被轉(zhuǎn)義的,像CA::Func(int)在DLL的export表中就是?Func@CA@@QAEHH@Z,具體定義說明可參看《xxx的自我修養(yǎng)》一書。因此,這里需要使用.def文件對函數(shù)進(jìn)行重命名,下面是DllMain.def文件內(nèi)容:
             
            1LIBRARY TESTDLL
            2EXPORTS
            3    Func = Func
            4    CA::CA(int= ??0CA@@QAE@H@Z
            5    CA::~CA = ??1CA@@QAE@XZ
            6    CA::Func0 = ?Func0@CA@@QAEHXZ
            7    CA::Func(int= ?Func@CA@@QAEHH@Z
            8    ;CA::FuncS(int,std::basic_string<char>&= ?FuncS@CA@@QBEABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HABV23@@Z
            9    CA::FuncS = ?FuncS@CA@@QBEABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HABV23@@Z


                多說一句,這里.def的編寫很需要Depends(Dependency Walker)工具的支持,其是查看DLL的首選工具啊。。


                編譯DLL,用下面代碼進(jìn)行測試:
                LoadLib.cpp
             1#include <iostream>
             2#include <string>
             3
             4#include <windows.h>
             5
             6//#include "DllMain.h"
             7
             8#define DllExport __declspec(dllexport)
             9
            10extern "C" int DllExport Func(int x);
            11
            12extern "C" class DllExport CA
            13{
            14public:
            15    CA(int x);
            16    ~CA();
            17
            18    int Func0();
            19    int Func(int x);
            20    const std::string& FuncS(int x, const std::string& str) const;
            21
            22private:
            23    int _x;
            24}
            ;
            25
            26typedef int (*func)(int);
            27typedef void (WINAPI *PCTOR)(int);
            28typedef int (WINAPI *func0)(void);
            29typedef int (WINAPI *funcc)(int);
            30typedef const std::string& (WINAPI *funcs)(int,const std::string&);
            31typedef void (WINAPI *PDTOR)(void);
            32
            33int main()
            34{
            35    HINSTANCE hdll;
            36    hdll = LoadLibraryA(("../DLLTEST/Debug/DLLTEST.dll"));
            37    if(hdll != NULL)
            38    {
            39        func pf = (func)GetProcAddress(hdll, "Func");
            40        std::cout << pf(10<< std::endl;
            41        CA* a = (CA*)malloc(sizeof(CA));
            42        PCTOR pc = (PCTOR)GetProcAddress(hdll, "CA::CA(int)");
            43        _asm { MOV ECX, a } 
            44        pc(5);
            45        func0 pf0 = (func0)GetProcAddress(hdll, "CA::Func0");
            46        _asm {MOV ECX, a }
            47        std::cout << pf0() << std::endl;
            48        funcc pfc = (funcc)GetProcAddress(hdll, "CA::Func(int)");
            49        _asm { MOV ECX, a }
            50        std::cout << pfc(10<< std::endl;
            51        funcs pfs = (funcs)GetProcAddress(hdll, "CA::FuncS");
            52        _asm { MOV ECX, a }
            53        std::cout << pfs(0, std::string("hello world")) << std::endl;
            54        PDTOR pd = (PDTOR)GetProcAddress(hdll, "CA::~CA");
            55        _asm { MOV ECX, a } 
            56        pd();        
            57        free(a);    
            58    }

            59    FreeLibrary(hdll);
            60    
            61    return 0;
            62}


                結(jié)果還算正常:
            1100
            2contructor
            35
            450
            5hello world
            6destructor
            7


                上面的代碼基本演示了DLL中Class的簡單使用,包括對contructor、destrunctor的調(diào)用,有參、無參、多參函數(shù)調(diào)用,不知道有啥缺陷,但至少Work了,嘿嘿~
                由上述代碼可以看出,這種“顯式”使用DLL中的Class是非常繁瑣和危險(xiǎn)的事情,因此我覺得能用“隱式”就不要用“顯式”,能靜態(tài)就不要用動態(tài)。。。
                注意到?jīng)],代碼沒有演示繼承和虛函數(shù),那是因此我加入Virtual函數(shù),程序就會core,實(shí)在搞不定,這里也就沒法給出好的方案來,不知道哪位有啥建議么。。。
                上面代碼參考了如下地址:
                      http://www.codeproject.com/dll/classesexportedusingLL.asp
                      http://blog.csdn.net/jdcb2001/archive/2006/11/21/1401569.aspx

            posted on 2009-09-24 17:50 codejie 閱讀(25434) 評論(8)  編輯 收藏 引用 所屬分類: C++

            評論

            # re: 練習(xí):LoadLibrary調(diào)用DLL中的Class[未登錄] 2009-09-25 01:17 Liu

            最近很高產(chǎn)么,看來在M記沒事可干阿

            繼續(xù)寫,我也學(xué)習(xí)學(xué)習(xí)  回復(fù)  更多評論   

            # re: 練習(xí):LoadLibrary調(diào)用DLL中的Class 2009-09-25 09:59 陳梓瀚(vczh)

            鑒于復(fù)雜參數(shù)的計(jì)算也有可能會毀掉ecx,而且你又不能再參數(shù)放進(jìn)去之后再修改ecx,所以你還是老老實(shí)實(shí)import一個(gè)完整的類吧……  回復(fù)  更多評論   

            # re: 練習(xí):LoadLibrary調(diào)用DLL中的Class 2009-09-25 11:43 codejie

            @Liu
            M記現(xiàn)在還沒有想起讓我做什么,我也是吃了空,想到啥就寫點(diǎn)啥,沒目的,估計(jì)也‘產(chǎn)’不出什么。。。別學(xué)了,省的耽誤了你老人家~
            另,我在看你A國的代碼,那叫一個(gè)慘啊,怎么也想不明白,號稱‘技術(shù)領(lǐng)先’的A國人,怎么會寫出這樣的代碼。。。  回復(fù)  更多評論   

            # re: 練習(xí):LoadLibrary調(diào)用DLL中的Class 2009-09-25 11:46 codejie

            @陳梓瀚(vczh)
            我也知道這種方式不好,至少virtual我還沒有搞定。你說的使用import方式該如何做?能給給實(shí)例不?我現(xiàn)在想解決的問題就是,如果只有DLL,沒有l(wèi)ib的情況下,如何使用‘顯式’方式訪問DLL中的Class。  回復(fù)  更多評論   

            # re: 練習(xí):LoadLibrary調(diào)用DLL中的Class 2009-09-25 15:47 溪流

            @codejie

            只有 DLL(如果不知道函數(shù)名,即不給出 .def),同樣不能“顯式”調(diào)用 DLL 中的函數(shù),不是嗎?
            不過同樣可以用你上面的工具找出函數(shù)名稱,然后 GetProcAddress,然后使用……  回復(fù)  更多評論   

            # re: 練習(xí):LoadLibrary調(diào)用DLL中的Class 2009-09-25 17:10 codejie

            @溪流
            不知道exports函數(shù)名的情況下使用DLL是一種比較極端的情況,不光是函數(shù)名稱不知道,函數(shù)的參數(shù)更是一個(gè)問題,.def中多數(shù)不說明參數(shù)的。
            這里使用LoadLibrary的‘顯式’調(diào)用,一般應(yīng)該多數(shù)用于實(shí)時(shí)加載DLL,而不是程序啟動就加載DLL,這樣作便于管理程序的內(nèi)存使用情況,記得在早期的windows16位機(jī)器上常用,還有就是利用DLL特性做plugins的程序中。
            這里的代碼只是我吃了空而突發(fā)奇想的,跟你說的一樣,現(xiàn)實(shí)中應(yīng)該很少用的。  回復(fù)  更多評論   

            # re: 練習(xí):LoadLibrary調(diào)用DLL中的Class 2012-07-17 09:14 Richard Wei

            如果要用顯試調(diào)用類,建議還是用接口方式吧,導(dǎo)出一個(gè)CreateInstance(interface** ppInterface)就可以了,簡單又方便,具體參考COM  回復(fù)  更多評論   

            # re: 練習(xí):LoadLibrary調(diào)用DLL中的Class 2012-07-17 09:43 codejie

            @Richard Wei
            神奇的COM, 龐大的COM, 恐怖的COM。。。  回復(fù)  更多評論   

            公告

            Using C++

            導(dǎo)航

            統(tǒng)計(jì)

            留言簿(73)

            隨筆分類(513)

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            久久久免费观成人影院| 久久国产精品99国产精| 少妇无套内谢久久久久| 一本久道久久综合狠狠爱| 国产精品久久久久久搜索| 久久综合给合综合久久| 久久精品国产亚洲AV无码偷窥| 亚洲国产精品一区二区久久| 久久笫一福利免费导航| 97超级碰碰碰久久久久| 午夜精品久久久久9999高清| 久久精品国产亚洲av麻豆色欲| 一本久久久久久久| 久久精品国产男包| 精品乱码久久久久久夜夜嗨| 婷婷伊人久久大香线蕉AV| 久久精品国产99久久久香蕉| 91久久精品91久久性色| 精品多毛少妇人妻AV免费久久| 大美女久久久久久j久久| 欧美黑人激情性久久| 日本精品久久久久久久久免费| 97久久精品无码一区二区天美 | 日产精品久久久久久久性色| 国产午夜精品久久久久九九电影| 日韩精品久久无码中文字幕| 久久亚洲AV成人无码软件| 久久国产高清一区二区三区| 国产产无码乱码精品久久鸭 | 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 国产亚洲欧美成人久久片| 久久99这里只有精品国产| 久久精品国产亚洲7777| 国产精品久久久久久久午夜片| 精品熟女少妇av免费久久| 亚洲午夜无码久久久久| 久久亚洲精品成人无码网站| 伊人伊成久久人综合网777| 日产久久强奸免费的看| 久久亚洲av无码精品浪潮| 四虎影视久久久免费|