Autor Beitrag
breakdancer1111
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 148

Windows 7 Home Premium 64Bit
Delphi XE2 Starter
BeitragVerfasst: Mo 22.08.05 09:03 
Hallo!

Ich lade eine selbst erstelle Dll Dynamisch in Delphi und möchte
eine procedure ausführen. Dafür habe ich folgendes gemacht. ;)

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
type
  TSampleDll = procedure;

var 
  Dll: TSamlpeDll = nil;

...

var Handle: THandle;
    r: real;
begin
  Handle := LoadLibrary(PChar(ExtractFilePath(ParamStr(0))+'sample.dll'));
  @PlugIn := GetProcAddress(Handle, 'Sampleprocedure');
  if @PlugIn <> nil then PlugIn;
  FreeLibrary(Handle);

  r := handle;
  Caption := FloatToStr(r); // Nur als Test
end;


Wenn ich das nun ausführe, wird mir in der Form Caption 0 angezeigt.
Das heißt doch mit anderen Worten, dass meine procedure nicht gefunden
wurde... aber warum?
MrSaint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1033
Erhaltene Danke: 1

WinXP Pro SP2
Delphi 6 Prof.
BeitragVerfasst: Mo 22.08.05 09:13 
user profile iconbreakdancer1111 hat folgendes geschrieben:
Wenn ich das nun ausführe, wird mir in der Form Caption 0 angezeigt.
Das heißt doch mit anderen Worten, dass meine procedure nicht gefunden
wurde... aber warum?


Falsch. Er konnte die DLL nicht laden! Handle ist doch das Handle zur geladenen DLL. Wenn das 0 ist, hat da was nicht geklappt! Und wenn die DLL nicht geladen ist, kann er die Procedure auch nicht finden ;)



MrSaint

_________________
"people knew how to write small, efficient programs [...], a skill that has subsequently been lost"
Andrew S. Tanenbaum - Modern Operating Systems
rochus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 416

Win XP Prof, Fedora Core 4, SuSE 7.0
D7 Ent, D2005 Pers
BeitragVerfasst: Mo 22.08.05 12:46 
Schon mal aufgefallen, was du da machst?

ausblenden Delphi-Quelltext
1:
2:
3:
  Handle := LoadLibrary(...);
  FreeLibrary(Handle);
  r := Handle; { library wurde schon geschlossen!!! }


das sollte eventuell:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
  Handle := LoadLibrary(...);
  r := Handle;
  FormName.Caption := FloatToStr(r);
  FreeLibrary(Handle);


sein. ;)

Grund: nach FreeLibrary ist dein Handle nicht mehr gültig und besitzt den Wert 0. Daher kommt der. Du kannst ja prüfen, ob deine Sachen alle geladen werden. schreib dir ne funktion, die dir deine aufrufe mitloggt (irgendwohin. Memo, Textdatei, was weiß ich):

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
  Handle := LoadLibrary(...);
  if Handle=0 then begin
    LogMessage('failed when loading library');
  end else begin
    LogMessage('library successfully loaded');
    { retrieving proc address }
    @Plugin := GetProcAddress(Handle, 'Sampleprocedure');
    if @Plugin <> nil then begin
       Plugin;
       LogMessage('successfully called procedure »Sampleprocedure«');
    end else begin
       LogMessage('failed to call »Sampleprocedure«');
    end;
    { save handle value to form caption }
    FormName.Caption := StrToInt(Integer(Handle));
    { free resources }
    FreeLibrary(Handle);
  end;


Um das ganze noch sicherer zu machen solltest du für den Aufruf GetProcAddress eine Konstante verwenden:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
const 
  PLUGIN_INIT_PROC = 'Sampleprocedure';

{ ... }
  GetProcAddress(Handle, PLUGIN_INIT_PROC);


Der Grund dafür ist, dass die GetProcAddress auf die Groß/Kleinschreibung achtet. Ist halt ne Win32 Funktion...


Du experimentierst also gerade mit Plugins. Wenn du bis heute Abend Zeit hast, dann meld dich. Ich hab neulich mal ein Beispielpluginframework für einen bekannten geschrieben, das auf DLLs und Interfaces basiert. Läuft dann im Prinzip so ähnlich wie die Delphi IDE Erweiterung: Du fragst das Hauptprogramm auf irgendwelche Services ab (z.B. IMenuService) und wenn ja, kannst dir einen Menüpunkt registrieren und so weiter ;)

Gruß

_________________
Im Nachhinein ist man immer ein Schlauch!
"Dream as if you'll live forever, live as if you'll die today!" James Dean
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Mo 22.08.05 13:00 
@rochus

Bei FreeeLIbrary wird das handle nicht mit 'var' übergeben, deshalb kann das da gar nicht geändert werden, also muss nach dem aufruf von FreeLibraryA immer noch der alte handle (<> 0) drin stehen

wahrscheinlich ist der pfad nicht korrekt angegeben und desahlb aknn die dll nicht geladen werden

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit
rochus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 416

Win XP Prof, Fedora Core 4, SuSE 7.0
D7 Ent, D2005 Pers
BeitragVerfasst: Mo 22.08.05 13:02 
Habs gefunden (lag auf meinem FTP und hatte nicht gedacht, dass der an ist *g*).

Das ist ne D7 Version von dem Ding, hab's nur dort probiert.

Have fun!
Einloggen, um Attachments anzusehen!
_________________
Im Nachhinein ist man immer ein Schlauch!
"Dream as if you'll live forever, live as if you'll die today!" James Dean
rochus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 416

Win XP Prof, Fedora Core 4, SuSE 7.0
D7 Ent, D2005 Pers
BeitragVerfasst: Mo 22.08.05 13:06 
hmm da hast du recht *ups*

_________________
Im Nachhinein ist man immer ein Schlauch!
"Dream as if you'll live forever, live as if you'll die today!" James Dean