• <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>
            流量統計:
            Rixu Blog (日需博客)
            日需博客,每日必需來踩踩哦..
            posts - 108,comments - 54,trackbacks - 0

            一、VC程序調用VC編寫的導出函數

                a)定義

                    1)創建dll工程

             

                       

             

                     2)在工程下添加源程序文件,并編寫代碼

                       

                        源代碼如下:

            1 _declspec(dllexport) int IntPlus(int a,int b)
            2 {
            3 return a+b;
            4 }

                        編譯運行生成dll

                       

               b)調用

                    1)創建普通的控制臺工程并將要使用dll和lib文件拷貝至工程目錄下

             

                   

                    2)工程設置中,生命要使用的lib文件 

                   

                    3)編寫源代碼

             1 #include<iostream>
            2 using namespace std;
            3
            4 extern int IntPlus(int a,int b);
            5
            6 int main()
            7 {
            8 int sum=0;
            9 sum=IntPlus(10,200);
            10 cout<<"調用全局的DLL結果: "<<sum<<endl;
            11 return 0;
            12 }

                    4)運行結果:

                    

             

             

             

            二、VC程序調用VC編寫的導出類

                 a)定義

                     1)創建DLL工程

                           步驟同(一)相似,是普通的DLL而不是MFC DLL

                     2)編寫代碼

                         

                           頭文件代碼如下:

             

            1 class _declspec(dllexport) Computation
            2 {
            3 public:
            4 int IntPlus(int a,int b);
            5 };

             

             

                           類的實現代碼如下:

             

            1 #include "Computation.h";
            2
            3 int Computation::IntPlus(int a,int b)
            4 {
            5 return a+b;
            6 }

             

             

                 b)調用

                     1)創建普通的控制臺工程,并將要引用的文件拷至對應目錄下

                         頭文件和lib文件放于工程目錄下:

                       

                         要調用的DLL文件至于Debug目錄下:

                       

                     2)將目錄下的Computation.h添加到工程文件中

                       

                      3)編寫主程序代碼

             1 #include<iostream>
            2 #include "Computation.h"
            3 #pragma comment(lib,"ExportClassForVC.lib")
            4 using namespace std;
            5
            6 int main()
            7 {
            8 Computation compute;
            9 int sum=100;
            10 sum=compute.IntPlus(10,200);
            11 cout<<"調用導出類DLL結果: "<<sum<<endl;
            12 return 0;
            13 }

                     4)編譯運行結果如下:

                         

            三、Delphi程序調用VC編寫的導出函數

                  a)定義

                      基本同(一)中的方式相同,但是代碼中需要標識:extern "C"

                      主要代碼如下:

             

            1 extern "C" _declspec(dllexport) int IntPlus(int a,int b)
            2 {
            3 return a+b;
            4 }

             

                      注:1)之所以要聲明extern "C"就是為了解決不同語言調用時函數名改編的問題

                             2)只能用于全局函數,不能用于類的成員函數

             

                  b)調用

                       在Delphi中調用DLL十分方便,只需要將DLL文件直接拷貝至工程目錄下即可(無需拷貝lib文件)

                       

                       1)主要代碼如下:

             1 unit Main;
            2
            3 interface
            4 uses
            5 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
            6 Dialogs, StdCtrls;
            7
            8 type
            9 TForm1 = class(TForm)
            10 btn1: TButton;
            11 procedure btn1Click(Sender: TObject);
            12 private
            13 { Private declarations }
            14 public
            15 { Public declarations }
            16 end;
            17
            18 function IntPlus(a:Integer; b:Integer):Integer;cdecl;external 'GlobalFuncForDelphi.dll';
            19
            20 var
            21 Form1: TForm1;
            22
            23 implementation
            24 {$R *.dfm}
            25 procedure TForm1.btn1Click(Sender: TObject);
            26 var
            27 sum:integer;
            28 begin
            29 sum:=IntPlus(100,8);
            30 showmessage('調用DLL中導出函數的結果為:'+inttostr(sum));
            31 end;
            32
            33 end.

                     注:由于C中默認的調用方式是cdecl方式,所以在該delphi程序中也要以cdecl方式調用

                      2)編譯運行測試結果如下:

                        

             

            四、Delphi程序調用VC編寫的導出類

                  說明:Delphi程序不能直接調用VC編寫的導出類,故需要用VC再編寫一個導出函數進行橋接;

                           Delphi通過調用該導出函數來訪為前一個dll中的導出類

                  a)定義用于橋接的DLL

                     1)將(三)中VC導出類的Computation.h和lib文件拷貝至橋接程序工程目錄下

                          

                     2)將要調用的DLL文件拷貝至工程的Debug目錄下

                         

                     3)編寫主程序

             1 #include "Computation.h"
            2 #pragma comment(lib,"ExportClassForVC.lib")
            3
            4 extern "C" _declspec(dllexport) int IntPlusBridge(int a,int b)
            5 {
            6 Computation compute;
            7 int sum=100;
            8 sum=compute.IntPlus(a,b);
            9 return sum;
            10 }

                      4)編譯生成用于橋接的DLL

                          

                     附:實際上對于引用的Computation.h頭文件,并不需要手動加入到Header Files中;項目中有一句“include XXX.h”,而該頭文件不是在項目中定義的,則該文件會被認為是外部依賴的,會被自動添加到External Dependencies目錄下

                          

                   b)調用

                       1)編寫主程序

             1 unit Main;
            2
            3 interface
            4
            5 uses
            6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
            7 Dialogs, StdCtrls;
            8
            9 type
            10 TForm1 = class(TForm)
            11 btn1: TButton;
            12 procedure btn1Click(Sender: TObject);
            13 private
            14 { Private declarations }
            15 public
            16 { Public declarations }
            17 end;
            18
            19 function IntPlusBridge(a:Integer; b:Integer):Integer;cdecl;external 'ExportClassBridgeForDelphi.dll';
            20
            21 var
            22 Form1: TForm1;
            23
            24 implementation
            25 {$R *.dfm}
            26 procedure TForm1.btn1Click(Sender: TObject);
            27 var
            28 sum:integer;
            29 begin
            30 sum:=IntPlusBridge(20,800);
            31 showmessage('調用導出類函數的執行結果為:'+inttostr(sum));
            32 end;
            33
            34 end.

                     2)將導出類DLL和橋接DLL添加到工程目錄下

                       

                     3)編譯運行執行結果如下:

                       


            Logo
            作者:Gezidan
            出處:http://www.rixu.net    
            本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
            posted on 2011-08-07 19:18 日需博客 閱讀(698) 評論(0)  編輯 收藏 引用 所屬分類: C C++ 、技術文章 、轉載
            国产99久久久久久免费看| 色综合久久久久综合体桃花网| 91精品国产综合久久香蕉 | 97久久精品无码一区二区| 久久99精品国产麻豆宅宅| 久久国产香蕉一区精品| 亚洲国产欧美国产综合久久| 亚洲国产精久久久久久久| 亚洲精品第一综合99久久 | 久久香综合精品久久伊人| 国产亚洲精久久久久久无码| 伊人 久久 精品| 99久久99久久精品国产片果冻| 中文字幕无码免费久久| 久久精品亚洲福利| 久久精品国产精品亚洲精品| 久久午夜无码鲁丝片秋霞 | 久久精品中文无码资源站| 久久久综合九色合综国产| 日韩av无码久久精品免费| 久久久久亚洲av毛片大| 久久精品www| 久久99国产乱子伦精品免费| 久久AV无码精品人妻糸列| 美女久久久久久| 久久久久亚洲AV成人网| 嫩草影院久久国产精品| 国产精品久久久久jk制服| 欧美亚洲色综久久精品国产| 久久精品日日躁夜夜躁欧美| 久久亚洲精品无码播放| 久久婷婷色综合一区二区| 久久se精品一区二区影院| 精品乱码久久久久久夜夜嗨| 国产成人AV综合久久| 99久久精品国产免看国产一区| 久久精品亚洲一区二区三区浴池| 亚洲va久久久噜噜噜久久天堂 | 国内精品久久人妻互换| 精品久久久久久久无码| 国产精品禁18久久久夂久 |