* * *

Author Topic: ShowMessage in a DLL ?  (Read 11492 times)

KlausGunther

  • New member
  • *
  • Posts: 36
ShowMessage in a DLL ?
« on: December 28, 2012, 10:20:42 pm »
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

  • Jr. Member
  • **
  • Posts: 94
Re: ShowMessage in a DLL ?
« Reply #1 on: December 29, 2012, 01:04:18 am »
I think no is posible, but you can raise a exception.

Code: [Select]
Raise Exception.Create('Error here');

KlausGunther

  • New member
  • *
  • Posts: 36
Re: ShowMessage in a DLL ?
« Reply #2 on: December 29, 2012, 01:10:46 am »
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

  • Hero Member
  • *****
  • Posts: 7958
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: ShowMessage in a DLL ?
« Reply #3 on: December 29, 2012, 06:14:17 am »
Quote
There might be more USES clauses than strictly required for this
Nope, in fact you write too many, kick that Windows (and LCLType) out of the uses clause :P

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

  • New member
  • *
  • Posts: 36
Re: ShowMessage in a DLL ?
« Reply #4 on: December 29, 2012, 10:23:00 am »
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

  • Jr. Member
  • **
  • Posts: 68
Re: ShowMessage in a DLL ?
« Reply #5 on: December 29, 2012, 11:02:37 am »
For Fatal: Identifier not found: "Application", place Forms on uses.
Lazarus + FPC Trunk
Windows 10 x64

KlausGunther

  • New member
  • *
  • Posts: 36
Re: ShowMessage in a DLL ?
« Reply #6 on: December 29, 2012, 11:43:34 am »
@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

  • Newbie
  • Posts: 3
Re: ShowMessage in a DLL ?
« Reply #7 on: September 23, 2013, 02:49:52 am »
Acá esta la solución definitiva para LAZARUS. 8)
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. O:-)

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.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus