Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-01

dll with windows impossible???

15 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michel Meunier

Hello,
I am trying again without any hope, to use dll in a wondows application. It's very strange it's working with console application but not with windows apllication.
My dll code is very very simple:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
library dlltest;

{$mode objfpc}{$H+}

uses
Classes

  { add your units here };
function test ( ) : integer ; stdcall ;
begin
test:=1;
end ;
exports
test ;
end .
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

next the console program wich is working, very simple: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
program testdll;

{$mode objfpc}{$H+}
uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes,dynlibs
  { add your units here };
var handledll:integer;
begin
handledll:=loadlibrary('dlltest.dll');
if handledll=0 then writeln('raté') else writeln('OK');
writeln(handledll);
end.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
now the windows program:
main part:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
program testdllwindows;
{$mode objfpc}{$H+}

uses

  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Interfaces, // this includes the LCL widgetset
  Forms
  { add your units here }, testdllwin;
begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
and the unit:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
unit testdllwin;
{$mode objfpc}{$H+}
interface

uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Buttons,dynlibs;

type
  { TForm1 }
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
var
  Form1: TForm1;
implementation

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
 var handledll:integer;
begin
handledll:=loadlibrary('dlltest.dll');
if handledll=0 then showmessage('raté') else showmessage('OK');
end;
initialization
  {$I testdllwin.lrs}

end.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx and IT DOESN'T WORK, why? It is exactly the same code! More and more I think I am going to translate my code in C, because FPC seems unable to manage properly dll library.
Thanks for your help.

Michel

Michel Meunier Web: www.etoiles-a-bleau.fr

Reply by email

Felipe Monteiro de Carvalho
to Michel Meunier

I tested here and everything worked fine on the Application with a Form.

Further, I´ve written dozens of DLLs with Free Pascal and all worked fine.

My guess is that there is something wrong with your Setup.

What Windows version are you using?

What FPC and Lazarus versions are you using?

Maybe your download was slightly corrupted, you could try to remove your fpc / lazarus install and do a free install with the latest *stable* version.

Or else, you should try the obvious. Is the DLL on the same folder as the executable?

How are you executing the application?

Felipe Monteiro de Carvalho

Reply by email

Flávio Etrusco
to Michel Meunier: I am trying again without any hope, to use dll in a wondows application. It's very…
Show quoted text 5 lines
I am trying again without any hope, to use dll in a wondows application. (...) More and more I think I am going to translate my code in C, because FPC seems unable to manage properly dll library. Thanks for your help.

Do you think whining and shouting "I will turn to C" will get you better replies than otherwise?

Regards,
Flávio

Reply by email

Michael Van Canneyt
to Michel Meunier: I am trying again without any hope, to use dll in a wondows application. It's very…

On Wed, 24 Jan 2007, Michel Meunier wrote:

[cut code]

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx and IT DOESN'T WORK, why? It is exactly the same code! More and more I think I am going to translate my code in C, because FPC seems unable to manage properly dll library.

It has nothing to do with FPC.

1. Compile the DLL without debug info.
(lazarus compiles it by default with debug info included. Disable that)

2. ALWAYS specify the complete path to the DLL. Do NOT assume that the program is started with as the current directory the program directory. This is a common mistake.

If you are still experiencing problems after that, mail me in private.

Michael.

Reply by email

Michel Meunier
to Felipe Monteiro de Carvalho: I tested here and everything worked fine on the Application with a Form. Further, I´ve…
Felipe Monteiro de Carvalho wrote 8 lines
I tested here and everything worked fine on the Application with a Form. Further, I´ve written dozens of DLLs with Free Pascal and all worked fine. My guess is that there is something wrong with your Setup. What Windows version are you using?

Windows XP with SP2

What FPC and Lazarus versions are you using?

Lazarus-0.9.20
fpc-2.0.4-20061105
the latest stable version.

Show quoted text 6 lines
Maybe your download was slightly corrupted, you could try to remove your fpc / lazarus install and do a free install with the latest *stable* version. Or else, you should try the obvious. Is the DLL on the same folder as the executable?

