Autor Beitrag
chubbson
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 32
Erhaltene Danke: 1



BeitragVerfasst: Di 30.06.09 10:25 
Ich habe eine DLL der ich in einer Function eine Callback procedure mitgebe, die sich jedoch in der gleichen Klasse/Form befinded wie die dll geladen wird. Wenn ich das so mache funktioniert. das nicht
Mit der Meldung 'Variable erforderlich'
Wenn ich die Funktion Globel in der Unit definiere.. Funktioniert das ganze. Was für eine erklärung gibt es dafür? ist es nicht möglich innerhalb der selben Klasse/Form eine Funktion aufzurufen etc..

Pseudo Code dafür:

DLL Code: (MyDll.dll)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
TMyCallBack= procedure; stdcall;

procedure MyDLLFunction(myCB: TMyCallBack) stdcall;
begin
  if assigned(myCB) then begin 
    myCB;
  end;
end;

exports
  MyDLLFunction name 'MYDLLFUNCTION',


Klasse Form:
ausblenden volle Höhe 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:
30:
31:
32:
33:
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen } 
    procedure OnMyCallBack();  //Wenn ich die procedure hier deklariere kommt die Fehlermeldung 'Variable erforderlich'
  end;

  procedure OnMyCallBack();  //Wenn ich die procedure hier deklariere Funktionier es Wunderbar


implementation 

type TMyDLLFunction = procedure(procPointer: Pointer); stdcall;

var
  MyDLLFunction: TMyDLLFunction; {Function pointer}

procedure TForm1.FormCreate(Sender: TObject);
begin
  @MyDLLFunction := NIL;
  @MyDLLFunction :=
      GetProcAddress(LoadLibrary('MyDll.DLL'), 'MYDLLFUNCTION');

  MyDLLFunction(@OnMyCallBack); 
end;

procedure {TForm1.}OnMyCallBack;
begin
  //Arbeite!
end;


Kann es sein das es nicht Funtkioniert weil 'MyDLLFunction' schon im Constructor aufgerufen wird?

Vielen Dank
Gruss
Chubbson
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 30.06.09 10:59 
Das liegt an einer einfachen Sache:
Eine Methode eines Objekts ist im Grunde ein Pointer auf die Funktion / Prozedur und ein Pointer auf das zugehörige Objekt. Siehe auch den Record TMethod.

Du kannst das also einfach lösen, indem du keinen Pointer sondern eine solche Methode an die DLL übergibst:
ausblenden volle Höhe Formularquelltext
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:
30:
31:
32:
33:
34:
35:
36:
37:
  type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen } 
    procedure OnMyCallBack(); stdcall; // nicht das stdcall vergessen, wenn du es in der DLL so deklarierst!
  end;

implementation

type
  TMyCallBack = procedure of object; stdcall;
  TMyDLLFunction = procedure(procPointer: TMyCallBack); stdcall;

var
  MyDLL: THandle; // geladene DLL merken und auch wieder freigeben!
  MyDLLFunction: TMyDLLFunction;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyDLL := LoadLibrary(PChar(ExtractFilePath(ParamStr(0)) + 'MyDll.DLL'));
  @MyDLLFunction := GetProcAddress(MyDLL, 'MYDLLFUNCTION');
  MyDLLFunction(OnMyCallBack); 
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FreeLibrary(MyDLL);
end;

procedure TForm1.OnMyCallBack;
begin
  ShowMessage('Hallo'#13#10 + Caption);
end;
Und:
ausblenden DLL
1:
2:
type
  TMyCallBack = procedure of object; stdcall;
Ich hoffe ich habe mich nicht vertippt, aber zumindest sollte das Vorgehen klar sein.

// EDIT:
Doch vertippt, der Parameter an TMyDLLFunction muss natürlich auch TMyCallBack sein. *korrigiert*
chubbson Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 32
Erhaltene Danke: 1



BeitragVerfasst: Di 30.06.09 11:53 
jaenicke ich danke dir

Funktioniert tiptop