Page 1 of 1
Connect to mysql inside dll
Posted: Fri 27 Nov 2009 02:09
by aamoaa
Hi ,
For example, I have a main unit of the program.
this unit call dll library the dll include connection to the database in the form and other code .
When i close the application i write this in formclose
Code: Select all
procedure Tfr_sell_buy.FormClose (Sender: TObject; var Action: TCloseAction);
begin
MyConnection1.Connected: = false;
MyConnection1.Free;
Action: = caFree;
end;
Now I can close the application with out problem.
Problem if i add a connection object MyConnection1 on the main unit or just adding myaccess to uses in the main unit.
When application is terminated i found it still i manager task.
and i can not freelibrary it also return me error .
How i Solved it .
i used trial mydac 5.90 for d2010
Thank you
Posted: Fri 27 Nov 2009 08:17
by Dimon
This problem is connected with work with dll, but not with MyDAC. The point is that you create the TMyConnection class object in an application and use it in a dll. Methods of TMyConnection use the "as" operator which returns error.
To solve the problem you can use the TCustomMyConnection.AssignConnect method to assign the TMyConnection object created in application to the TMyConnection object of dll.
Also you can use the approach used in the MyDAC DLL demo. You can find this demo by the following path: MyDAC_InstDir\Demos\Miscellaneous\Dll.
MyDAC_InstDir is the MyDAC installation directory on your computer.
Posted: Sat 28 Nov 2009 15:53
by aamoaa
Same Problem in your your Demos\dll
I'm thinking to use the class (TThread) to connection . THis way good or not
Posted: Mon 30 Nov 2009 07:37
by Dimon
Please, specify exact steps to reproduce the problem.
Posted: Mon 30 Nov 2009 23:59
by aamoaa
Hi,
Code: Select all
library aacol;
uses
SysUtils, Classes, myaccess, mydacvcl, IniFiles;
var con : TMyConnection ;
myq_m : TMyQuery ;
myq_d : TMyQuery ;
{$R *.res}
//----------------------------------------
function F_cusname : TStringList ;
var i : Integer ;
begin
//------------
con := TMyConnection.Create(nil);
myq_m := TMyQuery.Create(nil);
myq_d := TMyQuery.Create(nil);
Result := TStringList.Create ;
with con do
begin
Server := 'localhost';
Database := 'aa';
Username := 'root';
Password := '11';
LoginPrompt := false ;
Options.UseUnicode := true ;
Connect ;
end;
with myq_m do
begin
Connection := con ;
SQL.Clear ;
SQL.Add('select * from customers') ;
open ;
first ;
for I := 0 to recordcount-1 do
begin
Result.Add(FieldByName('cus_name').AsString) ;
next ;
end;
Close ;
end;
con.Disconnect ;
con.Free ;
myq_m.Free ;
myq_d.Free ;
end;
//------------
exports F_cusname ;
end.
Code: Select all
var F_cusname: function : TStringList ;
st : TStringList ;
begin
st := TStringList.Create ;
DLLhandle := LoadLibrary(aacol.dll');
@F_cusname := GetProcAddress(DLLhandle, 'F_cusname');
st := F_cusname ;
for I := 0 to st.Count - 1 do ComboBox1.Items.Add(st[i]);
FreeLibrary(DLLhandle) ;
Show the error .

Posted: Tue 01 Dec 2009 09:05
by Dimon
The problem is arised because the exported "F_cusname" function returns a TStringList object. To solve the problem use a procedural type (like in the MyDAC DLL demo) instead of "function: TStringList"