Link to home
Start Free TrialLog in
Avatar of formi
formiFlag for Switzerland

asked on

Using Resources in Delphi

I'm using Delphi2010 and try the first time to deal with "Resources". So I tried to create a resourcefile from a simple textfile and compiled it with BRCC32.EXE to a myResource.res. In the Delphi-project I embed the file with {$R myResource.res}

Then I found some code to load the text in a memo-field. The "loadResource"-methode raises an error "invalid parameter". If I open the exe-file with a resource-editor I wonder that I can not see my resource that I embedded.

Shouldn't I see that resource in the exe-file?? Where could be the problem?

Here is the code I load the resource:
{$R *.dfm}
{$R FormicaUpd.res}

function GetResourceAsPointer(ResName: pchar; ResType: pchar;
  out Size: longword): pointer;
var
    InfoBlock: HRSRC;
    GlobalMemoryBlock: HGLOBAL;
begin
  InfoBlock := FindResource(hInstance, resname, restype);
  if InfoBlock = 0 then
    if size = 0 then
       raise Exception.Create(SysErrorMessage(GetLastError));
     GlobalMemoryBlock := LoadResource(hInstance, InfoBlock);
     if GlobalMemoryBlock = 0 then
       raise Exception.Create(SysErrorMessage(GetLastError));
     Result := LockResource(GlobalMemoryBlock);
     if Result = nil then
       raise Exception.Create(SysErrorMessage(GetLastError));
   end;

   function GetResourceAsString(ResName: pchar; ResType: pchar): string;
   var
     ResData: PChar;
     ResSize: Longword;
   begin
     ResData := GetResourceAsPointer(resname, restype, ResSize);
     SetString(Result, ResData, ResSize);
   end;

procedure getResource;
var
     sample_txt: pointer;
     size: longword;
begin
  F_Main.Memo1.Lines.Text := GetResourceAsString('langDe', 'text');
end;

and in the res-file I can see the "Text / langDe / German resource
ASKER CERTIFIED SOLUTION
Avatar of VahaC
VahaC
Flag of Ukraine image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of formi

ASKER

Great, that works! Thanks a lot!