Hi, all,
I have a DLL written in C++ with a function defined this way:
void WINAPI GetBaseDirectory(LPSTR AppDir)
I'm trying to use it in a form application but it crash with the SIGSEGV error.
This is the full code of the main unit:
unit frmMain;
{$mode objfpc}{$H+}
{$calling stdcall}
interface
uses
Windows, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
Tfrm = class(TForm)
lblDir: TLabel;
procedure lblDirDblClick(Sender: TObject);
private
public
end;
var
frm: Tfrm;
implementation
function GetTheAppDir(var AppDir : LPSTR) : LONG; stdcall; external 'ChkEnv' name 'GetBaseDirectory';
{$R *.lfm}
{ Tfrm }
procedure Tfrm.lblDirDblClick(Sender: TObject);
var
i : LONG;
s : LPSTR;
begin
s := PChar(' ');
i := GetTheAppDir(s);
if i <> 0 then begin
lblDir.Caption := Trim(String(s));
end else begin
lblDir.Caption := 'Program not installed';
end;
end;
end.
The error happens in this line:
lblDir.Caption := 'Program not installed';
Wich is really weird, because that means that the DLL call was ok and the problem should be elsewhere but can't see where.
I'm using Lazarus v 2.0.0 with FPC 3.0.4.
Any ideas?
Thank you.
-----------------------------
Edit: I did some tests. If I have this:
procedure Tfrm.lblDirDblClick(Sender: TObject);
begin
lblDir.Caption := 'Program not installed';
end;
And comment the uses Windows and function definition, leaving the directive:
{$calling stdcall}
It crash too. If I comment the directive and comment out the function, it crashes again; if I delete stdcall from the function definition it works but still gets an access violation (on closing the form when debugging, after the double click event with the compiled exe).
I thought the function needed the stdcall...
My head explodes!