Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to properly create instance of TADOConnection from within DLL?

Status
Not open for further replies.

djjd47130

Programmer
Nov 1, 2010
480
US
I'm building a DLL as an interaction with a database. The end goal is for programmers to use the DLL instead of connecting to the Database directly. Currently, I am passing the connection string into this DLL for every function, therefore creating/freeing a connection instance every time. What I would like to do is somehow create an instance of a TADOConnection (inside the DLL from outside), return a handle, and when calling other functions I can pass that handle so the DLL knows what TADOConnection to use based on the Handle. Is this possible? I know you can create something in a DLL and return the instance to access it, but in this case I don't need to interact with the TADOConnection from outside the DLL - I just need to call my own functions which do the work for me.

Important: This will be used across multiple languages such as C#, therefore the DLL needs to remain compatible.

It should work something like...

DLL:
Code:
function CreateDB(ConnStr: PChar): DB_HANDLE; StdCall;
begin
  DB:= TADOConnection.Create(nil);
  Result:= NewDBHandle[DB];
    (Somehow get a handle of the TADOConnection, or generate a unique ID)  
    (Need to be able to keep the TADOConnection live somewhere inside the DLL until free'd)
    (Possibility of multiple connections at once from other sources)
end;

function GetSomeData(DB: DB_HANDLE; SomeData: PChar; var Size: DWORD): Bool; StdCall;
var
  Q: TADOQuery;
  R: String;
begin
  Q:= TADOQuery.Create;
  try
    Q.Connection:= TADOConnection(GetDBHandle[DB]);
    Q.SQL.Text:= 'select MyData from MyTable where ID = 1';
    Q.Open;
      R:= Q.FieldByName('MyData').AsString;
    Q.Close;
  finally
    Size:= Length(R);
    StrPCopy(SomeData, R);
    Q.Free;
  end;
end;

procedure FreeDB(DB: DB_HANDLE); StdCall;
begin
  DelDBHandle[DB];
end;




JD Solutions
 
if you want to shield database access via the dll, you don't expose the adoconnection object but you interface the GetSomeData function, this way you abstract the DB layer for the DLL consumer...

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thanks Daddy, that is exactly what I was aiming for. I know I have to do it this way, I just don't know how. This TADOConnection component must be active and connected during any given point of time, even if the DLL is not being used.

What I'm looking for is very similar to how you manage service handles in the WinSvc unit (which is what inspired me to do this new project)...

Code:
var
  SM, S: SC_Handle;
begin
  SM:= OpenSCManager(PChar(sMachine), PChar(sDatabase), SC_MANAGER_ALL_ACCESS);
  if SM > 0 then begin
    S:= OpenService(SM, PChar(sService), SC_ALL_ACCESS);
    if S > 0 then begin
      //Do something with the service's handle (S), like start, stop, pause, query, etc.
      CloseServiceHandle(S);
    end;
    CloseServiceHandle(SM);
  end;
end;

Similarily...

Code:
var
  DB: DB_Handle;
  SomeData: String;
  Size: DWORD;
begin
  DB:= OpenDB(PChar(sServer), PChar(sDatabase), PChar(sUser), PChar(sPass));
  if DB > 0 then begin
    //Do something with the database's handle
    Size:= 255;
    SomeData:= '';
    SetLength(SomeData, Size);
    if GetSomeData(DB, PChar(SomeData), Size) then begin
      //Read SomeData, etc.
    end;
    CloseDBHandle(DB);
  end;
end;



JD Solutions
 
maybe this thread will help you:

thread102-1186877

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top