开发者

How to check the {raw-data} of user defined resource is integer or strings from code?

开发者 https://www.devze.com 2023-03-24 23:17 出处:网络
I using Delphi XE, i\'ve the foll开发者_如何学Goowing rc file: DB.PREFIXsql_db{ \"ALPHA\\0\" } DB.MAJORsql_db{ 1 }

I using Delphi XE, i've the foll开发者_如何学Goowing rc file:

DB.PREFIX     sql_db     { "ALPHA\0" }
DB.MAJOR      sql_db     { 1 }
DB.MINOR      sql_db     { 1 }

My question is how to check the "raw-data" of user defined resource is integer or strings from code?


The issue to check the data type of the resource is that the Raw-Data can be interpretred as an integer or string in the same time, due which you must use the LockResource function that only returns a pointer to the resource without any additional information.

Check the next code , if you change the type of the RawData from PAnsiChar to PInteger the code will work too interpreting the data as an integer.

{$APPTYPE CONSOLE}
{$R Test.RES}

uses
  Windows,
  SysUtils;

Procedure CheckResource(const ResourceName:string);
 var
   hResInfo  : THandle;
   hResData  : THandle;
   RawData   : PAnsiChar; //-> the resource is treated as an string
   //RawData: PInteger; //-> the resource is treated as an integer
 begin
  hResInfo := FindResource(HInstance, PChar(ResourceName), 'sql_db');
  if hResInfo <> 0 then
  begin
    hResData:=LoadResource(HInstance, hResInfo) ;
    try
      if hResData <> 0 then
      begin
        RawData:=LockResource(hResData) ;
        Writeln(RawData);
      end
    finally
      FreeResource(hResInfo) ;
    end;
  end;
 end;

begin
  try
    CheckResource('DBPREFIX');
    CheckResource('DBMAJOR');
    CheckResource('DBMINOR');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  readln;
end.

When you defines a custom type resource, you are creating a specific type and is part of your work handle that resource. what you're doing now does not make much sense, because you're assigning different data types to the same custom type (sql_db), instead you must create different types to store strings and integers, and then create a function to process each type.

0

精彩评论

暂无评论...
验证码 换一张
取 消