靜態使用DLL動態連接庫

一、首先創建一個DLL工程。

1)  bcb集成環境中,通過菜單File | new | other打開new Items對話框,選中DLL Wizar項,選中C++,選中Use VCL,確定。系統會自動創建一個DLL工程,內容如下:

    //---------------------------------------------------------------------------

//---------------------------------------------------------------------------

#include <vcl.h>

#include <windows.h>

#pragma hdrstop

//---------------------------------------------------------------------------

//   Important note about DLL memory management when your DLL uses the

//   static version of the RunTime Library:

//

//   If your DLL exports any functions that pass String objects (or structs/

//   classes containing nested Strings) as parameter or function results,

//   you will need to add the library MEMMGR.LIB to both the DLL project and

//   any other projects that use the DLL.  You will also need to use MEMMGR.LIB

//   if any other projects which use the DLL will be performing new or delete

//   operations on any non-TObject-derived classes which are exported from the

//   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling

//   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,

//   the file BORLNDMM.DLL should be deployed along with your DLL.

//

//   To avoid using BORLNDMM.DLL, pass string information using "char *" or

//   ShortString parameters.

//

//   If your DLL uses the dynamic version of the RTL, you do not need to

//   explicitly add MEMMGR.LIB as this will be done implicitly for you

//---------------------------------------------------------------------------

 <?xml:namespace prefix = o />

#pragma argsused

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)

{

        return 1;

}

//---------------------------------------------------------------------------

 

2)  現在可以通過菜單Project | Build Project1來編譯和連接當前的DLL工程。儘管還沒有添加任何一句代碼,DLL文件仍然可以順利創建。但這裏我們要繼續進行下去。新建一個目錄: D:/samples/mydll,保存資源文件爲mydllprj.cpp,保存工程文件爲mydllprj.bpr

3)  新建的DLL中包含兩個函數,分別實現了兩個整數的加法運算和減法運算,具體代碼如下:

//---------------------------------------------------------------------------

 

#include <vcl.h>

#include <windows.h>

#pragma hdrstop

//---------------------------------------------------------------------------

//   Important note about DLL memory management when your DLL uses the

//   static version of the RunTime Library:

//

//   If your DLL exports any functions that pass String objects (or structs/

//   classes containing nested Strings) as parameter or function results,

//   you will need to add the library MEMMGR.LIB to both the DLL project and

//   any other projects that use the DLL.  You will also need to use MEMMGR.LIB

//   if any other projects which use the DLL will be performing new or delete

//   operations on any non-TObject-derived classes which are exported from the

//   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling

//   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,

//   the file BORLNDMM.DLL should be deployed along with your DLL.

//

//   To avoid using BORLNDMM.DLL, pass string information using "char *" or

//   ShortString parameters.

//

//   If your DLL uses the dynamic version of the RTL, you do not need to

//   explicitly add MEMMGR.LIB as this will be done implicitly for you

//---------------------------------------------------------------------------

 

#pragma argsused   int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)

{

        return 1;

}

//往下爲新添加代碼

extern "C" __declspec(dllexport) int PlusNum(int X,int Y)

{

  return X+Y;

}

extern "C" __declspec(dllexport) int MinusNum(int X,int Y)

{

  return X-Y;

}

//---------------------------------------------------------------------------

4)  開始編譯和連接工程,編譯和連接成功後在D:/samples/mydll中可見到mydllprj.dllmydllprj.lib

二、建立客戶應用程序,調用mydllprj.dll

1)  通過菜單File | Close all 關閉所有工程和文件,然後通過菜單File |new Application 打開一個新的工程,將資源和工程文件保存在D:/samples,分別命名爲mysmp.cppmysmpdll.bpr

2)  設計窗體界面如下:

<?xml:namespace prefix = v />

   調整好後如下:

3)  ComboBox1Items屬性中依次加入:兩數相加兩數相減,並將Style屬性設置爲csDropDownList

4)  mysmp.h中的窗體類Tform1聲明之前添加下面的代碼:

//---------------------------------------------------------------------------

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

//新加代碼

extern "C" __declspec(dllimport) int PlusNum(int X,int Y);

extern "C" __declspec(dllimport) int MinusNum(int X,int Y);

//extern "C" __declspec(dllimport) int MultiplyNum(int X,int Y);

//新加代碼結束

//---------------------------------------------------------------------------

class TForm1 : public TForm

{

__published:    // IDE-managed Components

        TLabel *Label1;

        TLabel *Label2;

        TLabel *Label3;

5)  添加Button1控件的OnClick事件的處理代碼如下:

//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)

{

 if(ComboBox1->Text=="兩數相加")

   Edit3->Text=IntToStr(PlusNum(StrToInt(Edit1->Text),StrToInt(Edit2->Text)));

 else if(ComboBox1->Text=="兩數相減")

   Edit3->Text=IntToStr(MinusNum(StrToInt(Edit1->Text),StrToInt(Edit2->Text)));

// else if(ComboBox1->Text=="兩數相乘")

  // Edit3->Text=IntToStr(MultiplyNum(StrToInt(Edit1->Text),StrToInt(Edit2->Text)));

 else

   MessageBox(Form1->Handle,"請指定一個計算方法。","警告",MB_OK|MB_ICONWARNING);

}

//---------------------------------------------------------------------------

6)  現在編譯連接和運行程序,程序會提示錯誤信息,錯誤信息如下:

  [Linker Error] Unresolved external '_PlusNum' referenced from D:/SAMPLES/MYSMP.OBJ

  [Linker Error] Unresolved external '_MinusNum' referenced from D:/SAMPLES/MYSMP.OBJ

這是因爲編譯器沒有找到開始生成的mydllprj.dll這個連接庫文件,需要在bcb集成開發環境中加入,通過菜單Project | add to Project,把d:/samples/mydll/mydllprj.lib添加進來。然後編譯連接並運行,這時系統會提示找不到mydllprj.dll運行庫,這是因爲mydllprj.dlld:/samples/mydll/,系統按指定的路徑找不到這個文件,我們這裏可以把mydllprj.dll複製到d:/samples下,然後編譯運行,這是你就可以看到期待已久的界面了。 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章