如何在C++ Builder的DLL中使用數據庫控件

使用方法與平常的使用辦法一樣,不過要注意幾個地方:

1、TSession及TDatabase要加上或者是用new生成實例,平時可能不會加TSession,但在DLL中一定要這樣做,不然在啓動或者退出時會出錯。

2、在導出DLL的函數中,一定生成DataModule的實例,不然連接數據庫時會有問題的。

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

#include
#include
#include "Unit2.h"
#include "Unit4.h"

#pragma hdrstop

#pragma argsused
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
? return 1;
}
//---------------------------------------------------------------------------

extern "C"? __declspec(dllexport) void ShowForm();

void ShowForm()
{
???? TForm2 *aTForm2 = new TForm2(NULL);
???? TDataModule4 *aTDataModule4 = new TDataModule4(aTForm2);
???? aTForm2->ShowModal();
???? delete aTDataModule4;if(aTDataModule4) aTDataModule4 = NULL;
???? delete aTForm2; if (aTForm2) aTForm2 = NULL;
}

調用


void __fastcall TForm3::Button1Click(TObject *Sender)
{
? HINSTANCE Dll = LoadLibrary(".//Project1.dll");

???? if (Dll)
???? {
???????? LoadFunction = (ShowType *)GetProcAddress(Dll, "_ShowForm");
???????? if (LoadFunction)
???????????? LoadFunction();
???????? else
???????????? ShowMessage(SysErrorMessage(GetLastError()));
???????? FreeLibrary(Dll);
???? }

???? else
???? {
???????? ShowMessage(SysErrorMessage(GetLastError()));
???????? ShowMessage("Unable to load the Dll");
???? }

}
//---------------------------------------------------------------------------

不能上傳zip文件,沒有辦法將整個工程傳上。

USEFORM("Unit2.cpp", Form2);
USEFORM("Unit4.cpp", DataModule4); /* TDataModule: File Type */
//---------------------------------------------------------------------------

#include
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("Unit3.cpp", Form3);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
? try
? {
???? Application->Initialize();
???? Application->CreateForm(__classid(TForm3), &Form3);
???? Application->Run();
? }
? catch (Exception &exception)
? {
???? Application->ShowException(&exception);
? }
? catch (...)
? {
???? try
???? {
?????? throw Exception("");
???? }
???? catch (Exception &exception)
???? {
?????? Application->ShowException(&exception);
???? }
? }
? return 0;
}
//---------------------------------------------------------------------------
#------------------------------------------------------------------------------
VERSION = BWS.01
#------------------------------------------------------------------------------
!ifndef ROOT
ROOT = $(MAKEDIR)/..
!endif
#------------------------------------------------------------------------------
MAKE = $(ROOT)/bin/make.exe -$(MAKEFLAGS) -f$**
DCC = $(ROOT)/bin/dcc32.exe $**
BRCC = $(ROOT)/bin/brcc32.exe $**
#------------------------------------------------------------------------------
PROJECTS = Project1.dll Project2.exe
#------------------------------------------------------------------------------
default: $(PROJECTS)
#------------------------------------------------------------------------------

Project1.dll: Project1.bpr
? $(ROOT)/bin/bpr2mak $**
? $(ROOT)/bin/make -$(MAKEFLAGS) -f$*.mak

Project2.exe: Project2.bpr
? $(ROOT)/bin/bpr2mak $**
? $(ROOT)/bin/make -$(MAKEFLAGS) -f$*.mak


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

#include
#include
#include "Unit2.h"
#include "Unit4.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
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
? return 1;
}
//---------------------------------------------------------------------------

extern "C"? __declspec(dllexport) void ShowForm();

void ShowForm()
{
???? TForm2 *aTForm2 = new TForm2(NULL);
???? TDataModule4 *aTDataModule4 = new TDataModule4(aTForm2);
???? aTForm2->ShowModal();
???? delete aTDataModule4;if(aTDataModule4) aTDataModule4 = NULL;
???? delete aTForm2; if (aTForm2) aTForm2 = NULL;
}

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

#include
#pragma hdrstop

