20.1.6 Tresourcestream Object
The Tresourcestream object is another class of MemoryStream object that provides access to Windows application resources and therefore calls it a resource flow. Tresourcesream also inherited from the Tcustommemorystream. Therefore, on the basis of the Tcustommemorystream object, the construction method that establishes a connection to the specified resource module or resource file is defined, and write is overridden to implement the write data to the resource file.
The following is an introduction to the implementation of Tresourcestream
1. Private domain
Tresourcestream does not define a new property, but it defines two data fields Hresinfo and Hglobol and a private method initialize in the private section, which are defined as follows:
Tresourcestream = Class (Tcustommemorystream)
Private
HRESINFO:HRSRC;
Hglobal:thandle;
Procedure Initialize (Instance:thandle; Name, Restype:pchar);
...
End
HRSRC is a structure handle that describes Windows resource information. The hglobal variable represents the handle of the module in which the resource resides. If you are working with an application resource, Hglohal represents the handle of the EXE program, and if it is a dynamic-link library (DLL), Hglobal represents the handle of the dynamic-link library. The Tresourcestream object uses these two variables to access resources in an application or dynamic-link library.
The Initialize method is used internally by Tresourcestream objects. Its construction methods create and Createfromid are all called initialize methods to complete initialization of tresourcestream. It is implemented as follows:
Procedure Tresourcestream.initialize (Instance:thandle; Name, Restype:pchar);
Procedure Error;
Begin
Raise Eresnotfound.create (Fmtloadstr (Sresnotfound, [Name]));
End
Begin
Hresinfo: = FindResource (Instance, Name, restype);
If Hresinfo = 0 then Error;
Hglobal: = LoadResource (Instance, hresinfo);
If Hglobal = 0 then Error;
Setpointer (Lockresource (Hglobal), Sizeofresource (Instance, hresinfo));
End
In this method implementation, you first call the Windows function Foundresource to get the resource named name and type ResType in the module specified by the parameter instance, and then call LoadResource to call the resource into memory. and returns the handle to the resource in memory, and finally, the resource is copied into the Resourcestream. The instance parameter of the method represents the module handle of the resource to invoke. A module can be either an executable file or a dynamic-link library. An exception event occurs if a resource is not found in the module when the resource is read.
2. Construction method Create and Createfromid
There is no big difference in implementation between the two methods. As the name suggests, the first method is to construct tresourcestream through resource names; The second method constructs tresourcestream through the resource ID, and in the implementation process, they call the Initialize method. The following are their implementations:
Constructor Tresourcestream.create (instance:thandle; const resname:string;
Restype:pchar);
Begin
Inherited Create;
Initialize (Instance, Pchar (resname), restype);
End
Constructor Tresourcestream.createfromid (Instance:thandle; Resid:integer;
Restype:pchar);
Begin
Inherited Create;
Initialize (Instance, Pchar (resid), restype);
End
Both methods have instance parameters, and the meaning of the parameter value is described in Insitialize.
3. Write method
The Tresourcestream write method accomplishes just one thing, and it produces this exception event, which is implemented as follows:
function tresourcestream.write (const Buffer; Count:longint): Longint;
Begin
Raise Estreamerror.createres (Scantwriteresourcestreamerror);
End
As you can see from the method implementation, the Tsourcestream object is not allowed to write data. Once the data is written to the resource stream, an exception event is generated.