- - * - WhiteUnicorn - * - -




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >


3209:How to use a user defined resource.

KEYWORDS: resource files, linking, user defined, resource workshop AREA: All


(*

The following demonstrates linking a user defined resource.

Resources are compiled into a ".res" file that is
attached to your application's .exe file at build time.
Even after you distribute your application, the resources
contained in your application's .exe file can be edited with
a resource editor. The resource editor in Borland's Resource
Workshop, that ships with the RAD pack, can produce and edit
both 16 and 32 bit resources that are self contained,
standalone, or embedded in a .exe or .dll in full WYSIWYG
fashion.

It's worth noting that all versions of Delphi ship with the
Borland Resource Command Line Compiler (BRCC.EXE and BRCC32.EXE),
and can be found in Delphi's Bin directory.

We first must create a text file containing our resource
definitions in the application's build directory. You may
name the file anything you wish, so long as it has the
file extension ".rc" and the filename without the extension
is not the same as any unit or project filename. This is
very important, as Delphi also will create a number of
resource files for your project automatically.

Here is the contents of the .rc file for our example:

MYUSERDATA MYDATATYPE TEST.TXT

MYUSERDATA is the resource name, MYDATATYPE is the resource type
and TEST.TXT is the name of the file continuing the user data we
will link in.

We must next create the TEST.TXT file, using any ASCII editor
(notepad will do fine). For our example, the file needs to
contain one single line:

Hello!

To compile the .rc file to a .res file that can be linked
with your application, simply type on the dos command line
the full path to the resource compiler, and the full path
to the name of the .rc file to compile. Here is an example:

c:\Delphi\Bin\brcc32.exe c:\Delphi\MYRES.RC

When the compiler is finished, you should have a new file
with the same name as the .rc file you've compiled, only
with an extension of ".res".

You can link the resource file with your application simply
by adding the following statement to your application's
code, substituting the name of your resource file:

{$R ResFileName.RES}

where ResFileName.RES is the actual name of the compiled
.res file.

Once the .res file is linked to your program, you can load
the resource from any module, even if you specified the
$R directive in a different unit.

To actually use the resource, you must make a few Windows API
calls. First you will call the FindResource() function, passing
the instance handle of your application, the name of the resource
to load, and the resource type. If FindResource is successful, the
function will return a handle to the unloaded resource.

Next, you can call the SizeOfResource() function to find the aligned
size of the resource, if you want to know the actual size of the
loaded resources memory block. Be forewarned that this not the
actual size of the data, but rather the size of the memory block
that will be allocated when you load the resource. If you want
to know the actual size of the data, you will need to embed that
information in the data itself.

To load the resource, we will call the LoadResource() function,
passing the handle returned to us when we called FindResource().
This function will return a handle to the loaded resource that we
can pass to the LockResource() function to retrieve a pointer to
the resource that we use to actually work with the data.

In our example, we will loop through the data, adding the data
to a string, until we find the embedded '!' character, signaling
that the end of the data has been found.

Finally, we will free the resource by first calling UnLockResource()
and then FreeResource(), passing the handle of the loaded resource.

Example Code:
*)


implementation

{$R *.DFM}

{$R MYRES.RES}

procedure TForm1.Button1Click(Sender: TObject);
var
  hRes        : THandle; {handle to the resource}
  pRes        : pointer; {pointer to the resource}
  ResSize     : longint; {aligned size of the resource}
  i           : integer; {counting variable}
 {$IFDEF WIN32}
  s           : shortstring; {a string to play with}
 {$ELSE}
  s           : string;      {a string to play with}
 {$ENDIF}
begin
 {find the resource}
  hRes := FindResource(hInstance,
                       'MYUSERDATA',
                       'MYDATATYPE');
  if hRes = 0 then begin
    ShowMessage('Could not find the resource');
    exit;
  end;
 {get the aligned size of the resource}
  ResSize := SizeOfResource(hinstance, hRes);
  if ResSize = 0 then begin
    ShowMessage('Nothing to load - size = 0');
    Exit;
  end;
 {load the resource}
  hRes := LoadResource(hInstance, hRes);
  if hRes = 0 then begin
    ShowMessage('Resource Load Failure');
    Exit;
  end;
 {Get a pointer to the resource}
  pRes := LockResource(hRes);
  if pRes = nil then begin
    ShowMessage('Resource Lock Failure');
    FreeResource(hRes);
    Exit;
  end;

{convert the resource pointer to a string}
  s:='';
  i := 0;
  while pChar(pRes)[i] <> '!' do begin
    s := s + pChar(pRes)[i];
    inc(i);
  end;

 {prove it works}
  ShowMessage(s);

 {unlock and free the resource}
  UnLockResource(hRes);
  FreeResource(hRes);
end;

end.

<end of ti>


        TI



* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >



- - * - Anastasija aka WhiteUnicorn - * - - LJLiveJournal
PFPhotoFile