Browse Source

* Avoid useless exceptions by checking existence of resource

Michaël Van Canneyt 2 years ago
parent
commit
f9a96e5e63
1 changed files with 14 additions and 8 deletions
  1. 14 8
      rtl/objpas/classes/classes.inc

+ 14 - 8
rtl/objpas/classes/classes.inc

@@ -1404,21 +1404,27 @@ end;
 
 { all targets should at least include the sysres.inc dummy in the system unit to compile this }
 function CreateComponentfromRes(const res : string;Inst : THandle;var Component : TComponent) : Boolean;
+
   var
     ResStream : TResourceStream;
+    ResID : TFPResourceHandle;
+    
   begin
-    result:=true;
 
     if Inst=0 then
       Inst:=HInstance;
-
+    ResId:=FindResource(Inst, Res, RT_RCDATA);
+    result:=ResID<>0;
     try
-      ResStream:=TResourceStream.Create(Inst,res,RT_RCDATA);
-      try
-        Component:=ResStream.ReadComponent(Component);
-      finally
-        ResStream.Free;
-      end;
+      if Result then
+        begin
+        ResStream:=TResourceStream.CreateFromID(Inst,ResID,RT_RCDATA);
+        try
+          Component:=ResStream.ReadComponent(Component);
+        finally
+          ResStream.Free;
+        end;
+        end;
     except
       on EResNotFound do
         result:=false;