Board index » delphi » D2 - TDatabase/TQuery DLL Split

D2 - TDatabase/TQuery DLL Split

Learned Ones,

   We're trying to do a seemingly simple task.  

o Our 'main' application contains a TDatabase component.
o Many DLL's with TQuerys and we want them to use that
  TDatabase component in the app.
o TDatabase Properties:
    .KeepConnection = TRUE
    .Connected = TRUE
    .LoginPrompt = FALSE
o The application connects to the DB automagically at start up
  with args or ini entries.
o DLL's are dynamically linked (static or dynamic = same results).
o Oracle db, SQL Links (32)

This almost works.  One of the function arguments (in
the DLL) is of type TDatabase (a pointer, right?).
We then programatically set the TQuery.DatabaseName
to the TDatabase.DatabaseName in the DLL.

It then prompts us for name and password - WHY??!!  

That TDatabase component is already active.  We even put
in a MessageDLG to show us the Connected and other useful
properties at the beginning of the DLL function.  Everything
is groovy until we open any dataset in the DLL
(eg TQuery.Open).  We are then prompted for the name and
password!

If you made it this far - Thank You for reading.  ANY ideas
at all will be greatfully accepted.  Surely somebody is
doing something similar?

Oh yeah, I tried searching Borland's site for any appropriate
TI's, but loki.borland.com appeared to be down...

In advance,

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Chris Kasten
Programmer/Analyst

#include <std/disclaimer.h>
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

 

Re:D2 - TDatabase/TQuery DLL Split


Quote
kas...@brookings.net (Chris Kasten) wrote:

SOLVED!!!  Solution at bottom if interested...

Quote

> Learned Ones,

>    We're trying to do a seemingly simple task.  

> o Our 'main' application contains a TDatabase component.
> o Many DLL's with TQuerys and we want them to use that
>   TDatabase component in the app.
> o TDatabase Properties:
>     .KeepConnection = TRUE
>     .Connected = TRUE
>     .LoginPrompt = FALSE
> o The application connects to the DB automagically at start up
>   with args or ini entries.
> o DLL's are dynamically linked (static or dynamic = same results).
> o Oracle db, SQL Links (32)

> This almost works.  One of the function arguments (in
> the DLL) is of type TDatabase (a pointer, right?).
> We then programatically set the TQuery.DatabaseName
> to the TDatabase.DatabaseName in the DLL.

> It then prompts us for name and password - WHY??!!  

> That TDatabase component is already active.  We even put
> in a MessageDLG to show us the Connected and other useful
> properties at the beginning of the DLL function.  Everything
> is groovy until we open any dataset in the DLL
> (eg TQuery.Open).  We are then prompted for the name and
> password!

> If you made it this far - Thank You for reading.  ANY ideas
> at all will be greatfully accepted.  Surely somebody is
> doing something similar?

Had some help from Borland, but nothing specific enough
to get us going anywhere.  Finally came up with a solution
that appears to be working.

o Pass the TDatabase.Handle from the Main App to the DLL.
o Have the DLL create a TDatabase Component
   o give it some sort of name (db.databasename := 'Bill')
   o set its Handle to the one passed in (db.Handle := dbh)

Simplicity!  I'll copy the source for a simple little APP
and DLL combo below.  If anyone sees any problem with this
method holler quick!

Thanks.

{==================================
 Application Code
 ==================================}
unit appl;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, DB, bde;

type
  TForm1 = class(TForm)
    dbOracle: TDatabase;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

procedure myDllCall(dbhandle: hdbidb); stdcall; external 'TESTDLL.DLL'

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
   dbOracle.Open;
   myDllCall(dbOracle.Handle);
end;

end.

{==================================
 DLL Code
 ==================================}
library testdll;

uses
  bde,
  db,
  dbTables,
  Dialogs;

procedure myDllCall(dbHandle : hdbidb); stdcall;
var
   Database : TDatabase;
begin
   Database := TDatabase.Create(nil);
   try
      Database.DatabaseName := 'anything_goes';
      Database.Handle := dbHandle;

      with TQuery.Create(nil) do begin
         try
            DatabaseName := Database.DatabaseName;
            SQL.Add('Select sysdate from dual');
            open;
            ShowMessage(FieldByName('sysdate').AsString);
         finally
            Free;
         end;
      end;
   finally
      Database.Free;
   end;
end;

exports
   myDllCall index 1;

begin

end.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Chris Kasten
Programmer/Analyst

#include <std/disclaimer.h>
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Re:D2 - TDatabase/TQuery DLL Split


Quote
kas...@brookings.net (Chris Kasten) wrote:
>kas...@brookings.net (Chris Kasten) wrote:
>SOLVED!!!  Solution at bottom if interested...
>o Pass the TDatabase.Handle from the Main App to the DLL.
>o Have the DLL create a TDatabase Component
>   o give it some sort of name (db.databasename := 'Bill')
>   o set its Handle to the one passed in (db.Handle := dbh)

That looks like the simple fix I have been looking for!  I wonder if I
can pass a PB transaction object database handle to the Delphi written
DLL??

Re:D2 - TDatabase/TQuery DLL Split


Quote
cklinc...@worldnet.att.net (Chris Lincoln) wrote:
> kas...@brookings.net (Chris Kasten) wrote:

> >kas...@brookings.net (Chris Kasten) wrote:

> >SOLVED!!!  Solution at bottom if interested...

> >o Pass the TDatabase.Handle from the Main App to the DLL.
> >o Have the DLL create a TDatabase Component
> >   o give it some sort of name (db.databasename := 'Bill')
> >   o set its Handle to the one passed in (db.Handle := dbh)

> That looks like the simple fix I have been looking for!  I wonder if I
> can pass a PB transaction object database handle to the Delphi written
> DLL??

Dunno.  By PB do you mean PowerBuilder?  If so, I really doubt it.
Anything whose handle is an hdbidb type (I think its defined in BDE.PAS)
ought to work tho.

HTH,

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Chris Kasten
Programmer/Analyst

#include <std/disclaimer.h>
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Other Threads