Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

=Getting DATA from Resource is like this:

function GetMyRes(resname:string):string;
var
rStream: TResourceStream;
s:string;
begin
rStream := TResourceStream.Create(hInstance, resname, RT_RCDATA);
...
...
result:=s;
End;

>

=Using the DATA

Procedure GetReses;
var
myres1:string;
myres2:string;
begin
myres1:=GetMyRes("MYSTRING1");
myres2:=GetMyRes("MYSTRING2");
end;

>

=Putting DATA to Resource is like this:

Procedure SaveToResource(Dest:string);
var
s:string;
cbData     : Cardinal;
hDestRes   : THANDLE;
begin
if x=1 then s:='1a' else s:='1b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRING1',0,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;

if x=2 then s:='2a' else s:='2b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRING2',0,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;

if x=3 then s:='3a' else s:='3b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRING3',0,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;

if x=4 then s:='4a' else s:='4b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRING4',0,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;

if x=5 then s:='5a' else s:='5b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRING5',0,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;
end;

>

The problem here, WHAT if I PUT the Resource like this: (the zero incremented)

Procedure SaveToResource(Dest:string);
var
s:string;
cbData     : Cardinal;
hDestRes   : THANDLE;
begin
if x=1 then s:='1a' else s:='1b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRINGS_ALL',0,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;

if x=2 then s:='2a' else s:='2b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRINGS_ALL',1,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;

if x=3 then s:='3a' else s:='3b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRINGS_ALL',2,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;

if x=4 then s:='4a' else s:='4b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRINGS_ALL',3,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;

if x=5 then s:='5a' else s:='5b';
cbData := Length(s);
hDestRes := BeginUpdateResource(PChar(Dest), False);
if hDestRes <> 0 then
begin
UpdateResource(hDestRes, RT_RCDATA,'MYSTRINGS_ALL',4,PChar(s),cbData);
EndUpdateResource(hDestRes,FALSE)
end;
end;

How to get the "MYSTRINGS_ALL" in range 0 to 4?

>

This is not working, since it only GETS when I put ZER0s in all I named one by one.

function GetMyRes(resname:string):string;
var
rStream: TResourceStream;
s:string;
begin
rStream := TResourceStream.Create(hInstance, resname, RT_RCDATA);
...
result:=s;
End;

Whats the correct Function or the way to get all range from 0 to 4 strings?

> I think this is the right one, I think so!, please correct if any wrong in the code.

function GetMySTRResources(id:integer): string;
 var
   hF,hR:THandle;
   s:PChar;
 begin
  hF:=FindResourceEx(HInstance, RT_RCDATA ,'MYSTRINGS_ALL', id);
  if hF<>0 then begin
    hR:=LoadResource(HInstance, hF) ;
    if hR<>0 then begin
      s:=LockResource(hR) ;
      if Not Assigned(s) then s:='';
      UnlockResource(hR) ;
    end;
    FreeResource(hF) ;
  end;
  Result := s;
 end;

Thanks Rob Kennedy

share|improve this question
    
Resources are not implicitly null-terminated, so you cannot simply assign s to Result. See how the VCL does it by fetching the resource size. Or make sure to store the null character in the resource to begin with. – Rob Kennedy Jun 14 '13 at 5:15
    
Why not indent your sample source code? – Jeroen Wiert Pluimers Jun 14 '13 at 6:22
up vote 1 down vote accepted

Using TResourceStream, you cannot get all the values you've stored. TResourceStream uses FindResource, but you need it to use FindResourceEx because the numbers you're changing are the language ID, and FindResource doesn't offer that parameter.

Instead, you can call FindResourceEx yourself. That returns a resource handle for you to pass to LoadResource. That returns a memory handle; use LockResource to get a pointer to the real data. Look in Classes.pas in TResourceStream.Initialize to see how it's done.

Once you're able to specify the language ID of your resources, then you can load them each separately and arrange them into whatever data structure you want, including an array.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.