Unit HookType

*****************************************************} { MemProof } { Hook functions prototypes } { } { Copyright (c) 1998 Atanas Stoyanov } { } { 6/2/98 - Modifications by Stefan Hoffmeister } { 5/10/98- Initial release } { } {*******************************************************} {$A+,B-,C+,D-,E-,F-,G+,H+,I+,J+,K-,L+,M-,N+,O-,P+,Q-,R-,S-,T-,U-,V+,W+,X+,Y+,Z1} // ****** What follows are important compiler switches // ****** expanded in their long form so that one can // ****** actually read and understand them // ****** DO NOT CHANGE ANY SETTING BELOW {$DEBUGINFO OFF} {$ENDIF DEBUG} {$STACKFRAMES ON} {$TYPEDADDRESS ON} {$OPTIMIZATION OFF

Classes

Functions

AddLastWin32Error - Adds the Last error returned by windows as an error entry.
ClearFlag - clears a particular flag Flags is one of the HF_xx constants.
IsFlagSet - 128 returns true is a particular flag bit is set Flags is one of the HF_xx constants.
IsPointerOverrunOK - will check if the ovewrrun stamp is still ok (the memory has not been overran by a bad write

will check if the overrun stamp is still ok (the memory has not been overran by a bad write
SetFlag - sets a particular flag Flags is one of the HF_xx constants.
StampForOverrunTest - will stamp the address - Size of the stamp variable with a specific value

will stamp the address - Size of the stamp variable with a specific value

Types

PExportedFunctions
TAddError
TAllocateResource
TExportedFunctions
TFreeResource
TFreeResourcesWithTag
TNewHookDLL
TNewHookFunction
TNewHookGroup
TNewHookItem
TRegisterDLLs
TRegisterFunctions
TRegisterGroups
TRegisterItems
TSetResourceTag

Constants

HDS_NORMAL
HF_RESERVED2
HF_RESERVED3
HF_RESERVED4
HF_RESERVED5
HF_RESERVED6
HF_RESERVED7
HF_STRESS
HF_TESTFOROVERRUN
HGS_NORMAL
HGS_SIZEITEM
HGS_TRACEITEM
HIS_HIDDEN
HIS_NORMAL
OverrunStampBytes

Variables


Functions


procedure AddLastWin32Error(AnErrorFunction: TAddError);

Adds the Last error returned by windows as an error entry. Called form Windows API hooks when they an detect error

Adds the Last error returned by windows as an error entry. Called form Windows API hooks when they detect error


procedure ClearFlag(PFlag : PInteger; const Flag : Integer);

clears a particular flag Flags is one of the HF_xx constants.

clears a particular flag Flags is one of the HF_xx constants.


function IsFlagSet(PFlag : PInteger; const Flag : Integer) : Boolean;

128 returns true is a particular flag bit is set Flags is one of the HF_xx constants.

returns true is a particular flag bit is set Flags is one of the HF_xx constants.


function IsPointerOverrunOK(P: PChar; Size: Integer): Boolean;

will check if the ovewrrun stamp is still ok (the memory has not been overran by a bad write

will check if the overrun stamp is still ok (the memory has not been overran by a bad write


procedure SetFlag(PFlag : PInteger; const Flag : Integer);

sets a particular flag Flags is one of the HF_xx constants.

sets a particular flag Flags is one of the HF_xx constants.


procedure StampForOverrunTest(P: PChar; Size: Integer);

will stamp the address - Size of the stamp variable with a specific value

will stamp the address - Size of the stamp variable with a specific value


Types


PExportedFunctions = ^TExportedFunctions

TAddError = procedure( ErrorGroup : Integer;
                       ErrorNo : Integer;
                       ErrorText : PChar;
                       Tag : Integer)
Add error. Called when an error is detected in some of the APIs
TAllocateResource = procedure ( SomeText : PChar;
                                Resource : Pointer;
                                ResourceSize : Integer;
                                ResourceGroup  : Integer;
                                ResourceSubType : Integer;
                                Tag : Integer)
AllocateResource is called when a resource is being allocated. For resources where the size is unknown, call with ResourceSize = 0. This will avoid size checking
TExportedFunctions = record
AAddError : TAddError;
AAllocateResource : TAllocateResource;
AFreeResource : TFreeResource;
AFreeResourcesWithTag : TFreeResourcesWithTag;
ASetResourceTag : TSetResourceTag;
end;

TFreeResource = procedure ( Resource : Pointer;
                            ResourceSize : Integer;
                            ResourceGroup : Integer)
FreeResource is called when a resource is being freed. It will check if the whole size is being freed For resources where the size is unknown, call with ResourceSize = 0. This will avoid size checking
TFreeResourcesWithTag = procedure ( ResourceTag : Integer;
                                    ResourceGroup : Integer;
                                    IncludeSubItems : Boolean)
FreeResourceWithtag is called when all the resources with a specific tage are being freed.
TNewHookDLL = function ( Instance : THandle;
                               DLLName : PChar;
                               DLLDescription : PChar;
                               Style : Integer) : Integer
instance is the HInstance of the calling dll. installs a new dll to be hooked upon. Returns a unique identifier to be used as Dll_ID Style : Integer is a set HDS_xx constants If error will return -1
TNewHookFunction = procedure( Item_ID     : Integer;
                              FunctionName : PChar;//Function to be hooked
                              NewFunction  : Pointer;
                              POldAddress  : Pointer)
Hooks a function. Item_ID is returned by previous call to NewHookItem The function name is the name of exported function If the function is imported by ordinal, one can use it ex FunctionName = '2' -N.B. do NOT use Pchar(2) New Function is the address of the hook function to be called POldFunction is the address of a variable that will hold the address of the original hook funciton If error will return -1
TNewHookGroup = function( Dll_ID : Integer;
                                GroupDescription : PChar;
                                Style : Integer) : Integer
installs a new group of functions to be hooked. groups of functions share the same Free procedure, even tough they are Style : Integer is a set HGS_xx constants different items Returns a unique ID to be used as Group_ID If error will return -1
TNewHookItem = function  ( Group_ID : Integer;
                           ItemDescription : PChar;
                           var PStress : PInteger;
                           Style : Integer;
                           BitmapID : Integer) : Integer
installs a new item to be hooked. Items are the smallest unit that will be hooked. Items are part of groups PStress is a pointer to the Stress variable, Style : Integer is a set of HIS_xx constants BitmapID : Resource ID of the bitmap glyph for this item. Used in the UI to identify this item. Returns a unique ID to be used as Item_ID If error will return -1
TRegisterDLLs = function ( RegisterFunction : TNewHookDLL) : Integer
follow the types of the four functions that each dll should export function that will be called first to register the hooked dlls
TRegisterFunctions = function ( RegisterFunction : TNewHookFunction;
                                     Functions : PExportedFunctions) : Integer
TRegisterFunctions
TRegisterGroups = function( RegisterFunction : TNewHookGroup) : Integer
function that will be called to register the hooked groups/categories
TRegisterItems = function (RegisterFunction : TNewHookItem) : Integer
function that will be called to register the hooked functions
TSetResourceTag = procedure( Resource : Pointer;
                             ResourceTag : Integer;
                             ResourceGroup : Integer)
Sets the tag of a previously allocated resource

Constants

HDS_NORMAL = 0

Hook dll style constants

HF_RESERVED2 = HF_TESTFOROVERRUN shl 1

HF_RESERVED3 = HF_RESERVED2 shl 1

HF_RESERVED4 = HF_RESERVED3 shl 1

HF_RESERVED5 = HF_RESERVED4 shl 1

16

HF_RESERVED6 = HF_RESERVED5 shl 1

32

HF_RESERVED7 = HF_RESERVED6 shl 1

64

HF_STRESS = 1

Hook Flags constants

HF_TESTFOROVERRUN = HF_STRESS shl 1

if this bit is set, the resource is being stressed and should return an error

HGS_NORMAL = 0

Hook group style constants

HGS_SIZEITEM = HGS_TRACEITEM shl 1

items in this group will be size-base, as opposed to resource base memory allocations should be with this option

HGS_TRACEITEM = 1

items in this group will not be reported in the final report. it is used only for tracing purposes and is not really a resource

HIS_HIDDEN = 1

items that are not be shown in the user interface

HIS_NORMAL = 0

Hook item style constants

OverrunStampBytes = 4

the number of bytes for the memory overrun function

Variables