Is it good to use VirtualProtect to change a resource in Delphi?

I am working on a simple localization attempt in D2010. I process all the lines in the forms, because the ETM seems redundant for my needs, like other third-party tools ... (although I'm not sure about that)!

Is the code below for changing Const.pas lines safe for changing labels on standard messages?

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; 
+1
source share
2 answers

Yes, this should be fine, but I have some comments:

  • Be sure to call the HookResourceString function HookResourceString only one thread. If two threads call it at the same time, you can restore the wrong permissions.

  • Also, for multithreading, make sure that you are not using this code at a time when some other thread may be trying to load the resource. LoadResString reads the Identifier field twice, and it must have the same value both times.

  • There is no need to declare new values ​​as typed constants. Ordinary true constants are beautiful. (The compiler knows that they must be PChars because they are passed as actual arguments to the PChar parameter.)

+4
source

Why not use dxgettext? It's open source so you can at least take a look at how they do it ...
http://dxgettext.po.dk/

+1
source

Source: https://habr.com/ru/post/904311/


All Articles