On 19.08.2012 10:18, Martin Schreiber wrote:
Hi,
In 2008 and 2011 there were threads about FPC and Unicode resource strings
with the conclusion that FPC does not support them.
Are Unicode resource strings implemented in FPC now? I did not find it in
documentation.
Is there a replacement or supplement for
http://wiki.freepascal.org/FPC_Unicode_support
which answers such questions?

Thanks, Martin
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel
Dear FPC-all!

Since the beginning of my FPC use I maintain the following FPC/Delphi compatible code.

to get unicode string from resource i use

function _W(const pRec: PResStringRec):WideString;
begin
  try
    Result:=LoadResStringW(pRec);
  except
    Result:='';
  end;
end;


example of use

// this may be placed in separate includes for Delphi and FPC
{$ifdef FPC}
resourcsestring
    sSomeConstant='This is UTF-8 encoded resource string';
{$else}
resourcsestring
    sSomeConstant='This is winXXXX encoded resource string';
{$endif}

var ws:WideString;

ws:=_W(@sSomeConstant); // this is X-platform way and Delphi/FPC compatible


// FPC version of string loader
function LoadResStringW(const pRec: PResStringRec): WideString;inline;
begin
  if pRec=nil then
  Result:=''
  else
  begin
    Result:=UTF8Decode(PAnsiString(pRec)^);
  end;
end;

// Delphi version uses WinAPI call
function LoadResStringW(const pRec: PResStringRec): WideString;
var
  hInst:cardinal;
  Buffer: array [0..1023] of char;
begin^M
  if pRec = nil then Exit;

  if pRec^.Identifier < 64*1024 then
  begin
    hInst:=FindResourceHInstance(pRec^.Module^);
    // try wide
    if LoadStringW(hInst,pRec^.Identifier, @Buffer, SizeOf(Buffer))>0 then
      Result:=PWideChar(@Buffer)
    else // try narrow
    if LoadString(hInst,pRec^.Identifier, @Buffer, SizeOf(Buffer))>0 then
      Result:=PChar(@Buffer)
    else
      Result:='-lost-';
  end
  else
    Result := PWideChar(pRec^.Identifier);
end;

regards


_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply via email to