Recent

Author Topic: DLL Problem  (Read 11330 times)

captian jaster

  • Guest
DLL Problem
« on: January 06, 2011, 05:01:35 pm »
This is the program:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. Uses
  6.   Classes,SysUtils;
  7.  
  8. Procedure PrintHello;External 'Lib.dll'name'PrintHello';
  9.  
  10.  
  11. begin
  12.   PrintHello;
  13.   Readln;
  14. end.
  15.  

This is the DLL:
Code: Pascal  [Select][+][-]
  1. library Lib;
  2.  
  3.  
  4. Procedure PrintHello;
  5. begin
  6.   Writeln('Hello World');
  7. end;
  8.  
  9. {$IFDEF WINDOWS}{$R Lib.rc}{$ENDIF}
  10.  
  11. begin
  12. end.
  13.  
  14. Export
  15.   PrintHello;
  16.  
It says "Failed to initialize program" (etc etc)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12025
  • Debugger - SynEdit - and more
    • wiki
Re: DLL Problem
« Reply #1 on: January 06, 2011, 05:17:29 pm »
Is that windows?
One possibility (there could be many more) would be the AntiVirus...

Happened to me once (same or similar error), BitDefender simply deletes some dll created by fpc.
Despite BitDefenders config being set to ask me, it deletes the file, with no warning.
There support team also never replied...

captian jaster

  • Guest
Re: DLL Problem
« Reply #2 on: January 06, 2011, 05:49:27 pm »
Is that windows?
One possibility (there could be many more) would be the AntiVirus...

Happened to me once (same or similar error), BitDefender simply deletes some dll created by fpc.
Despite BitDefenders config being set to ask me, it deletes the file, with no warning.
There support team also never replied...

Yeah its windows..
And I'm running MSE :\

captian jaster

  • Guest
Re: DLL Problem
« Reply #3 on: January 06, 2011, 08:46:12 pm »
I tried this code instead:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode Delphi}{$H+}
  4.  
  5. Uses
  6.   Classes,SysUtils,DynLibs;
  7.  
  8. Var
  9.  PrintHello:Procedure;
  10.  Handle:TLibHandle;
  11.  
  12.  
  13. begin
  14.   Handle := LoadLibrary('Lib.dll');
  15.   if Handle = NilHandle then Writeln('Failed to Load library..')Else begin
  16.     Writeln('Loaded Library! Assigning Procedures');
  17.     @PrintHello := GetProcAddress(Handle,'PrintHello');
  18.     Writeln('Got Procedure... Testing Procedure...');
  19.     Sleep(1000);
  20.     PrintHello;
  21.     Readln;
  22.   end;
  23. end.
  24.                        
  25.  
It raises an External SigSegV Error and then when the Debug report comes up its a bunch of ??????

Bart

  • Hero Member
  • *****
  • Posts: 5667
    • Bart en Mariska's Webstek
Re: DLL Problem
« Reply #4 on: January 06, 2011, 11:54:00 pm »
Maybe something like this?
(Untested code)

Code: [Select]
type
  THelloProc = Procedure;
var
  P: Pointer;
  PrintHello: THelloProc;

begin 
  Handle := LoadLibrary('Lib.dll'); 
  if Handle = NilHandle then Writeln('Failed to Load library..')
  Else
  begin 
    Writeln('Loaded Library! Assigning Procedures'); 
    P := GetProcAddress(Handle,'PrintHello'); 
    if (P <> nil) then  //you must check this!
    begin
      Writeln('Got Procedure... Testing Procedure...'); 
      PrintHello := THelloProc(P);
      Sleep(1000); 
      PrintHello; 
      Readln; 
    end
    else writeln('Unable to retriev adress of PrintHello from Lib.dll');
  end;
end;

Bart

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: DLL Problem
« Reply #5 on: January 07, 2011, 01:34:12 am »
Here you go. Your original code is wrong. The compiler will ignore everything after your program text is over(the last "end."). And export is different from exports. Export is a procedure modifier that doesn't have a whole lot to do with exports in this context :)
Code: Pascal  [Select][+][-]
  1. library TestLib;
  2.  
  3. Procedure PrintHello;
  4. begin
  5.    Writeln('Hello World');
  6. end;
  7.  
  8. Exports
  9.    PrintHello name 'PrintHello';
  10.  
  11. begin
  12. end.
  13.  

captian jaster

  • Guest
