Autor Beitrag
beljo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 129



BeitragVerfasst: Di 25.04.06 14:16 
Hallo Leute,
ich beschäftige mich zurzeit mit dll's.
Hier habe ich eine dll erzeugt
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
library rechnen;
uses
  SysUtils,
  Classes;

{$R *.RES}
function addiere(a,b:integer):integer;
 begin
  result:= a+b;
 end;
  exports
  addiere;
begin
end.

Und hier will ich auf die function addiere über ein anderes Delphi-Programm zurückgreifen
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;
  function addiere(a,b:integer):integer;stdcall;
type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;

implementation
function addiere(a,b:integer):integer;stdcall;
external 'C:\Documents and Settings\MinicS\Desktop\saveWechselträger\Neuer Ordner\rechnen.dll'
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.text:=IntToSTr(addiere(2,3));
end;

end.

Compilieren funzt, leider stürzt das Programm ab sobald ich den Button1 drücke.
Ich hoffe jemand kann mir helfen,
Danke im Vorraus
Martin1966
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1068

Win 2000, Win XP
Delphi 7, Delphi 2005
BeitragVerfasst: Di 25.04.06 14:34 
Benutze auch mal in der DLL den Aufrufkonversion. Keine Ahnung ob das klappt.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
library rechnen;

uses
  SysUtils,
  Classes;

{$R *.RES}

function addiere(a,b:integer):integer; stdcall;
begin
  result:= a+b;
end;

exports
  addiere;

begin
end.

_________________
Ein Nutzer der Ecke ;-)
nullplan001
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 212

Win 2000 Professional, Debian Linux 4.0 (Etch,Stable)
Pascal (FreePascal 2.0.2, TurboPascal 7.0), C(++) (G++/GCC 3.4.2 + MinGW), Java (JDK 1.5.0_07), PHP (PHP 5.1.4)
BeitragVerfasst: Di 25.04.06 15:04 
*räusper* Delphi hat leider keine Glaskugel. Die Funktionsdefinition im Programm muss entweder wie folgt lauten:
ausblenden Delphi-Quelltext
1:
function add(a,b:integer):integer; external 'rechnen.dll' name 'addieren';					

oder
ausblenden Delphi-Quelltext
1:
function addieren(a,b:integer):integer; external 'rechnen.dll';					

oder du bindest zur Laufzeit ein. Das sieht dann so aus:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
function addieren(a,b:integer):integer;
var 
  add:function (x,y:integer):integer;
  Lib:THandle;
begin
  @add := nil;
  Lib := LoadLibrary(PCHar('rechnen.dll'));
  try
    if Lib = 0 then begin Result := -1; exit; end;
    @add := GetProcAddress(Lib,PChar('addieren'));
    if @add = nil then begin Result := -1; exit; end;
    result := add(a,b);
  finally
    FreeLibrary(Lib);
  end;
end;

Da musst du aber auch die DLL zum Programm mitliefern, da die DLL zur Laufzeit eingebunden wird. Hat den Vorteil, dass du, wenn du einen Bug in addieren entdeckst, nicht das ganze programm austauschen musst, sondern nur die DLL.
Btw: Das ganze steht glaub ich auch in der Delphi-Library, aber ganz sicher auf www.delphi-praxis.de und ist durch etwas intelligentes Googlestoßen zu finden.
Tschö,
nullplan

_________________
Ich fahr' nicht selber, weil ich festgestellt habe: ich fahre zu emotional. Bin 180 gefahren wo 30 erlaubt war... -- Jürgen von der Lippe
beljo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 129



BeitragVerfasst: Di 25.04.06 15:44 
jep,hat geklappt.
Irgendwann werde ich es auch dynamisch versuchen, aber fürs erste reicht es mir so.
Danke nochmals Leute :-)