この質問は、この質問から生じました。
問題は、システムからの多くのコールバック コマンドを保持できる非ビジュアル コンポーネントを作成することです。ユーザーは、IDE で無制限の数のコールバックを定義できます。コールバックは TCollection で TCollectionItem として定義されます。
これは非常にうまく機能するパターンですが、いくつかの欠点があります。(後で説明します)したがって、もっとうまくできるのではないかと思います;-)
これはメインコンポーネントであり、ユーザーは CommandsTable コレクションを介して IDE で無制限の数のコールバック関数を定義できます
TMainComp = class(TComponent)
private
CallbacksArray: array [0..x] of pointer;
procedure BuildCallbacksArray;
public
procedure Start;
published
property CommandsTable: TCommandCollection read FCommandsTable write SetCommandsTable;
end;
すべてのコレクション アイテムは次のようになります。InternalCommandFunction は、システムから呼び出されるコールバックです。(stdcall 呼び出し規約)
TCommandCollectionItem = class(TCollectionItem)
public
function InternalCommandFunction(ASomeNotUsefullPointer:pointer; ASomeInteger: integer): Word; stdcall;
published
property OnEventCommand: TComandFunc read FOnEventCommand write FOnEventCommand;
end;
TComandFunc = function(AParam1: integer; AParam2: integer): Word of Object;
そして、これが実装です。プロセス全体は「開始」手順で開始できます
procedure TMainComp.Start;
begin
// fill CallBackPointers array with pointers to CallbackFunction
BuildCallbacksArray;
// function AddThread is from EXTERNAL dll. This function creates a new thread,
// and parameter is a pointer to an array of pointers (callback functions).
// New created thread in system should call our defined callbacks (commands)
AddThread(@CallbacksArray);
end;
そしてこれが問題のコードです。「InternalEventFunction」関数へのポインタを取得する唯一の方法は、MethodToProcedure() 関数を使用することだと思います。
procedure TMainComp.BuildCallbacksArray;
begin
for i := 0 to FCommandsTable.Count - 1 do begin
// it will not compile
//CallbacksArray[i] := @FCommandsTable.Items[i].InternalEventFunctionWork;
// compiles, but not work
//CallbacksArray[i] := @TCommandCollectionItem.InternalCommandFunction;
// works pretty good
CallbacksArray[i] := MethodToProcedure(FCommandsTable.Items[i], @TCommandCollectionItem.InternalCommandFunction);
end;
end;
function TEventCollectionItem.InternalEventFunction(ASomeNotUsefullPointer:pointer; ASomeInteger: integer): Word; stdcall;
begin
// some important preprocessing stuff
// ...
if Assigned(FOnEventCommand) then begin
FOnEventCommand(Param1, Param2);
end;
end;
前に説明したように、問題なく動作しますが、関数 MethodToProcedure() はサンク手法を使用しています。データ実行防止 (DEP) が有効になっているシステムや 64 ビット アーキテクチャではプログラムが動作しないため、これは避けたいと思います。新しい MethodToProcedure() 関数が必要になる可能性があります。
そのためのより良いパターンを知っていますか?
完了するために、これが MethodToProcedure() です。(原作者は誰だか知らない)。
TMethodToProc = packed record
popEax: Byte;
pushSelf: record
opcode: Byte;
Self: Pointer;
end;
pushEax: Byte;
jump: record
opcode: Byte;
modRm: Byte;
pTarget: ^Pointer;
target: Pointer;
end;
end;
function MethodToProcedure(self: TObject; methodAddr: Pointer): Pointer;
var
mtp: ^TMethodToProc absolute Result;
begin
New(mtp);
with mtp^ do
begin
popEax := $58;
pushSelf.opcode := $68;
pushSelf.Self := Self;
pushEax := $50;
jump.opcode := $FF;
jump.modRm := $25;
jump.pTarget := @jump.target;
jump.target := methodAddr;
end;
end;