• <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>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計模式} {C#基礎(chǔ)}

            C++ Dll

            前言:為了介紹C#寫界面,C++寫算法的快捷開發(fā)方式,C#與C++的交互,首先介紹c++,C#內(nèi)部的DLL,COM調(diào)用。

            一, 靜態(tài)的Lib:靜態(tài)的lib經(jīng)過編譯后只有.h和.lib文件,沒有dll,因?yàn)閷?shí)現(xiàn)部分也包含在lib中,這就是與動態(tài)dll的區(qū)別。還有在寫靜態(tài)lib的時候不需要在使用導(dǎo)出關(guān)鍵字_declspec(dllexport)。一般有2中方法調(diào)用靜態(tài)lib,如下實(shí)例:

            靜態(tài)lib:CPPLib->test.h

            #pragma once

            class CTest
            {
            public:
                CTest(void);
            public:
                ~CTest(void);
            public:
                
            int Add(int x, int y);
                
            int Square(int x);
            };

            //函數(shù)的實(shí)現(xiàn)必須寫在.cpp文件中,否則編譯有錯,說重復(fù)定義
            int Max(int x, int y);

            靜態(tài)lib的實(shí)現(xiàn)文件: CPPLib->test.cpp

            #include "StdAfx.h"
            #include 
            "Test.h"

            CTest::CTest(void)
            {
            }

            CTest::~CTest(void)
            {
            }

            int CTest::Add(int x, int y)
            {
             return x
            +y;
            }
            int CTest::Square(int x)
            {
                return x
            *x;
            }

            int Max(int x, int y)
            {
                return x;
            }


            client調(diào)用CPPLibClient->CPPibClient.cpp

            #include "stdafx.h"
            //#include "test.h"
            //#include <windows.h>
            //#include <string>
            //#include <assert.h>


            //#include "../CppLib/test.h" 
            //#pragma comment(lib,"../debug/CppLib.lib")
            //#pragma 使用法


            //#include "test.h"
            //修改編譯選項(xiàng)調(diào)用靜態(tài)庫
            //需要修改:include的path,lib的path,和加入lib的名字,如下:
            //C++->General->additional include directories
            //Linker->General->additional library directories
            //linker->input->additional dependencies

            //不能動態(tài)加載靜態(tài)的lib庫
            //HMODULE m_handle = LoadLibrary(L"../debug/CppLib.Lib");
            //GetProcAddress(m_handle, "Max");
            //FreeLibrary(m_handle);

            int _tmain(int argc, _TCHAR* argv[])
            {
                CTest test;
                
            int a = test.Add(10,20);
                printf(
            "the result is :%d\n",a);
                a 
            = Max(10,20);
                printf(
            "the result is :%d\n",a);
                return 
            0;
                
            }


            調(diào)用方法:可以看出對于靜態(tài)的只可以使用修改編譯選項(xiàng)和pragma comment()來調(diào)用,不能使用loadlibrary()來調(diào)用。

            二 ,動態(tài)DLL:在動態(tài)dll中,可以導(dǎo)出變量,函數(shù)和整個類。編譯后有.h,.lib和dll文件,真正的實(shí)現(xiàn)包含在dll中,所以在client調(diào)用動態(tài)dll的時候,必須要使用dll,最后和client的放在同意目錄下。要導(dǎo)出必須使用導(dǎo)出關(guān)鍵字_declspec(dllexport)。有時還使用extern “C”,為了使導(dǎo)出能夠與C兼容,一般我們都加extern “c”。
            一般調(diào)用有3中方法,實(shí)例如下:

            實(shí)例1:演示了導(dǎo)出變量和函數(shù),和前2中調(diào)用方法,修改編譯選項(xiàng)和pragma comment().
            動態(tài)dll:CPPdll->test.h

            #pragma once


            extern 
            "C" _declspec(dllexport) int nCppDll;
            extern 
            "C" _declspec(dllexport) int fnCppDll(void);

            extern 
            "C" _declspec(dllexport) int Max(int a, int b);
            extern 
            "C" _declspec(dllexport) int Min(int a, int b);


            動態(tài)dll的實(shí)現(xiàn):CPPDLL->test.cpp

            #include "StdAfx.h"
            #include 
            "Test.h"


            // This is an example of an exported variable
             
            int nCppDll=100;

            // This is an example of an exported function.
            int fnCppDll(void)
            {
                return 
            42;
            }

            int Max(int a, int b)
            {
            if(a>=b)return a;
            else
            return b;
            }
            int Min(int a, int b)
            {
            if(a>=b)return b;
            else
            return a;


            client的調(diào)用:cppclient->cppclient.cpp

            #include "stdafx.h"

            #pragma comment(lib, 
            "../debug/CppDll.lib")
            extern 
            "C"  int Max(int a,int b);//_declspec(dllimport)
            extern 
            "C" int Min(int a,int b); //_declspec(dllimport)
            extern 
            "C" _declspec(dllimport) int nCppDll;
            extern 
            "C" int fnCppDll(void);

            //#include "test.h"
            //修改編譯選項(xiàng)調(diào)用靜態(tài)庫
            //需要修改:include的path,lib的path,和加入lib的名字,如下:
            //C++->General->additional include directories
            //Linker->General->additional library directories
            //linker->input->additional dependencies

            int _tmain(int argc, _TCHAR* argv[])
            {
                
            int a;
                a  
            =Min(8,10);
                printf(
            "比較的結(jié)果為 %d\n",a);
                a
            = Max(8,10);
                printf(
            "比較的結(jié)果為%d\n",a);
                
                printf(
            "導(dǎo)出的變量:%d\n",nCppDll);

                a 
            = fnCppDll();
                printf(
            "fnCppDll的結(jié)果:%d\n",a);    

                return 
            0;
            }

            上面演示了對一般變量和函數(shù)的導(dǎo)出的調(diào)用方法中的其中的2中,修改編譯選項(xiàng)和pragma comment(),當(dāng)使用pragma comment()的使用,應(yīng)當(dāng)注意:
            使用#pragma隱式加載動態(tài)庫
            對于變量,必須申明且不能include頭文件。extern "C" _declspec(dllimport) int nCppDll;
            對于函數(shù),或include頭文件,或是申明。extern "C" int fnCppDll(void);
            對于類,最好使用函數(shù)封裝導(dǎo)出指針供使用。
            參考:http://m.shnenglu.com/mzty/archive/2006/07/24/10419.html

            實(shí)例2:演示類的導(dǎo)出和使用動態(tài)加載來調(diào)用。

            動態(tài)dll的類導(dǎo)出:CPPDll2->test.h

            #pragma  once
            //#include "boost/shared_ptr.hpp"

            class Test 
            {
            public:
             virtual ~Test() {}
             virtual void DoIt() 
            =0;
            };

            //extern "C" _declspec(dllexport)  std::auto_ptr<Test> CreateTest();
            //extern "C" _declspec(dllexport) boost::shared_ptr<Test> CreateTest();
            extern 
            "C" _declspec(dllexport) Test* CreateTestPtr();

            extern 
            "C" _declspec(dllexport) void DeleteTestPtr(Test*);


            動態(tài)dll的類導(dǎo)出的實(shí)現(xiàn):CPPDll2->test.cpp

            //test.cpp
            #include 
            "stdafx.h"
            #include 
            "Test.h"
            #include 
            <stdio.h>
            //#include <memory>
            //#include "boost/shared_ptr.hpp"


            class CTest : 
            public Test
            {
            public:
                virtual void DoIt()
               { printf(
            "Should do something\n"); }
            };

            //std::auto_ptr<Test> CreateTest()
            //{
            //    return std::auto_ptr<Test>(new CTest);
            //}


            //boost::shared_ptr<Test> CreateTest() 
            //
            //    return boost::shared_ptr<Test>(new CTest);  
            //}

            Test
            * CreateTestPtr()
            {
                return 
            new CTest();
            }

            void DeleteTestPtr(Test
            * t)
            {
                
            if(t != NULL)
                {
                    delete t;
                    t 
            = NULL;
                }

            }

            對loadlibrary的分裝,可以作為tools:

            //library.h
            #pragma once
            #include 
            <windows.h>
            #include 
            <string>
            #include 
            <assert.h>

            class Library
            {
            public:

             
            explicit Library(const wchar_t* name)
             {
              m_handle 
            = LoadLibrary(name);
              assert(m_handle);
              
            if (!m_handle)
               throw std::runtime_error(std::
            string("Could not find library file:"));  
             }

             ~Library()
             {
              FreeLibrary(m_handle);
             }

             void
            * GetProc(const char* name)
             {
              void
            * proc = ::GetProcAddress(m_handle, name);
              assert(proc);
              return proc;
             }

            private:
             HMODULE m_handle;
            };


            client的調(diào)用:


            #include 
            "stdafx.h"
            #include 
            "library.h"
            #include 
            "../CppDll2/test.h"

            int _tmain(int argc, _TCHAR* argv[])
            {
                
                typedef Test
            * (*CREATE)();
                typedef void (
            *DEL)(Test*);    

                Library lib(L
            "CppDll2.dll");
                
            //std::auto_ptr<Test> test = ((std::auto_ptr<Test> ) lib.GetProc("CreateTest"));
                Test
            * test = (((CREATE)(lib.GetProc("CreateTestPtr")))());
                test
            ->DoIt();
                ((DEL)(lib.GetProc(
            "DeleteTestPtr")))(test);
                return 
            0;
            }


            上面的是對類的動態(tài)調(diào)用,注意需要include頭文件哦!
            //通過API動態(tài)加載動態(tài)庫
            //對于類的導(dǎo)出,最好使用函數(shù)封裝,導(dǎo)出類的指針。
            //動態(tài)加載dll, 如果導(dǎo)出的函數(shù)只使用 extern,而沒有使用extern "C" 則GetProcAdress會找不到函數(shù)的指針,要想找到可以使用真正要找的函數(shù)原型哦,可能是函數(shù)名后加@@。。。,也可以使用編號來找到需要的函數(shù)地址。
            //但是如果導(dǎo)出函數(shù)使用extern "C"的話,導(dǎo)出函數(shù)的返回值不能是auto_ptr<>或shared_ptr<>哦,但是我們?nèi)匀豢梢允褂弥悄苤羔樑叮捎玫姆椒ㄊ遣皇褂胷eturn返回,使用函數(shù)的參數(shù)返回哦。//使用智能指針導(dǎo)出的更好的實(shí)現(xiàn),請參考 http://m.shnenglu.com/eXile

            //參考: http://m.shnenglu.com/eXile/archive/2007/04/19/22262.html

            //更多dll類型:http://www.vckbase.com/document/viewdoc/?id=1116

            //調(diào)用約定:http://blog.chinaunix.net/u/21790/showart_265932.html


            三 資源DLL

            在C++中,我們可以建立純資源的動態(tài)dll,比如說我們建立了一個動態(tài)的資源dll,里面增加一個string: id為IDS_APPLICATION,值為:aaa,
            則我們可以在client動態(tài)調(diào)用如下:

            #include "stdafx.h"
            #include 
            <windows.h>
            #include 
            "../ResDll/resource.h"

            int _tmain(int argc, _TCHAR* argv[])
            {
                HMODULE hModule
            =LoadLibrary(L"../debug/ResDll.dll");
                
            ifNULL != hModule)
                {
                    TCHAR szName[
            200];
                    ::LoadString(hModule,IDS_APPLICATION,szName,
            200);
                    FreeLibrary(hModule);
                }
                return 
            0;
            }


            資源還可以是其他的比如是icon,bitmap。。。。等,有對應(yīng)的load。()函數(shù)去調(diào)用。

            四,總結(jié)

            熟悉dll調(diào)用的3中方法,其中對靜態(tài)的調(diào)用只有2中方法,一般對類的導(dǎo)出調(diào)用要使用函數(shù)封裝哦!:~


            代碼下載:http://m.shnenglu.com/Files/mzty/DLLTest.rar

            posted on 2007-05-28 09:59 夢在天涯 閱讀(8662) 評論(1)  編輯 收藏 引用 所屬分類: CPlusPlus

            評論

            # re: C++ Dll 2007-06-22 12:26 .net編程

            好東西  回復(fù)  更多評論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807503
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久久久一本毛久久久| 日本久久久久亚洲中字幕| 97久久国产亚洲精品超碰热| 久久美女网站免费| 狠狠色狠狠色综合久久| 99久久精品日本一区二区免费| 久久久久久久亚洲精品| 精品久久久久久久无码| 免费精品99久久国产综合精品| 久久国产精品免费一区| 欧美精品一区二区精品久久| 久久天天躁狠狠躁夜夜2020老熟妇| 久久婷婷五月综合色奶水99啪| 久久精品18| 久久香蕉超碰97国产精品| 国产精品va久久久久久久| 久久久精品一区二区三区| 欧美精品福利视频一区二区三区久久久精品| 久久天天婷婷五月俺也去| 国产99久久久国产精品~~牛| 久久中文字幕精品| 久久久久国产一级毛片高清板| 久久久久亚洲精品天堂| 国产精品久久久久国产A级| 久久伊人中文无码| 国产成人久久久精品二区三区| 久久久av波多野一区二区| 久久人妻少妇嫩草AV蜜桃| 亚洲人成无码www久久久| 少妇无套内谢久久久久| 五月丁香综合激情六月久久| 亚洲精品午夜国产VA久久成人 | 久久青青草原亚洲av无码app| 久久精品成人欧美大片| 亚洲国产二区三区久久| 国内精品久久久久久久coent| 久久久久亚洲AV无码麻豆| 麻豆成人久久精品二区三区免费| 一级A毛片免费观看久久精品| 无码乱码观看精品久久| 伊人色综合久久天天人手人婷 |