Page 1 of 1

dll loading and "Access violation in module libmysql.dl

Posted: 05.09.2008, 08:37
by B@RT
I dynamically ro statistical load dll in which I create connection to mysql. The library works normally, but at an exit from the program (unload my dll) I receive an error message Access violation in module libmysql.dll

i use delphi 2007, zeos 6.6.3, mysql-5.1.24-rc, windows xp sp2 (libmysql.dll copy from mysql\mysql server 5.1\bin to project.exe folder)

Sorry for my bad english....

test dll code:

Code: Select all

library test_mysql;

uses
  ShareMem,
  SysUtils,
  Classes,
  Variants,
  ZConnection,
  ZDataset;

var
  oldapp:thandle;
  ZConnection: TZConnection;
  ExecQuery:TZQuery;

function ConnectToDB:boolean;stdcall;
begin   
result:=false;

ZConnection:=tZConnection.Create(application);
ZConnection.User:='root';
ZConnection.Password:='';
ZConnection.Port:=3306;
ZConnection.Protocol:='mysql';
ZConnection.Database:='mybase';
ZConnection.Properties.Add('codepage=cp1251');
ZConnection.HostName:='localhost';

try
  try
    ZConnection.Connect;
  finally
    ExecQuery:=TZQuery.Create(application);
    ExecQuery.Connection:=ZConnection;
    result:=true;
  end;
except
  ZConnection.free;
  result:=false;
  exit;
end;
end;

procedure LoadDLL(app:THandle);stdcall;
begin
oldapp:=application.Handle;
application.Handle:=app;
end;


procedure UnloadDLL;stdcall;
begin
application.Handle:=oldapp;
end;

function test:string;stdcall;
var r:string;
begin
if ZConnection.Connected=false then exit;

ExecQuery.SQL.Clear;
ExecQuery.SQL.Add('SELECT name FROM mybase.test');
ExecQuery.Open;
if VarIsNull(ExecQuery.FieldValues['name']) then r:='' else
r:=ExecQuery.FieldValues['name'];
result:=r;
ExecQuery.Close;
end;

exports ConnectToDB,UnloadDLL,LoadDLL,test;
  
end.
program code (static load):

Code: Select all

unit Unit1;

interface
..........................
function ConnectToDB:boolean; stdcall; external 'test_mysql.dll';
procedure LoadDLL(app:thandle); stdcall; external 'test_mysql.dll';
procedure UnloadDLL; stdcall; external 'test_mysqld.dll';
function test:string; stdcall; external 'test_mysql.dll';

var
  Form1: TForm1;

implementation
............................
procedure TForm1.FormCreate(Sender: TObject);
begin
LoadDll(application.handle);
ConnectToDB;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnloadDLL;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMesasge(test);
end;

end.

Posted: 05.09.2008, 12:43
by mdaems
Hi B@rt,

Don't you have to free the memory assigned to the connection and zquery when unloading the library? I must admit I know nothing about writing dll's. So it may have no impact on this.

Question : did you copy/paste these sources? If yes you had some typo in procedure UnloadDLL; stdcall; external 'test_mysqld.dll';

Mark

Posted: 05.09.2008, 14:12
by B@RT
I tried to add in procedure UnloadDll zquery.free and ZConnection.free, the error all the same takes place.

It is changed to "copy/paste" and letter D - a typing error (original dll name "sql_cmd.dll")

Posted: 06.09.2008, 10:22
by B@RT
It is already old bug what ideas of the decision of a problem are?
http://bugs.mysql.com/bug.php?id=5062

And here advise to start winsock before initialization mysql the client
http://bugs.mysql.com/bug.php?id=4039

This question already set, but have not solved?
http://zeos.firmos.at/viewtopic.php?t=617&highlight=dll

and (in dll not work)
http://zeos.firmos.at/viewtopic.php?t=738&highlight=dll

Posted: 06.09.2008, 11:02
by mdaems
Can you please log it as a bug report at zeosbugs.firmos.at ? Here we'll forget about it again. Include a link to this thread.

Mark

Posted: 07.09.2008, 16:27
by B@RT

Posted: 17.05.2009, 04:42
by TheNoobs
I tried to access zconnection in dll from program application
It successfully load, but at an exit from the program, it gives elisterror and memory could not be read.

Code: Select all

library Project1;

uses
  Forms,
  SysUtils,
  Classes,
  Variants,
  ZConnection,
  ZDataset;

var
  oldapp:thandle;
  fZConnection: TZConnection;

function ConnectToDB:boolean;stdcall;
begin
  fZConnection:=tZConnection.Create(application);
  fZConnection.User:='sysdba';
  fZConnection.Password:='masterkey';
  fZConnection.Port:=0;
  fZConnection.Protocol:='firebird-1.5';
  fZConnection.Database:='TEST.GDB';
  fZConnection.HostName:='localhost';
  fZConnection.TransactIsolationLevel := tiReadCommitted;

  try
    try
      fZConnection.Connect;
    finally
      result:=true;
    end;
  except
    fZConnection.free;
    result:=false;
    exit;
  end;
end;

procedure LoadDLL(app:THandle);stdcall;
begin
  oldapp:=application.Handle;
  application.Handle:=app;
end;


procedure UnloadDLL;stdcall;
begin
  application.Handle:=oldapp;
  fZConnection.Free;
end;

function getConnection: TZConnection;
begin
  result := fZConnection;
end;


exports ConnectToDB,UnloadDLL,LoadDLL,getConnection;


begin

end.

Code: Select all


function getConnection: TZConnection; stdcall; external 'project1.dll';

  ZReadOnlyQuery1 := TZReadOnlyQuery.Create(self);
  ZReadOnlyQuery1.Connection := getConnection;
  ZReadOnlyQuery1.SQL.Text := 'select * from putang';
  ZReadOnlyQuery1.ExecSQL;
  ZReadOnlyQuery1.Open;
  ZReadOnlyQuery1.Close;
  ZReadOnlyQuery1.Connection := nil;
  ZReadOnlyQuery1.Free;