• <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++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            C++ Dll

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

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

            靜態(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ù)的實現(xiàn)必須寫在.cpp文件中,否則編譯有錯,說重復定義
            int Max(int x, int y);

            靜態(tài)lib的實現(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調用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"
            //修改編譯選項調用靜態(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;
                
            }


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

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

            實例1:演示了導出變量和函數(shù),和前2中調用方法,修改編譯選項和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的實現(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的調用: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"
            //修改編譯選項調用靜態(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(
            "比較的結果為 %d\n",a);
                a
            = Max(8,10);
                printf(
            "比較的結果為%d\n",a);
                
                printf(
            "導出的變量:%d\n",nCppDll);

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

                return 
            0;
            }

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

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

            動態(tài)dll的類導出: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的類導出的實現(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的調用:


            #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)調用,注意需要include頭文件哦!
            //通過API動態(tài)加載動態(tài)庫
            //對于類的導出,最好使用函數(shù)封裝,導出類的指針。
            //動態(tài)加載dll, 如果導出的函數(shù)只使用 extern,而沒有使用extern "C" 則GetProcAdress會找不到函數(shù)的指針,要想找到可以使用真正要找的函數(shù)原型哦,可能是函數(shù)名后加@@。。。,也可以使用編號來找到需要的函數(shù)地址。
            //但是如果導出函數(shù)使用extern "C"的話,導出函數(shù)的返回值不能是auto_ptr<>或shared_ptr<>哦,但是我們仍然可以使用智能指針哦,采用的方法是不使用return返回,使用函數(shù)的參數(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

            //調用約定: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)調用如下:

            #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。。。。等,有對應的load。()函數(shù)去調用。

            四,總結

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


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

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

            評論

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

            好東西  回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統(tǒng)計

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

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1805194
            • 排名 - 5

            最新評論

            閱讀排行榜

            亚洲AV无码久久精品成人| 日韩精品国产自在久久现线拍| 国产精品99久久精品| 伊人久久大香线焦AV综合影院| 无码国内精品久久人妻麻豆按摩 | a级毛片无码兔费真人久久| 囯产精品久久久久久久久蜜桃| 日韩人妻无码一区二区三区久久99| 久久国产视频网| 久久久久亚洲精品中文字幕 | 久久无码精品一区二区三区| 一本久久久久久久| 精品久久久久久无码人妻热| 久久久久无码精品| 久久精品综合网| 77777亚洲午夜久久多人| 亚洲人成精品久久久久| 77777亚洲午夜久久多喷| 国产精品无码久久综合| 久久久久久久综合日本亚洲| 久久久综合香蕉尹人综合网| 国产A三级久久精品| 狠狠色丁香久久婷婷综合五月| 99久久99久久精品国产片果冻| 久久久久亚洲AV无码专区桃色| 久久精品日日躁夜夜躁欧美| 国产午夜精品久久久久免费视| 777久久精品一区二区三区无码| 久久这里只有精品视频99| 精产国品久久一二三产区区别| 久久99精品久久久久婷婷| 99久久国产亚洲高清观看2024| 伊人久久大香线蕉综合5g| 久久国产精品77777| 久久伊人五月天论坛| 国产成人久久精品激情| 久久久久女教师免费一区| 久久av无码专区亚洲av桃花岛| 久久久久久亚洲精品不卡| 国产国产成人精品久久| 亚洲国产精品成人久久|