- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题源于这个 one .
问题是:创建可以容纳来自系统的许多回调命令的非可视化组件。用户可以在 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() 使用 Thunk 技术。我想避免这种情况,因为程序将无法在启用数据执行保护 (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;
最佳答案
如果您可以更改 DLL 以接受记录数组而不是指针数组,那么您可以将记录定义为同时包含回调指针和对象指针,并为回调签名提供一个额外的指针参数。然后定义一个简单的代理函数,DLL可以调用这个对象指针作为参数,代理可以通过那个指针调用真正的对象方法。无需 thunking 或低级汇编,无需特殊编码即可在 32 位和 64 位中运行。类似于以下内容:
type
TCallback = function(AUserData: Pointer; AParam1, AParam2: Integer): Word; stdcall;
TCallbackRec = packed record
Callback: TCallback;
UserData: Pointer;
end;
TCommandFunc = function(AParam1, AParam2: integer): Word of object;
TCommandCollectionItem = class(TCollectionItem)
private
FOnEventCommand: TCommandFunc;
function InternalCommandFunction(APara1, AParam2: Integer): Word;
published
property OnEventCommand: TCommandFunc read FOnEventCommand write FOnEventCommand;
end;
TMainComp = class(TComponent)
private
CallbacksArray: array of TCallbackRec;
public
procedure Start;
published
property CommandsTable: TCommandCollection read FCommandsTable write SetCommandsTable;
end;
.
function CallbackProxy(AUSerData: Pointer; AParam1, AParam2: Integer): Word; stdcall;
begin
Result := TEventCollectionItem(AUserData).InternalEventFunction(AParam1, AParam2);
end;
procedure TMainComp.Start;
var
i: Integer;
begin
SetLength(CallbacksArray, FCommandsTable.Count);
for i := 0 to FCommandsTable.Count - 1 do begin
CallbacksArray[i].Callback := @CallbackProxy;
CallbacksArray[i].UserData := FCommandsTable.Items[i];
end;
AddThread(@CallbacksArray[0]);
end;
function TEventCollectionItem.InternalEventFunction(AParam1, AParam2: Integer): Word;
begin
// ...
if Assigned(FOnEventCommand) then begin
Result := FOnEventCommand(Param1, Param2);
end;
end;
如果这不是一个选项,那么使用 thunk 是给定您所展示的设计的唯一解决方案,并且您需要单独的 32 位和 64 位 thunk。不过不用担心 DEP。只需使用 VirtualAlloc()
和 VirtualProtect()
而不是 New()
,这样您就可以将分配的内存标记为包含可执行代码。这就是 VCL 自己的 thunk(例如,TWinControl
和 TTimer
使用的)如何避免 DEP 干扰。
关于delphi - 具有标准调用约定的回调系统的组件模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10615473/
我正在研究 learnyounode 的 HTTP 客户端作业。 我想知道为什么控制台记录来自response.on(“end”,callback)的数据仅输出预期输出的最后一部分,而控制台记录来自r
我正在尝试创建一个对象列表(在我的示例中为 List),我在其中使用 json 将对象添加到此列表,但该列表仍为空。这是我的代码: public List readCardsFromJson() {
我有一个 JavaScript 函数“print_something”,它在大约 300 个 jsp 帮助页面中实现。我发现这个“print_something”函数必须被纠正。所以我正在寻找一个不更
有 2 个 HTML 下拉列表,一个用于 12 小时时间,一个用于每小时 5 分钟的时间间隔。 .. 1 .. 12 .. 0 .. 55 .. 一直在尝试使用 if/
我有一个 A 类,我打算在它与设备驱动程序交互时将其放入共享库中。 我有一个 B 类,将来可能是 C、D、E...,它将使用共享库中的 A 类。 我想要在类 A 中设置回调函数的功能,以便当特定事件发
我需要能够在处理完 Observable.next() 之后执行回调。 我有一个组件“A”,它有一个主题使用 Subject.next() 发送通知。我有一个组件“B”,它订阅了 Subject.as
我有一张在顶部和底部单元格下方带有阴影的表格(此处使用 Matt Gallagher 的解决方案:http://cocoawithlove.com/2009/08/adding-shadow-effe
有人可以向我解释一下为什么这段代码有效 renderSquare(i) { return ( this.handleClick(i)} /> ); } 但
我可以让两个不同的客户端监听相同的 WCF 回调并让它们都接收相同的数据而不必进行两次处理吗? 最佳答案 不是真的 - 至少不是直接的。你所描述的听起来很像发布/订阅模式。 WCF 服务基本上在任何给
我是 SignalR 的新手,如果这个问题太明显,我深表歉意,但我在文档中找不到任何答案。 这是我的代码。 /*1*/ actions.client.doActionA = function (r
我有这个应用程序,您可以在其中输入一些文本并按下一个按钮,将此文本添加到自定义小部件中。这是代码: import 'dart:core'; import 'package:flutter/materi
我读到当您还想使用模型回调时不能使用 Keras 进行交叉验证,但是 this post表明这毕竟是可能的。但是,我很难将其纳入我的上下文。 为了更详细地探讨这个问题,我正在关注 machinelea
我尝试在重力表单中提交表单失败后运行一些 jQuery 代码,也就是验证发现错误时。 我尝试使用 Ajax:complete 回调,但它根本不触发。 我尝试运行的代码基本上将监听器添加到选择下拉列表中
我有一个 $image,我 .fadeIn 和 .fadeOut,然后 .remove .fadeOut 完成。这是我的代码: $image .fadeIn() .fadeOut(func
我正在处理一个自定义文件路径类,它应该始终执行一个函数 写入相应的系统文件及其文件对象后 关闭。该函数将文件路径的内容上传到远程位置。 我希望上传功能完全在用户的幕后发生 透视,即用户可以像使用其他任
这里是 javascript 新手,所以回调在我的大脑中仍然有点不确定。 我想做的是:给定一个“菜单”,它是一个 objectId 数组,查询与该 objectId 相对应的每个 foodItem,获
我正在学习回调,我编写了以下代码: var http = require('http'); var str = ""; var count = 2; function jugglingAsync(ca
这是我的困境,我有一系列被调用的函数,我正在使用回调函数在它们完成时执行函数。回调返回一个值并且效果也很好,我的问题是当我向回调添加参数时我无法再访问返回值。这是一个有效的例子: function m
This question already has answers here: Explanation of function pointers (4个答案) 上个月关闭。 如何将函数指针作为参数传递
我无法让以下代码工作。假设 ajax 调用有效,并且 msg['username'] 预设为 'john'。我想我对如何将变量传递给回调感到困惑。编辑:我认为我的主要困惑是如何从 Ajax 中获取“m
我是一名优秀的程序员,十分优秀!