Read and write registry

xiaoxiao2021-04-10  547

The registration database of WIN 95 and NT is a very important part of the system. There is a set of REG functions in the Win32 API to handle these issues. The general read and write process is as follows:

1. Use the RegopenkeyEx or RegreateKeyex function to open or create a key;

2. If the last step is successful, use the regQueryValueex read the value of the subkey, use the RegSetValueEx to set the sub-key value, use RegenumKey to get all sub-keys, use regdeletekey to delete a key;

3. Use the Regclosekey shutdown button after completing the operation.

The following program opens the HKEY_CURRENT_USER / SOFTWARE / ZEAL SOFTSTUDIO / SOFTWARE / ZEAL SOFTSTUDIO / ASKPRO FTP / LASTTIME button and then read the value of the Wol subkey.

HKEY HKEY;

Char SZ [256];

DWORD DWTYPE, SL = 256;

RegopenKeyex (HKEY_CURRENT_USER,

"Software // Zeal Softstudio // askpro ftp // lasttime",

NULL, Key_All_access, & HKey

RegQueryValueex (HKEY, "WOL", NULL, & DWTYPE, (LPBYTE) SZ, & SL

RegcloseKey (HKEY);

The MFC program can read and write the registry using the CREGKEY class. The way to call the API in the VB can refer to QA000226 "How to Access Windows System Registry".

Open the registration button

Long RegopenKeyex (HKEY HKEY, // Handle To Open Key

LPCTSTR LPSUBKEY, / / ​​Address of Name of Subkey To Open

DWORD ULOPTIONS, // Reserved = 0

Regsam Samdesired, // Security Access Mask

Phkey phkresult // address of Handle to Open Key

);

example:

HKEY HD;

HD = HKEY_LOCAL_MACHINE;

Char * regKeyname = "Software // XY123 // poker //";

Long a = regopenkeyex (hd, regkeyname, 0, key_read, & hd); // Successfully returned ERROR_SUCCESS, otherwise returns an error code

Close registration button

Long regclosekey (HKEY HKEY / / HANDLE TO Key To Close);

example:

RegcloseKey (HKEY_LOCAL_MACHINE);

Al: RegcloseKey (HD);

Establish registration key

Long RegcreateKeyex (HKEY HKEY, // Handle To An Open Key

LPCTSTR LPSUBKEY, / / ​​Address of Subkey Name

DWORD reserved, // reserved = 0

LPTSTR LPCLASS, / / ​​Address of class string

DWORD DWOPTIONS, / / ​​SPECIAL OPTIONS FLAG

Regsam Samdesired, // Desired Security Access

LPSecurity_Attributes LPSecurityAttributes, // Address of Key Security Structure

Phkey phkResult, // address of buffer for Opened Handlerpdword LPDWDISPOSITION / / Address of Disposition Value Buffer;

example:

Char * sclass = ""; // class name is specified as empty

DWORD NBF = 0; // Accept the return value, indicate whether to create a new button or open the existing key. (Test Always return REG_OPENED_EXISTING_KEY.

Long II = regreateKeyex (HD, RegkeyName, 0, Sclass, Reg_Option_non_Volatile,

Key_Read | Key_WRITE, NULL, & HD, & NBF;

// reg_option_non_volatile indicates that the key is permanently reserved. The security structure indicates null, automatically obtains a default value.

/ / Successfully returned ERROR_SUCCESS, otherwise returns an error code

Enumerate key value

Long RegenumValue (HKEY HKEY, // Handle to Key to Query

DWORD DWINDEX, // Index of Value To Query

LPTSTR LPVALUENAME, / / ​​Address of Buffer for Value String

LPDWORD LPCBVALUENAME, / / ​​Address for Size of Value Buffer

LPDWORD LPRESERVED, // Reserved = NULL

LPDWORD LPTYPE, / / ​​Address of Buffer for Type Code

LPBYTE LPDATA, / / ​​Address of Buffer for Value Data

LPDWORD LPCBDATA // Address for size of data buffer;

example:

DWORD DINX = 0;

Char Valuename [70]; // Allocate Numerical Name Buffer

STRCPY (Valuename, "Deskpattern"); // Jelly specifies which key value name

DWORD nsize = 69; // Value Name Buffer Size

DWORD K = reg_sz; // Indicate data type

Unsigned char vari [70]; // allocating numerical buffer

DWORD NCBVARI = 69; // Numerical buffer size

DINX = 0; // starting from 0

While ((II = RegenumValue (HD, DINX, VALUENAME, & NSIZE, NULL, & K, VARI, & NCBVARI))

= Error_no_more_items)

{

DINX ; // Index 1, ready to remove a value

nsize = 69; // Restore the original size

NCBVARI = 69;

}

After successful, return value 0, and each variable is returned to the following:

VALUENAME = value name, ending 0; if: Deskcolor

nsize = numeric name length, 9

K = reg_sz deskcolor type is REG_SZ

VARI = key value, 32768 deskcolor = "32768",

NCBVARI = key value REG_SZ includes end 0, = 6,

Read key value

Long RegqueryValueex (HKEY HKEY, // Handle To Key to Query

LPTSTR LPVALUENAME, / / ​​Address of Name of Value To Query

LPDWORD LPRESERVED, // RESERVED

LPDWORD LPTYPE, // Address of Buffer for Value Typelpbyte LPDATA, / / ​​Address of Data Buffer

LPDWORD LPCBDATA / / Address of Data Buffer Size;

example:

RegQueryValueex (HD, Valuename, Null, & K, VARI, & NCBVARI);

Variable definition and successful variable setting value with RegenumValueex

Write key value

Long RegsetValueex (HKEY HKEY, // Handle To Key To Set Value for

LPCTSTR LPVALUETS, // Name of The Value To Set

DWORD RESERVED, // Reserved

DWORD DWTYPE, / / ​​FLAG for Value Type

Const Byte * LPData, // Address of Value Data

DWORD CBDATA / / SIZE OF VALUE DATA);

example:

STRCPY (Valuename, "Hello");

Unsigned char vari [10];

DWORD K = REG_SZ;

STRCPY (CHAR *) VARI, "1234567")

RegSetValueex (HD, Valuename, 0, K, VARI, 7);

After success, add a key value in Poker Hello: REG_SZ: 1234567

Write integer variable:

INT HI = 8;

RegSetValueex (pj, valuename, 0, reg_binary, (unsigned char *) & hi, sizeof (int));

After successful, add a key value under Poker Hello2: reg_binary: 08 00 00 00

Void addeventsource ()

{

HKEY HK;

DWORD DWDATA;

Uchar szbuf [80];

// Add your source name as a subkey under the application

// Key in The EventLog Registry Key.

IF (REGCREATEKEY (HKEY_LOCAL_MACHINE,

System // CurrentControlSet // Services /

// EventLog // Application // Samplapp ", & hk)

"" COULD NOT CREATE THE REGISTRY Key. ");

// set the name of the message file.

STRCPY (SZBUF, "% systemroot% // system // samplapp.dll");

// add the name to the EventMessageFile Subkey.

IF (RegSetValueex (HK, // Subkey Handle

"EventMessageFile", // value name

0, // must be Zero

REG_EXPAND_SZ, / / ​​VALUE TYPE

(Lpbyte) SZBUF, // Pointer to Value Data

Strlen (SZBUF) 1)) // Length of Value Data

ErroRexit ("Could Not Set The Event Message File.");

// set the supported event type in the type = EventLog_ERROR_TYPE | EVENTLOG_WARNING_TYPE |

EventLog_information_type;

IF (RegSetValueex (HK, // Subkey Handle

"Typessupported", // Value Name

0, // must be Zero

REG_DWORD, / / ​​VALUE TYPE

(Lpbyte) & dwdata, // Pointer to Value Data

Sizeof (dword))) // Length of Value Data

"" "Could Not Set The Supported Types.");

RegcloseKey (HK);

}

The following code is rewritten to C: /DK1/ATM/harp/exatmshell.exe:

HKEY HKEY;

Long res;

DWORD DATATYPE = REG_SZ;

Unsigned char szvalue [_max_path];

STRCPY ((char *) Szvalue, "C: //dk1//ATM//harp/ /exatmshell.exe");

Res = :: regopenkeyex (HKEY_LOCAL_MACHINE,

"Software // Microsoft // Windows NT // CurrentVersion // WinLogon ///////////////////////////////////////////////////////>

Key_WRITE | Key_Read, & HKey

IF (res! = error_success)

{

AfxMessageBox ("AAA");

Return;

}

Res = :: RegSetValueex (HKEY, "Shell", 0, DataType, Szvalue, Strlen (LPCSTR (SzValue));

RegcloseKey (HKEY);

IF (res == error_success)

:: AFXMessageBox ("You have successfully set the registry from the key value of the Shell to C: //dk1//atm//harp//exatmshell.exe");

Else

:: AfxMessageBox ("Set failed: There is no such key!");

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

New Post(0)