Recent

Author Topic: Exception SIGSEGV in showing form from DLL  (Read 4007 times)

Sergio1969

  • Newbie
  • Posts: 2
Exception SIGSEGV in showing form from DLL
« on: September 05, 2012, 11:10:19 am »
Hi.
I wrote a library that exposes a procedure called "Crea" that creates and shows a form. The code of library is:


library libreria;

{$mode objfpc}{$H+}

uses
  Sharemem,
  Classes,
  Forms,
  Interfaces;

procedure Crea;stdcall;
begin
  with TForm.CreateNew(nil) do
    begin
      Top:=100;
      Left:=100;
      Width:=100;
      Height:=100;
      Show;
    end;
end;

exports
  Crea;

begin
end.



Then, I wrote a usually application that invokes the procedure "Crea":



unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  StdCtrls, ExtCtrls, DynLibs;

type
  TCrea=procedure;stdcall;

  { TForm1 }

  TForm1 = class(TForm)
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button2Click(Sender:TObject);
var HandleLibreria:TLibHandle;
    Crea:TCrea;
begin
  HandleLibreria:=LoadLibrary('libreria.dll');
  if (HandleLibreria<>0) then
    begin
      Crea:=TCrea(GetProcedureAddress(HandleLibreria,'Crea'));
      Crea;
    end;
end;

end.



(The project have the unit "Sharemem" as first unit).

Well, when I run the application, the "Crea" procedure is correctly executed. But, when the instruction "Show" in the "Crea" library procedure is executed, I get the external "SIGSEGV" exception.
Can anyone help me?
My operative system is Windows XP.
Thanks in advance.

Sergio.
« Last Edit: September 05, 2012, 11:20:35 am by Sergio1969 »

Sergio1969

  • Newbie
  • Posts: 2
Re: Exception SIGSEGV in showing form from DLL
« Reply #1 on: September 06, 2012, 02:36:04 pm »
I find the solution!
I put it here for anyone that in the future can meet the same trouble.
The solution is calling the Initialize method of the Application object in the body of library:

Code: [Select]
library Lib;
//...other stuffs
begin
  Application.Initialize;
end.

Doing so, the form is correctly showed errorless!

Bye.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Exception SIGSEGV in showing form from DLL
« Reply #2 on: September 06, 2012, 03:37:21 pm »
Thanks for posting the solution you found, Sergio!
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018