SIGSEGV after calling a DLL lazarus / 21-General ---------------------------------------------------------------------- Mike_HDF · 2021-01-27 14:51 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: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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! ---------------------------------------------------------------------- jamie · 2021-01-27 18:03 U can't use constant , use a uniquestring for return ---------------------------------------------------------------------- MarkMLl · 2021-01-27 18:14 Is this really safe? I thought that in the general case FPC couldn't call C++, only C. MarkMLl ---------------------------------------------------------------------- ASerge · 2021-01-27 18:15 Quote from: Mike_HDF on January 27, 2021, 03:51:46 pm void WINAPI GetBaseDirectory(LPSTR AppDir) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetTheAppDir(var AppDir : LPSTR) : LONG; stdcall; external 'ChkEnv' name 'GetBaseDirectory'; The definitions are different. Need Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure GetTheAppDir(AppDir: LPSTR); stdcall; external 'ChkEnv' name 'GetBaseDirectory'; ---------------------------------------------------------------------- Mike_HDF · 2021-01-27 18:32 Quote from: jamie on January 27, 2021, 07:03:29 pmU can't use constant , use a uniquestring for return Thanks for the reply. Not sure about what you are saying (I use Lazarus from time to time for little projects), tried to use UniqueString but got the crash again after the event. ---------------------------------------------------------------------- Mike_HDF · 2021-01-27 18:42 Quote from: MarkMLl on January 27, 2021, 07:14:48 pmIs this really safe? I thought that in the general case FPC couldn't call C++, only C. MarkMLl Hello, MarkMLI, Oh, I said it's C++ and problaby I made a mistake since all sources are .c and not .cpp. ---------------------------------------------------------------------- Mike_HDF · 2021-01-27 19:08 Quote from: ASerge on January 27, 2021, 07:15:48 pmQuote from: Mike_HDF on January 27, 2021, 03:51:46 pm void WINAPI GetBaseDirectory(LPSTR AppDir) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetTheAppDir(var AppDir : LPSTR) : LONG; stdcall; external 'ChkEnv' name 'GetBaseDirectory'; The definitions are different. Need Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure GetTheAppDir(AppDir: LPSTR); stdcall; external 'ChkEnv' name 'GetBaseDirectory'; Oh, thanks, I always make mistakes like that, but doesn't solve my problem: the moment I put stdcall it crashes before firing the double click event. I tried another function of the DLL and commented the previous one: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function SPFileOpen(FileName : LPSTR) : HANDLE; stdcall; external 'ChkEnv' name 'SPFileOpen';  I tested using a wrong file path and a correct one and it returns the values expected. But it crashes later and seems related to Strings (for example: ShowMessage('Any string')). Don't know what to do, I'll try the code in another computer. ---------------------------------------------------------------------- 440bx · 2021-01-27 19:42 It's not surprising your code doesn't work.  What's surprising is that it doesn't fail much sooner. First, what @Serge mentioned is really the first step.  Get the definition right.  Any C function that returns "void" is a Pascal procedure not a function.  (though in this particular case, it wouldn't cause an access violation.) The statement in your code:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i i := GetTheAppDir(s);doesn't make any sense because a function that returns void isn't returning a value (function result.)  The variable "i" in your code is getting whatever was left in eax/rax (depending on bitness.) Second, the statement in your code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  s := PChar('      ');the variable "s" ends up being a pointer to a constant in a read-only segment (because of $H+)  The reason it doesn't crash there is because the function parameter is declared as "var" this cause a pointer to "s" (which is in a read-write segment) to be passed to the dll function. Your declaration of "s" should be something like : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var s : packed array[0..MAXPATH] of char and declare the parameter to GetTheAppDir as a pchar (instead of a "var" to an "LPSTR") basically the definition @Serge posted but with "pchar" instead of "LPSTR" (LPSTR is most likely equated to pchar.  "Unhiding" the parameter type is always nice.) ---------------------------------------------------------------------- Mike_HDF · 2021-01-27 21:30 Quote from: 440bx on January 27, 2021, 08:42:29 pmIt's not surprising your code doesn't work.  What's surprising is that it doesn't fail much sooner. First, what @Serge mentioned is really the first step.  Get the definition right.  Any C function that returns "void" is a Pascal procedure not a function.  (though in this particular case, it wouldn't cause an access violation.) The statement in your code:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i i := GetTheAppDir(s);doesn't make any sense because a function that returns void isn't returning a value (function result.)  The variable "i" in your code is getting whatever was left in eax/rax (depending on bitness.) Second, the statement in your code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  s := PChar('      ');the variable "s" ends up being a pointer to a constant in a read-only segment (because of $H+)  The reason it doesn't crash there is because the function parameter is declared as "var" this cause a pointer to "s" (which is in a read-write segment) to be passed to the dll function. Your declaration of "s" should be something like : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var s : packed array[0..MAXPATH] of char and declare the parameter to GetTheAppDir as a pchar (instead of a "var" to an "LPSTR") basically the definition @Serge posted but with "pchar" instead of "LPSTR" (LPSTR is most likely equated to pchar.  "Unhiding" the parameter type is always nice.) Thanks, 440bx, You're right, The packed array declaration and the other things you mention fix the problem. I still have a lot to learn. ---------------------------------------------------------------------- jamie · 2021-01-27 21:47 That functions/Procedure obviously isn't structured very well, it should have a length parameter and return the actual length... but in any case..... For such brain dead functions.. Var   S:String; Begin  SetLength(S, MaxPath);  GetBaseFileNameORwhatEver(Pchar(S));  SetLength(S, Pos(#0, S)); End;  Now S contains the data/././ ---------------------------------------------------------------------- 440bx · 2021-01-28 03:45 Quote from: Mike_HDF on January 27, 2021, 10:30:10 pmThanks, 440bx, You're right, The packed array declaration and the other things you mention fix the problem. I still have a lot to learn. You're welcome.  As far as learning, that's often accomplished by making mistakes and asking questions, IOW, you're on the right track. ---------------------------------------------------------------------- PascalDragon · 2021-01-28 08:09 Quote from: MarkMLl on January 27, 2021, 07:14:48 pmIs this really safe? I thought that in the general case FPC couldn't call C++, only C. As long as the mangled name as well as the calling convention matches, nothing speaks against calling C++ code. Considering that linking worked (and it would indeed be a C++ library, not a C one) I assume the function is exported as extern "C" which uses a C mangled name. And as long as no class instances are involved C++ simply uses the standard C calling convention by default. In the past I have already successfully worked with static C++ functions by specifying their mangled name in the name-clause.