How to use the database control in the C ++ Builder DLL

zhaozj2021-02-16  52

The use method is the same as the usual way of use, but you should pay attention to a few places:

1, TSESSION and TDATABASE To add or use new generation instances, usually do not add Tsession, but must do this in the DLL, otherwise it will be wrong when starting or exiting.

2. In the function of exporting the DLL, you must generate a DataModule instance, otherwise there is a problem with the database.

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

#include

#include

#include "unit2.h"

#include "unit4.h"

#pragma HDRSTOP

#pragma argsusedbool 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 attataModule4 ; if (atdataModule4) atdataModule4 = NULL; ???? delete atform2; if (atform2) atform2 = null;

transfer

Void __fastcall tform3 :: button1click (TOBJECT * Sender) {? hinstance dll = loadingLibrary (".// Project1.dll");

???? if (dll) ???? {???????? LoadFunction = (showType *) getProcaddress (DLL, "_showform"); ???????? if (loadfunction) ?? ?????????? loadFunction (); ??????????????????????? ShowMessage (GetLastError ())); ????? ???Freelibrary (dll); ????}

???? Else ???? {???????? showMessage (getLastError ())); ???????? ShowMessage ("Unable to load the dll");??? ?

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

You can't upload zip files, there is no way to pass throughout the project.

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 - $ **

DCC = $ (root) /bin/dcc32.exe $ **

BRCC = $ (root) /bin/brcc32.exe $ **

# ------------------------------------------------- -----------------------------

Projects = Project1.dll Project2.exe

# ------------------------------------------------- -----------------------------

DEFAULT: $ (Projects)

# ------------------------------------------------- -----------------------------

Project1.dll: Project1.bpr? $ (Root) / bin / bpl2mak $ **? $ (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 There Cases, // ?? The file borlndmm.dll should be deployed along with your dll.//// ?? to avoid use boorlndmm.dll, pass string information using "char *" OR / / ?? Shortstring parameters./// ?? I F 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 argsusedbool 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 attataModule4 ; if (atdataModule4) atdataModule4 = NULL; ???? delete atform2; if (atform2) atform2 = null;} // ----------------------- -------------------------------------------------- -

#include

#pragma HDRSTOP

#include "unit2.h" #include "unit4.h" // ---------------------------------- ----------------------------------------- # prgma package (smart_init) #pragma resource "* .dfm" TFORM2 * FORM2; // --------------------------------------- ------------------------------------__ fastcall tform2 :: tform2 (tcomponent * Owner)?: tform ( Owner) {} // -------------------------------------------- -------------------------------

Void __fastcall tform2 :: button1click (TOBJECT * Sender) {? // DataModule4-> DLLTESTSTDB-> Connected = true; // cannot be used

? Messagedlg ("DLL window test program !!", 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 ??? capen = # 36890 # 36807'dll '# 26469 # 20351 # 29992 # 25968 # 25454 # 20214 # 65281 # 65281 # 65281 # 65281 # 65281 # 65281 # 65281 # 65281 # 65281 # 65281 # 65281 # 65281 # 65281 # 65281 # 65281 # 65281 # 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? Endend // ---------------------------- -------------------------------------------------- ----------------- # i indef unit2h # define unit2h // ------------------------- -------------------------------------------------- #include

#include

#include

#include

#include

#include

#include

#include

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

Class TFORM2: Public TForm

{

__published:? // IDE-management 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" // ----------------------------------------- ---------------------------------- # prgma package (smart_init) #pragma resource "* .dfm" typef void __Declspec (dllimport) showType (); showtype * loading;

TFORM3 * form3; // -------------------------------------------- -------------------------------__ fastcall tform3 :: tform3 (tcomponent * ooner)?: tform (ooner) {} / / --------------------------------------------------- -------------------------- Void __fastcall tform3 :: button1click (TOBJECT * Sender) {? hinstance dll = loadingLibrary (". // Project1. DLL ");

???? if (dll) ???? {???????? LoadFunction = (showType *) getProcaddress (DLL, "_showform"); ???????? if (loadfunction) ?? ?????????? loadFunction (); ??????????????????????? ShowMessage (GetLastError ())); ????? ???Freelibrary (dll); ????}

???? Else ???? {???????? showMessage (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? Endend // --------------------------------------- ----------------------------------

#ifndef unit3H # Define unit3H // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------- # include

#include

#include

#include

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

Class TFORM3: PUBLIC TFORM

{

__published:? // IDE-management 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 (TDataModule) {} // -------------------------------------------------- ------------------------? 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 DLLTESTSTDB: TDATABASE ??? AliasName =' bcdemos '??? connect = true ??? DatabaseName =' Dlltestdb '??? sessionname =' dlltestSession '??? left = 88 ??? TOP = 16? END? Object session1: tsession ??? Active = true ??? sessionname =' DLLTestSe SSION '??? left = 24 ??? TOP = 8? Endend // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------- # i indef unit4h # define unit4h / / --------------------------------------------------- -------------------------- # include

#include

#include

#include

#include

#include

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

Class TdataModule4: Public TdataModule

{

__published:? // IDE-management Components

TDataSource * DataSource1;

Ttable * Table1;

TDATABASE * DLLTESTDB;

? Tession * session1;

PRIVATE:? // user declarations

Public: ?? // user declarations? __fastcall tdataModule4 (Tcomponent * Owner);

}

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

Extern package tdataModule4 * DataModule4;

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

#ENDIF

转载请注明原文地址:https://www.9cbs.com/read-20148.html

New Post(0)