确定使用VirtualProtect来更改Delphi中的资源? [英] Ok to use VirtualProtect to change resource in Delphi?
问题描述
下面的代码是为了更改Const.pas字符串,安全地更改标准消息框上的按钮标签?
程序HookResourceString(rs:PResStringRec; newStr:PChar);
var
oldprotect:DWORD;
begin
VirtualProtect(rs,SizeOf(rs ^),PAGE_EXECUTE_READWRITE,@oldProtect);
rs ^ .Identifier:=整数(newStr);
VirtualProtect(rs,SizeOf(rs ^),oldProtect,@oldProtect);
结束
const
NewOK:PChar ='New Ok';
NewCancel:PChar ='New Cancel';
过程TForm.FormCreate;
begin
HookResourceString(@SMsgDlgOK,NewOK);
HookResourceString(@SMsgDlgCancel,NewCancel);
结束
是的,这应该是罚款,但我有一些评论:
-
确保从只有一个调用您的
HookResourceString
函数线程一次如果两个线程同时调用它,您可能会最终恢复错误的权限。 -
同时对于多线程,请确保您一次不使用此代码当一些其他线程可能尝试加载资源。
LoadResString
读取标识符
字段两次,并且需要两次都具有相同的值。 -
不需要将新值声明为类型的常量。普通的真实常数很好。 (编译器知道他们需要是PChars,因为它们作为PChar参数的实际参数传递。)
I'm working on a simple localization effort in D2010. I'm handling all strings on forms because ETM seems like overkill for my needs, as did other 3rd party tools... (although I'm not so sure at this point!)
Is the code below for changing the Const.pas strings considered safe to change the button labels on standard message boxes?
procedure HookResourceString(rs: PResStringRec; newStr: PChar);
var
oldprotect: DWORD;
begin
VirtualProtect(rs, SizeOf(rs^), PAGE_EXECUTE_READWRITE, @oldProtect);
rs^.Identifier := Integer(newStr);
VirtualProtect(rs, SizeOf(rs^), oldProtect, @oldProtect);
end;
const
NewOK: PChar = 'New Ok';
NewCancel: PChar = 'New Cancel';
Procedure TForm.FormCreate;
begin
HookResourceString(@SMsgDlgOK, NewOK);
HookResourceString(@SMsgDlgCancel, NewCancel);
end;
Yes, it should be fine, but I have some comments:
Make sure to call your
HookResourceString
function from only one thread at a time. If two threads call it simultaneously, you could end up restoring the wrong permissions.Also for multithreading, make sure you don't use this code at a time when some other thread might be attempting to load a resourcestring.
LoadResString
reads theIdentifier
field twice, and it needs to have the same value both times.There's no need to declare the new values as typed constants. Ordinary true constants are fine. (The compiler knows they need to be PChars because they're passed as actual arguments for a PChar parameter.)
这篇关于确定使用VirtualProtect来更改Delphi中的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!