From 812a665cbe35af46c47f83e5980709e40b652b42 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 24 Mar 2011 03:44:26 +0000 Subject: [PATCH] rtl: don't crash if resource is not found in TResourceStream and was passed by ID instead of Name (in this case exception tried to get a string from ID) git-svn-id: trunk@17173 - --- rtl/objpas/classes/classesh.inc | 4 ++-- rtl/objpas/classes/streams.inc | 32 ++++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/rtl/objpas/classes/classesh.inc b/rtl/objpas/classes/classesh.inc index 8473c7fa0c..a33117ebd0 100644 --- a/rtl/objpas/classes/classesh.inc +++ b/rtl/objpas/classes/classesh.inc @@ -926,7 +926,7 @@ type private Res: TFPResourceHandle; Handle: THandle; - procedure Initialize(Instance: THandle; Name, ResType: PWideChar); + procedure Initialize(Instance: THandle; Name, ResType: PWideChar; NameIsID: Boolean); public constructor Create(Instance: THandle; const ResName: WideString; ResType: PWideChar); constructor CreateFromID(Instance: THandle; ResID: Integer; ResType: PWideChar); @@ -937,7 +937,7 @@ type private Res: TFPResourceHandle; Handle: THandle; - procedure Initialize(Instance: THandle; Name, ResType: PChar); + procedure Initialize(Instance: THandle; Name, ResType: PChar; NameIsID: Boolean); public constructor Create(Instance: THandle; const ResName: string; ResType: PChar); constructor CreateFromID(Instance: THandle; ResID: Integer; ResType: PChar); diff --git a/rtl/objpas/classes/streams.inc b/rtl/objpas/classes/streams.inc index 9ddaa10760..c3c139b7da 100644 --- a/rtl/objpas/classes/streams.inc +++ b/rtl/objpas/classes/streams.inc @@ -772,49 +772,61 @@ end; {****************************************************************************} {$ifdef UNICODE} -procedure TResourceStream.Initialize(Instance: THandle; Name, ResType: PWideChar); +procedure TResourceStream.Initialize(Instance: THandle; Name, ResType: PWideChar; NameIsID: Boolean); begin Res:=FindResource(Instance, Name, ResType); if Res=0 then - raise EResNotFound.CreateFmt(SResNotFound,[Name]); + if NameIsID then + raise EResNotFound.CreateFmt(SResNotFound,[IntToStr(PtrInt(Name))]) + else + raise EResNotFound.CreateFmt(SResNotFound,[Name]); Handle:=LoadResource(Instance,Res); if Handle=0 then - raise EResNotFound.CreateFmt(SResNotFound,[Name]); + if NameIsID then + raise EResNotFound.CreateFmt(SResNotFound,[IntToStr(PtrInt(Name))]) + else + raise EResNotFound.CreateFmt(SResNotFound,[Name]); SetPointer(LockResource(Handle),SizeOfResource(Instance,Res)); end; constructor TResourceStream.Create(Instance: THandle; const ResName: WideString; ResType: PWideChar); begin inherited create; - Initialize(Instance,PWideChar(ResName),ResType); + Initialize(Instance,PWideChar(ResName),ResType,False); end; constructor TResourceStream.CreateFromID(Instance: THandle; ResID: Integer; ResType: PWideChar); begin inherited create; - Initialize(Instance,PWideChar(ResID),ResType); + Initialize(Instance,PWideChar(ResID),ResType,True); end; {$else UNICODE} -procedure TResourceStream.Initialize(Instance: THandle; Name, ResType: PChar); +procedure TResourceStream.Initialize(Instance: THandle; Name, ResType: PChar; NameIsID: Boolean); begin Res:=FindResource(Instance, Name, ResType); if Res=0 then - raise EResNotFound.CreateFmt(SResNotFound,[Name]); + if NameIsID then + raise EResNotFound.CreateFmt(SResNotFound,[IntToStr(PtrInt(Name))]) + else + raise EResNotFound.CreateFmt(SResNotFound,[Name]); Handle:=LoadResource(Instance,Res); if Handle=0 then - raise EResNotFound.CreateFmt(SResNotFound,[Name]); + if NameIsID then + raise EResNotFound.CreateFmt(SResNotFound,[IntToStr(PtrInt(Name))]) + else + raise EResNotFound.CreateFmt(SResNotFound,[Name]); SetPointer(LockResource(Handle),SizeOfResource(Instance,Res)); end; constructor TResourceStream.Create(Instance: THandle; const ResName: string; ResType: PChar); begin inherited create; - Initialize(Instance,pchar(ResName),ResType); + Initialize(Instance,pchar(ResName),ResType,False); end; constructor TResourceStream.CreateFromID(Instance: THandle; ResID: Integer; ResType: PChar); begin inherited create; - Initialize(Instance,pchar(PtrInt(ResID)),ResType); + Initialize(Instance,pchar(PtrInt(ResID)),ResType,True); end; {$endif UNICODE}