Re: DLL Problem
« Reply #6 on: January 07, 2011, 04:24:26 am »
Here you go. Your original code is wrong. The compiler will ignore everything after your program text is over(the last "end."). And export is different from exports. Export is a procedure modifier that doesn't have a whole lot to do with exports in this context :)
Code: Pascal  [Select][+][-]
  1. library TestLib;
  2.  
  3. Procedure PrintHello;
  4. begin
  5.    Writeln('Hello World');
  6. end;
  7.  
  8. Exports
  9.    PrintHello name 'PrintHello';
  10.  
  11. begin
  12. end.
  13.  
This worked.. Thanks..

Maybe something like this?
(Untested code)

Code: [Select]
type
  THelloProc = Procedure;
var
  P: Pointer;
  PrintHello: THelloProc;

begin 
  Handle := LoadLibrary('Lib.dll'); 
  if Handle = NilHandle then Writeln('Failed to Load library..')
  Else
  begin 
    Writeln('Loaded Library! Assigning Procedures'); 
    P := GetProcAddress(Handle,'PrintHello'); 
    if (P <> nil) then  //you must check this!
    begin
      Writeln('Got Procedure... Testing Procedure...'); 
      PrintHello := THelloProc(P);
      Sleep(1000); 
      PrintHello; 
      Readln; 
    end
    else writeln('Unable to retriev adress of PrintHello from Lib.dll');
  end;
end;

Bart
This code(Something like it) worked:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode Delphi}{$H+}
  4.  
  5. Uses
  6.   Classes,SysUtils,DynLibs;
  7.  
  8. Var
  9.  PrintHello:TProcedure;
  10.  Handle:TLibHandle;
  11.  P:Pointer;
  12.  
  13. begin
  14.   Handle := LoadLibrary('Lib.dll');
  15.   if Handle = NilHandle then Writeln('Failed to load library!')Else begin
  16.     Writeln('Got Library... Getting procedure');
  17.     P := GetProcAddress(Handle,'PrintHello');
  18.     if P <> Nil then
  19.     begin
  20.       Writeln('Got pointer.. Assigning...');
  21.       PrintHello := TProcedure(P);
  22.       Sleep(800);
  23.       PrintHello;
  24.     end Else Writeln('Failed to get procedure');
  25.   end;
  26.   Readln;
  27. end.  
  28.  
Thanks you guys :D

cdbc

  • Hero Member
  • *****
  • Posts: 2573
    • http://www.cdbc.dk
Re: DLL Problem
« Reply #7 on: January 08, 2011, 08:48:16 am »
Hi

P := GetProcAddress(Handle,'PrintHello'); 
if P <> Nil then PrintHello := TProcedure(P);
 
shorthand:

pointer(PrintHello):= GetProcAddress(Handle,'PrintHello');

Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Bart

  • Hero Member
  • *****
  • Posts: 5667
    • Bart en Mariska's Webstek
Re: DLL Problem
« Reply #8 on: January 09, 2011, 01:13:22 am »
B.t.w. Probably the PrintHello should be declared as stdcall in my previous example.

Code: [Select]
type
  THelloProc = Procedure; stdcall;
var
  P: Pointer;
  PrintHello: THelloProc;

Bart

cdbc

  • Hero Member
  • *****
  • Posts: 2573
    • http://www.cdbc.dk
Re: DLL Problem
« Reply #9 on: January 09, 2011, 10:48:45 am »
Hi

@Bart

And then again, maybe not. Iirc the default calling convention in delphi and fpc is "register", which means "stdcall" will mangle the parameters and result...

"register" is supposed to be the fastest way...?!?

Code: [Select]
library Lib; 
 
 
Procedure PrintHello; { NO stdcall defined here, ie. register }
begin 
  Writeln('Hello World'); 
end; 
 
{$IFDEF WINDOWS}{$R Lib.rc}{$ENDIF} 
 
begin 
end. 
 
Exports
  PrintHello;


Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Bart

  • Hero Member
  • *****
  • Posts: 5667
    • Bart en Mariska's Webstek
Re: DLL Problem
« Reply #10 on: January 09, 2011, 12:48:12 pm »
You might be rigth.
I simply don't know, nor did I test  8)

Bart

cdbc

  • Hero Member
  • *****
  • Posts: 2573
    • http://www.cdbc.dk
Re: DLL Problem
« Reply #11 on: January 09, 2011, 02:08:21 pm »
Hi.
@Cj
If you are interested, i could mail you a complete project with libraries and host application. Might inspire you, with some techniques...
Regards - Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

 

TinyPortal © 2005-2018