yes of course

How are you executing the application?

inside Lazarus, and also outside Lazarus

Best regards

Michel Meunier www.etoiles-a-bleau.fr

Reply by email

Michel Meunier
to Flávio Etrusco: Do you think whining and shouting "I will turn to C" will get you better replies than…
Flávio Etrusco wrote 8 lines
> I am trying again without any hope, to use dll in a wondows application. > (...) > More and more I think I am going to translate my code in C, because FPC > seems unable to manage properly dll library. > Thanks for your help. Do you think whining and shouting "I will turn to C" will get you better replies than otherwise?

Absolutly NO, but my problem is to have a reliable compiler to work properly, not to run after strange problems.

Show quoted text 8 lines
Regards, Flávio _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Michel Meunier www.etoiles-a-bleau.fr

Reply by email

Michel Meunier
to Michael Van Canneyt: [cut code] It has nothing to do with FPC. 1. Compile the DLL without debug info. (lazarus…
Michael Van Canneyt wrote 15 lines
On Wed, 24 Jan 2007, Michel Meunier wrote: [cut code] > xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx > and IT DOESN'T WORK, why? It is exactly the same code! > More and more I think I am going to translate my code in C, because FPC seems > unable to manage properly dll library. > It has nothing to do with FPC. 1. Compile the DLL without debug info. (lazarus compiles it by default with debug info included. Disable that)

it's the case

Show quoted text 5 lines
2. ALWAYS specify the complete path to the DLL. Do NOT assume that the program is started with as the current directory the program directory. This is a common mistake.

Ok, I have tried, and the problem is the same :-(

Show quoted text 9 lines
If you are still experiencing problems after that, mail me in private. Michael. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Michel Meunier www.etoiles-a-bleau.fr

Reply by email

Michael Van Canneyt
to Michel Meunier
Michel Meunier wrote 24 lines
Michael Van Canneyt a écrit : > On Wed, 24 Jan 2007, Michel Meunier wrote: > > [cut code] > > > xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx > > and IT DOESN'T WORK, why? It is exactly the same code! > > More and more I think I am going to translate my code in C, because FPC > > seems > > unable to manage properly dll library. > > > > It has nothing to do with FPC. > > 1. Compile the DLL without debug info. > (lazarus compiles it by default with debug info included. Disable that) > it's the case > 2. ALWAYS specify the complete path to the DLL. Do NOT assume that > the program is started with as the current directory the program > directory. This is a common mistake. > > Ok, I have tried, and the problem is the same :-(

1. What is the return value of LoadLibrary ? Have you checked it ?

2. If it is 0 immediatly get the last error: raiselastoserror. This should give you at least a hint why windows thinks it can't load your library.

3. Did you specify $APPTYPE GUI in the library as well ? If not, do so. Otherwise the library will try to initialize the terminal, when it cannot: there is none, since the loading program does not have one!

4. You can also try vice versa: specify apptype console in your GUI program. The GUI will still run, but in addition a terminal will be opened, allowing the library to initialize it.

Can you test all this, please ?

Michael.

Reply by email

Bisma Jayadi
to Michel Meunier: Absolutly NO, but my problem is to have a reliable compiler to work properly, not to run…
Absolutly NO, but my problem is to have a reliable compiler to work properly, not to run after strange problems.

It's completely non sense to blame your compiler before do some deep investigations upon your problem. Many people have used FPC to build dynamic libraries without problems and work perfectly, both console or GUI program. It could be caused by your own mistake!

-Bee-

has Bee.ography at:
http://beeography.wordpress.com

Reply by email

Michel Meunier
to Michael Van Canneyt: 1. What is the return value of LoadLibrary ? Have you checked it ? 2. If it is 0…
Michael Van Canneyt wrote 36 lines
On Wed, 24 Jan 2007, Michel Meunier wrote: > Michael Van Canneyt a écrit : > >> On Wed, 24 Jan 2007, Michel Meunier wrote: >> >> [cut code] >> >> >>> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx >>> and IT DOESN'T WORK, why? It is exactly the same code! >>> More and more I think I am going to translate my code in C, because FPC >>> seems >>> unable to manage properly dll library. >>> >>> >> It has nothing to do with FPC. >> >> 1. Compile the DLL without debug info. >> (lazarus compiles it by default with debug info included. Disable that) >> >> > it's the case > >> 2. ALWAYS specify the complete path to the DLL. Do NOT assume that >> the program is started with as the current directory the program >> directory. This is a common mistake. >> >> >> > Ok, I have tried, and the problem is the same :-( > 1. What is the return value of LoadLibrary ? Have you checked it ?

the return value is 0

2. If it is 0 immediatly get the last error: raiselastoserror. This should give you at least a hint why windows thinks it can't load your library.

when I make a test with Lazarus, I get this message: project raises exception class 'External: SIGSEGV' and next, in another window:

execution stopped
Adress: $7C92E183
procedure: ntdll!LdrFindRessource_U
File:

And I have also replaced my 'testdll.dll' by a dll compiled with Delphi and next renamed 'testdll.dll', and it's ok, so the problem is with the library compile with FPC.

3. Did you specify $APPTYPE GUI in the library as well ? If not, do so. Otherwise the library will try to initialize the terminal, when it cannot: there is none, since the loading program does not have one!

I have added:
{$APPTYPE GUI}
just after 'library dlltest;' in the code and no change.

4. You can also try vice versa: specify apptype console in your GUI program. The GUI will still run, but in addition a terminal will be opened, allowing the library to initialize it.

I have added: {$APPTYPE CONSOLE} in the GUI program to test the dll, and no change.

Can you test all this, please ? Michael.

OUPS, i have found the solution, in the linking part, the option -gl was set, it's difficult to translate in english, but it's to insert the number of lines in case of debug. And now it's working!! Whaou, thanks a lot for your help.

Michel Meunier Web: www.etoiles-a-bleau.fr

Reply by email

Michael Van Canneyt
to Michel Meunier
Michel Meunier wrote 27 lines
Michael Van Canneyt a écrit : > On Wed, 24 Jan 2007, Michel Meunier wrote: > > > > Michael Van Canneyt a écrit : > > > > > On Wed, 24 Jan 2007, Michel Meunier wrote: > > > > > > [cut code] > > > > > > > > > > xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx > > > > and IT DOESN'T WORK, why? It is exactly the same code! > > > > More and more I think I am going to translate my code in C, because > > > > FPC > > > > seems > > > > unable to manage properly dll library. > > > > > > > > > > > It has nothing to do with FPC. > > > > > > 1. Compile the DLL without debug info. > > > (lazarus compiles it by default with debug info included. Disable > > > that) > > > > > > > > it's the case

As you discovered, it was not the case: -gl is also debug info.

This is a known problem, and is already in the bugtracker if I remember correctly.

Michael.

Reply by email

Felipe Monteiro de Carvalho
to Michael Van Canneyt
Michael Van Canneyt wrote
As you discovered, it was not the case: -gl is also debug info. This is a known problem, and is already in the bugtracker if I remember correctly.

Strangly, it works here with -gl set.

Felipe Monteiro de Carvalho

Reply by email

Michel Meunier
to Bisma Jayadi: It's completely non sense to blame your compiler before do some deep investigations upon…
Bisma Jayadi wrote 7 lines
> Absolutly NO, but my problem is to have a reliable compiler to work > properly, not to run after strange problems. It's completely non sense to blame your compiler before do some deep investigations upon your problem. Many people have used FPC to build dynamic libraries without problems and work perfectly, both console or GUI program. It could be caused by your own mistake!

That's right, but as you can see, it's a problem with the option -gl.

Show quoted text 11 lines
-Bee- has Bee.ography at: http://beeography.wordpress.com _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Michel Meunier Web: www.etoiles-a-bleau.fr

Reply by email