• <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++寫算法的快捷開發方式,C#與C++的交互,首先介紹c++,C#內部的DLL,COM調用。

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

            靜態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);
            };

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

            靜態lib的實現文件: 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"
            //修改編譯選項調用靜態庫
            //需要修改:include的path,lib的path,和加入lib的名字,如下:
            //C++->General->additional include directories
            //Linker->General->additional library directories
            //linker->input->additional dependencies

            //不能動態加載靜態的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;
                
            }


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

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

            實例1:演示了導出變量和函數,和前2中調用方法,修改編譯選項和pragma comment().
            動態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);


            動態dll的實現: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"
            //修改編譯選項調用靜態庫
            //需要修改: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;
            }

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

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

            動態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*);


            動態dll的類導出的實現: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;
            }


            上面的是對類的動態調用,注意需要include頭文件哦!
            //通過API動態加載動態庫
            //對于類的導出,最好使用函數封裝,導出類的指針。
            //動態加載dll, 如果導出的函數只使用 extern,而沒有使用extern "C" 則GetProcAdress會找不到函數的指針,要想找到可以使用真正要找的函數原型哦,可能是函數名后加@@。。。,也可以使用編號來找到需要的函數地址。
            //但是如果導出函數使用extern "C"的話,導出函數的返回值不能是auto_ptr<>或shared_ptr<>哦,但是我們仍然可以使用智能指針哦,采用的方法是不使用return返回,使用函數的參數返回哦。//使用智能指針導出的更好的實現,請參考 http://www.shnenglu.com/eXile

            //參考: http://www.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++中,我們可以建立純資源的動態dll,比如說我們建立了一個動態的資源dll,里面增加一個string: id為IDS_APPLICATION,值為:aaa,
            則我們可以在client動態調用如下:

            #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。()函數去調用。

            四,總結

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


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

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

            評論

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

            好東西  回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

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

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804159
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久婷婷是五月综合色狠狠| 国产色综合久久无码有码| 丁香色欲久久久久久综合网| 亚洲国产精品无码久久| 久久久久久无码Av成人影院 | 亚洲精品乱码久久久久久蜜桃| 久久久久99这里有精品10| 国内高清久久久久久| 久久精品国产亚洲77777| 东京热TOKYO综合久久精品| 久久99精品综合国产首页| 国内精品久久久久久久涩爱| 久久久久高潮综合影院| 日日躁夜夜躁狠狠久久AV| 国产精品一区二区久久| 久久亚洲中文字幕精品一区| 亚洲国产另类久久久精品| 999久久久免费精品国产| 精品久久久久久久久久中文字幕 | 国产精品久久久久久久| 久久国产成人亚洲精品影院| 亚洲AV无码久久寂寞少妇| 亚洲午夜精品久久久久久人妖| 久久婷婷国产剧情内射白浆| 久久精品国产清自在天天线| 99久久99这里只有免费的精品| 久久久久亚洲av综合波多野结衣| 九九久久精品国产| 午夜精品久久久久9999高清| 91久久香蕉国产熟女线看| 欧美亚洲色综久久精品国产| 中文字幕亚洲综合久久菠萝蜜| 久久久99精品成人片中文字幕| 久久99国产综合精品免费| 亚洲精品乱码久久久久久不卡| 精品久久久无码中文字幕天天| 欧美精品一本久久男人的天堂| 国产69精品久久久久777| 奇米影视7777久久精品人人爽| 无码人妻少妇久久中文字幕| 欧美一级久久久久久久大|