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:
JD Solutions
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