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

类别:编程语言 点击:0 评论:0 推荐:

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

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

本文地址:http://com.8s8s.com/it/it28734.htm