如何在C++ Builder的DLL中使用数据库控件

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

使用方法与平常的使用办法一样,不过要注意几个地方:

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()

}

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

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航