=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
s
toResult
. 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