tags:

views:

190

answers:

2

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;
+1  A: 

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 the Identifier 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.)

Rob Kennedy
Thanks A LOT, Rob!!
Tom1952
+1  A: 

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

André
Indeed. It has quite similar code to hook into resource string loading. Or simply use it!
mghie
Andre: thanks for the suggestion. I may come back to dggettext, but am a little gunshy about incorporating so much code that I wouldn't be able to debug if it breaks. And thanks, mghie, too. I always appreciate your input.
Tom1952
I understand that "gunshy" feeling, however dxgettext is used by a LOT of people who NEED it to work perfectly. If 180 people are using it, that's 180 eyeballs looking at that code, and only one set (yours) looking at your code. Sometimes, we learn the wrong lesson from our mistakes. :-)
Warren P