ShowMessage in a DLL ? lazarus / 34-Windows ---------------------------------------------------------------------- KlausGunther · 2012-12-28 21:20 How can I do a ShowMessage within a DLL ? Let a sample DLL code be: Code: [Select]library KGF; {$mode delphi}{$H+} uses   Interfaces,   Windows,   Dialogs,   SysUtils,   LCLType,   Classes, function test():integer; stdcall; export; begin         showmessage('a');         result := 18; end; exports   test ; begin end.    There might be more USES clauses than strictly required for this, but there are used in a more complete version. This is the minimum code to produce the problem. A call to my test function produces an access violation with mention "read of address 00000006". How can I get this to work ? ---------------------------------------------------------------------- otorres · 2012-12-29 00:04 I think no is posible, but you can raise a exception. Code: [Select]Raise Exception.Create('Error here'); ---------------------------------------------------------------------- KlausGunther · 2012-12-29 00:10 Oh, I shure can raise an exception. But why couldn't I do it ? I can do in in Delphi 6 Personal Edition (and probably in all newer Delphi versions). I'm converting a Delphi 6 DLL to Lazarus/FPC, because I want to port it to Windows 64 bit environment. This just works fine, but some little points are blocking me, and this is one of them. Has this never been done ? I did a lot og googlingn prior to posting this message. I found indeed many posts telling that for this special action, there are bugs, impossibilities... but those posts are fairly old (4, 5 or 6 years back), and I hoped this restriction could have been removed since then. ---------------------------------------------------------------------- Leledumbo · 2012-12-29 05:14 QuoteThere might be more USES clauses than strictly required for thisNope, in fact you write too many, kick that Windows (and LCLType) out of the uses clause What you need is Application.Initialize somewhere before the ShowMessage line. You can do it in the initialization section, or locally before ShowMessage (followed by Application.Terminate). A DLL has its own set of linked library, not shared with the calling application. ---------------------------------------------------------------------- KlausGunther · 2012-12-29 09:23 I now have this: Code: [Select]library FPC_showmessage; {$mode delphi}{$H+} uses   Interfaces,   Dialogs,   SysUtils,   Classes; function test():integer; stdcall; export; begin         showmessage('a');         result := 18; end; exports   test ; begin   Application.Initialize; end.      [code] The compiler says:  FPC_showmessage.lpr(22) Fatal: Identifier not found: "Application" I'm still missing something. ---------------------------------------------------------------------- Fahmy Rofiq · 2012-12-29 10:02 For Fatal: Identifier not found: "Application", place Forms on uses. ---------------------------------------------------------------------- KlausGunther · 2012-12-29 10:43 @Leledumbo et Famy Rofiq: Thank you both ! It's working ! OK. Now, I can go ahead with my conversion. There is a Delphi DLL with over 10,000 lines of code, and several others, a bit smaller. Somewhat less then 200 functions, covering numerous aspects, including my own relational database, keyboard and mouse hooks, inter-process communication and manu others. I try to port all this to Lazarus/FPC, because that's my only mean to cross-compile for a Windows 64 bit environment. Thank you again for your help - I really appreciate it ! ---------------------------------------------------------------------- cokocool · 2013-09-23 00:49 Acá esta la solución definitiva para LAZARUS. Hay algunos ejemplos para Delphi pero no me corren bien en Lazarus. Les dejo el código de la librería y también del formulario con dos botones. Espero les sea de ayuda. Código de la DLL: ############################################################## library MiLibreria; {$mode objfpc}{$H+} uses   Dialogs ,Interfaces  //para uso de los ShowMessage   ,sysutils, Forms   function MiFuncion(x: double; y: PChar): PChar; stdcall; var   s :ansistring;   //s: string; //puede ser asi tambien begin    s := 'Hello ' + FloatToStr(x) + ' ' + y + '!';   result := PChar(s); end; procedure Mensajito; stdcall; begin     ShowMessage('Hola mundo!'); end; exports      MiFuncion, Mensajito; begin       Application.Initialize; end. ############################################################## Código del Form con dos botones: ############################################################## unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)       Button1: TButton;       Button2: TButton;       procedure Button1Click(Sender: TObject);       procedure Button2Click(Sender: TObject);   private     { private declarations }   public     { public declarations }   end; var   Form1: TForm1;   procedure Mensajito; external 'MiLibreria.dll';   function MiFuncion(x: double; y: PChar): PChar; external 'MiLibreria.dll'; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject); begin   ShowMessage(Mifuncion(23.54, 'Jorge')); end; procedure TForm1.Button2Click(Sender: TObject); begin     Mensajito; end; end. ############################################################## Saludos.