Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi DLL-Problem (bei dynamischer Einbindung) (https://www.delphipraxis.net/75552-dll-problem-bei-dynamischer-einbindung.html)

katjah 21. Aug 2006 11:40


DLL-Problem (bei dynamischer Einbindung)
 
Ich habe Probleme mit der Verwendung von DLLs.
Beim statischen Einbinden funktioniert dasselbe problemlos. Sobald das ganze dynamisch eingebunden wird, gibt es Probleme:
entweder liefert die Funktion oder Müll zurück oder aber das ganze bricht mit einem Laufzeitfehler (mit Anzeige des CPU-Fensters) ab.
Das ganze scheint sich also irgendwas im Speicher zu zerdeppern.
Evtl. hat von Euch einer eine Idee, was ich da noch falsch mache ?

Hier der Code der dll:
Delphi-Quellcode:
library TurnFkt;
 
uses
  SysUtils,
  Classes;

{$R *.res}

var ColCaption:string;

function GetColCaption(ColName,Spezial:PChar): PChar;stdcall;
begin
    ColCaption:=ColName;
  Result:=PChar(ColCaption);
end;

exports GetColCaption;

begin
end.
und hier die Funktion im aufrufenden Programm:
Delphi-Quellcode:
function GetColCaption(ColName,Spezial:PChar): PChar;stdcall;
//  external '../TurnFkt.dll';
type TGetColCaption=function(ColName,Spezial:PChar): pchar;
var FGetColCaption:TGetColCaption;
    Handle       : THandle;
begin
  Handle:=LoadLibrary(Pchar('../TurnFkt'));
  if Handle<>0 then
  begin
    @FGetColCaption:=GetProcAddress(Handle,'GetColCaption');
    if @FGetColCaption<>nil then
      Result:=FGetColCaption(ColName,Spezial);
    FreeLibrary(Handle);
  end;
end;
Als Anleitung habe ich das Dll-Tutorial von www.dsdt.info verwendet, Sharemem möchte ich nicht einsetzen.

[edit=SirThornberry]Code-Tags durch Delphi-Tags ersetzt. Mfg, SirThornberry[/edit]

SirThornberry 21. Aug 2006 12:03

Re: DLL-Problem (bei dynamischer Einbindung)
 
beim statichen einbinden hast du die Aufrufconvention (stdcall) angegeben, beim dynamischen nicht. Und genau da liegt der Fehler. Wenn dann so:
Delphi-Quellcode:
type TGetColCaption=function(ColName,Spezial:PChar): pchar; stdcall;

katjah 21. Aug 2006 13:07

Re: DLL-Problem (bei dynamischer Einbindung)
 
Danke für den Hinweis, aber damit funktioniert es leider auch nicht.

Mavarik 21. Aug 2006 13:10

Re: DLL-Problem (bei dynamischer Einbindung)
 
Hmm
Delphi-Quellcode:
@FGetColCaption:=GetProcAddress(Handle,'GetColCaption'+#0);
Vielleicht so?

Frank

TKC 21. Aug 2006 13:48

Re: DLL-Problem (bei dynamischer Einbindung)
 
hmm .. ich denke es liegt daran ...

Delphi-Quellcode:
    if @FGetColCaption<>nil then
      Result:=FGetColCaption(ColName,Spezial);
    FreeLibrary(Handle);
Du bekommst einen Pointer auf eine Zeichenkette und speicherst den in Result.
Wenn du jetzt FreeLibrary aufruft wird der Pointer Zerstört und du bekommst nur noch Müll!

probiers mal so:
Delphi-Quellcode:
function GetColCaption(ColName, Spezial: PChar): string;
type
  TGetColCaption = function(ColName, Spezial: PChar): PChar;

var
  FGetColCaption             : TGetColCaption;
  Handle                     : THandle;

begin
  Handle := LoadLibrary(PChar('../TurnFkt'));

  if Handle <> 0 then
    begin
      @FGetColCaption := GetProcAddress(Handle, 'GetColCaption');
      if @FGetColCaption <> nil then
        Result := FGetColCaption(ColName, Spezial)^;
      FreeLibrary(Handle);
    end;
end;

freak4fun 21. Aug 2006 13:52

Re: DLL-Problem (bei dynamischer Einbindung)
 
Delphi-Quellcode:
Handle:=LoadLibrary(Pchar('../TurnFkt.dll'));
Fehlt vielleicht nur die Endung? :D

MfG
freak

_frank_ 21. Aug 2006 13:52

Re: DLL-Problem (bei dynamischer Einbindung)
 
Delphi-Quellcode:
  Handle:=LoadLibrary(Pchar('../TurnFkt.dll'));
Gruß Frank

TKC 21. Aug 2006 13:56

Re: DLL-Problem (bei dynamischer Einbindung)
 
Zitat:

Zitat von _frank_
Delphi-Quellcode:
  Handle:=LoadLibrary(Pchar('../TurnFkt.dll'));
Gruß Frank


Dürfte nicht weiter relevant sein .. s. MSDN

Zitat:

If the string does not specify a path, the function uses a standard search strategy to find the file. See the Remarks for more information.
If no file name extension is specified in the lpFileName parameter, the default library extension .dll is appended. However, the file name string can include a trailing point character (.) to indicate that the module name has no extension.

_frank_ 21. Aug 2006 14:34

Re: DLL-Problem (bei dynamischer Einbindung)
 
stümmt, man lernt nie aus ;)

ist denn das Handle <>0? schonmal mit dem debugger geschaut?
nicht dass er mit dem relativen Pfad nicht klar kommt...

Gruß Frank

TKC 21. Aug 2006 14:42

Re: DLL-Problem (bei dynamischer Einbindung)
 
Zitat:

Zitat von _frank_
stümmt, man lernt nie aus ;)

ist denn das Handle <>0? schonmal mit dem debugger geschaut?
nicht dass er mit dem relativen Pfad nicht klar kommt...

Gruß Frank

Ich habe doch oben schon erklärt wo der Fehler liegt ... wenn Handle=0 ist würde nichts in Result stehen !


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:59 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz