分享
 
 
 

如何在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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有