Delphi implements monitoring and scanning for registry

zhaozj2021-02-17  48

Disclaimer: Renhe Groups and individuals other than 9CBs are reproduced herein must indicate the source and the author.

Delphi's own Tregistry can only implement the basic operation of the registry, if we want to monitor the change in the registry or scan all children under the registry specific item, the Tregistry class is not powerful. I took a half-day SDK, I finally realized the monitoring and scan of Delphi to the registry, I didn't dare to enjoy exclusive, which was dedicated to the vast Delphi enthusiast.

Monitor registry related items to use an API: RegNotifyChangeKeyValue.

Long RegNotifyChangeKeyValue

HKEY HKEY, // Handle Bool BwatchSubtree, // Does Sub-key DWRD DWNOTIFYFILTER, // Which changed, // Accept registry change events Event object handle Bool Fasynchronous // Report before the registry changes or after the registry changes);

Note The above HEVENT is an event object handle that accepts a registry changing event. We must use API: CREATEEVENT to create a system event object.

Handle CreateEvent

LPSecurity_attributes lpeventattributes, // security_attributes structure Bool BmanualReset, // Does the Bool BinitialState, / / ​​Do not set the name of the initial LPCTSTR LPNAME // event object);

New project, add a listbox, two button.

// Write an example // monitor the registry // monitor the hkey_current_user / software item All sub-keys Procedure TFORM1.BUTTON1CLICK (Sender: Tobject); Var Hnotify: Thandle; HKEYX: HKEY; DWRES: DWORD; Begin Hnotify: = CreateEvent NIL, // Do not use the security_attributes structure false, // not automatically reset true, // Set the name of the initial statue 'registryNotify' // event object);

IF hnotify = 0 The begin ShowMessage ('CreateEvent Failed.'); EXIT; END;

if RegOpenKeyEx (HKEY_CURRENT_USER, // with key 'Software', // subkey 0, // reserved KEY_NOTIFY, // save the monitoring hKeyx // handle) <> ERROR_SUCCESS then begin CloseHandle (hNotify); Showmessage ( 'RegOpenKeyEx failed. '); EXIT;

if RegNotifyChangeKeyValue (hKeyx, // handle to monitor key sub TRUE, // monitoring of this sub-key REG_NOTIFY_CHANGE_NAME or REG_NOTIFY_CHANGE_LAST_SET, hNotify, // accept the registry changes to event object handle TRUE // registry before reporting changes) <> ERROR_SUCCESS then begin CloseHandle (hNotify); RegCloseKey (hKeyx); Showmessage ( 'RegNotifyChangeKeyValue failed'); exit; end; dwRes: = WaitForSingleObject (hNotify, 60 * 1000); // monitored one minute if dwRes = 0 then Showmessage ( 'Registry Will be changed. ');

CloseHandle (HNOTIFY); RegcloseKey (HKEYX); END;

It should be noted that the API: WaitForsingleObject will wait until the registry change event or timeout will return, during which our program will lose response. The solution is to create a new thread and monitor the registry in the new thread.

Scan the registry to use two additional APIs: RegenumKey and RegenumValue.

Long regenumkey (HKEY HKEY, // To scan the registry project handle DWORD dwindex, // to scan the Subkey serial number lptstr lpname, // The Subkey name to be scanned LPDWORD LPCBNAME, // The Subkey name to be scanned is occupied;

The method of using this function is: first assign DWIndex 0, call rehanumKey; then INC, then call Regenkey until the return value is ERROR_NO_MORE_ITEMS, indicating that there is no more children.

// Scan an example of the registry // only demonstrates how to enumerate a layer of children under HKEY_CURRENT_USER / Software Procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT); var Buf: array [0..255] of char; Ires: integer ; hKeyx: HKEY; dwIndex, dwSize: DWORD; begin if RegOpenKeyEx (HKEY_CURRENT_USER, 'Software', 0, KEY_READ or KEY_ENUMERATE_SUB_KEYS, hKeyx) <> ERROR_SUCCESS then begin Showmessage ( 'RegOpenKeyEx failed.'); exit; end;

dwIndex: = 0; repeat dwSize: = 255; iRes: = RegEnumKey (hKeyx, dwIndex, buf, dwSize); if iRes = ERROR_NO_MORE_ITEMS then break else if iRes = ERROR_SUCCESS then begin Listbox1.Items.Add (buf); Inc (dwIndex End; Until IRES <> Error_suCcess;

RegcloseKey (HKEYX); END;

转载请注明原文地址:https://www.9cbs.com/read-29103.html

New Post(0)