#include "Unit2.h"
#include "Unit4.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
? : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Button1Click(TObject *Sender)
{
? //DataModule4->dllTestDB->Connected = true;//不能這樣用

?MessageDlg("DLL窗口測試程序中的按鈕!!",mtWarning,TMsgDlgButtons() << mbYes << mbNo,0);
}
//---------------------------------------------------------------------------
object Form2: TForm2
? Left = 209
? Top = 243
? Width = 759
? Height = 426
? Caption = 'Form2'
? Color = clBtnFace
? Font.Charset = DEFAULT_CHARSET
? Font.Color = clWindowText
? Font.Height = -11
? Font.Name = 'MS Sans Serif'
? Font.Style = []
? OldCreateOrder = False
? PixelsPerInch = 96
? TextHeight = 13
? object Label1: TLabel
??? Left = 424
??? Top = 16
??? Width = 255
??? Height = 33
??? Caption = #36890#36807'DLL'#26469#20351#29992#25968#25454#24211#25511#20214#65281#65281#65281
??? Font.Charset = DEFAULT_CHARSET
??? Font.Color = clRed
??? Font.Height = -16
??? Font.Name = 'MS Sans Serif'
??? Font.Style = [fsBold]
??? ParentFont = False
? end
? object Button1: TButton
??? Left = 8
??? Top = 8
??? Width = 75
??? Height = 25
??? Caption = 'Button1'
??? TabOrder = 0
??? OnClick = Button1Click
? end
? object DBNavigator1: TDBNavigator
??? Left = 96
??? Top = 8
??? Width = 240
??? Height = 25
??? DataSource = DataModule4.DataSource1
??? TabOrder = 1
? end
? object DBGrid1: TDBGrid
??? Left = 8
??? Top = 32
??? Width = 393
??? Height = 177
??? DataSource = DataModule4.DataSource1
??? TabOrder = 2
??? TitleFont.Charset = DEFAULT_CHARSET
??? TitleFont.Color = clWindowText
??? TitleFont.Height = -11
??? TitleFont.Name = 'MS Sans Serif'
??? TitleFont.Style = []
? end
? object DBMemo1: TDBMemo
??? Left = 8
??? Top = 208
??? Width = 393
??? Height = 169
??? DataField = 'Notes'
??? DataSource = DataModule4.DataSource1
??? ScrollBars = ssVertical
??? TabOrder = 3
? end
? object DBImage1: TDBImage
??? Left = 408
??? Top = 72
??? Width = 329
??? Height = 201
??? DataField = 'Graphic'
??? DataSource = DataModule4.DataSource1
??? TabOrder = 4
? end
end
//---------------------------------------------------------------------------

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published:?// IDE-managed Components
? TButton *Button1;
? TDBNavigator *DBNavigator1;
? TDBGrid *DBGrid1;
? TDBMemo *DBMemo1;
? TDBImage *DBImage1;
? TLabel *Label1;
? void __fastcall Button1Click(TObject *Sender);
private:?// User declarations
public:??// User declarations
? __fastcall TForm2(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------

#include
#pragma hdrstop

#include "Unit3.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
typedef void __declspec(dllimport)ShowType();
ShowType *LoadFunction;


TForm3 *Form3;
//---------------------------------------------------------------------------
__fastcall TForm3::TForm3(TComponent* Owner)
? : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm3::Button1Click(TObject *Sender)
{
? HINSTANCE Dll = LoadLibrary(".//Project1.dll");

???? if (Dll)
???? {
???????? LoadFunction = (ShowType *)GetProcAddress(Dll, "_ShowForm");
???????? if (LoadFunction)
???????????? LoadFunction();
???????? else
???????????? ShowMessage(SysErrorMessage(GetLastError()));
???????? FreeLibrary(Dll);
???? }

???? else
???? {
???????? ShowMessage(SysErrorMessage(GetLastError()));
???????? ShowMessage("Unable to load the Dll");
???? }

}
//---------------------------------------------------------------------------
object Form3: TForm3
? Left = 247
? Top = 132
? Width = 696
? Height = 480
? Caption = 'Form3'
? Color = clBtnFace
? Font.Charset = DEFAULT_CHARSET
? Font.Color = clWindowText
? Font.Height = -11
? Font.Name = 'MS Sans Serif'
? Font.Style = []
? OldCreateOrder = False
? PixelsPerInch = 96
? TextHeight = 13
? object Button1: TButton
??? Left = 152
??? Top = 176
??? Width = 75
??? Height = 25
??? Caption = 'Button1'
??? TabOrder = 0
??? OnClick = Button1Click
? end
end
//---------------------------------------------------------------------------

#ifndef Unit3H
#define Unit3H
//---------------------------------------------------------------------------
#include
#include
#include
#include
//---------------------------------------------------------------------------
class TForm3 : public TForm
{
__published:?// IDE-managed Components
? TButton *Button1;
? void __fastcall Button1Click(TObject *Sender);
private:?// User declarations
public:??// User declarations
? __fastcall TForm3(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm3 *Form3;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------

#include
#pragma hdrstop

#include "Unit4.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TDataModule4 *DataModule4;
//---------------------------------------------------------------------------
__fastcall TDataModule4::TDataModule4(TComponent* Owner)
? : TDataModule(Owner)
{
}
//---------------------------------------------------------------------------
?object DataModule4: TDataModule4
? OldCreateOrder = False
? Left = 259
? Top = 181
? Height = 203
? Width = 335
? object DataSource1: TDataSource
??? DataSet = Table1
??? Left = 136
??? Top = 16
? end
? object Table1: TTable
??? Active = True
??? DatabaseName = 'dllTestDB'
??? SessionName = 'dllTestSession'
??? TableName = 'biolife.db'
??? Left = 192
??? Top = 24
? end
? object dllTestDB: TDatabase
??? AliasName = 'BCDEMOS'
??? Connected = True
??? DatabaseName = 'dllTestDB'
??? SessionName = 'dllTestSession'
??? Left = 88
??? Top = 16
? end
? object Session1: TSession
??? Active = True
??? SessionName = 'dllTestSession'
??? Left = 24
??? Top = 8
? end
end
//---------------------------------------------------------------------------

#ifndef Unit4H
#define Unit4H
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------
class TDataModule4 : public TDataModule
{
__published:?// IDE-managed Components
? TDataSource *DataSource1;
? TTable *Table1;
? TDatabase *dllTestDB;
? TSession *Session1;
private:?// User declarations
public:??// User declarations
? __fastcall TDataModule4(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TDataModule4 *DataModule4;
//---------------------------------------------------------------------------
#endif

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章