The registry of Windows 95 and NT is an important component of the system. There is a set of Reg functions in Win32 API to handle these problems. The General read/write process is as follows:
1. Use the regopenkeyex or regcreatekeyex function to open or create a key; 2. If the previous step is successful, use regqueryvalueex to read the sub-key value, use regsetvalueex to set the sub-key value, use regenumkey to obtain all the sub-keys, and use regdeletekey to delete a key; 3. Use regclosekey to close the key after completing the operation. The following section Program Open the HKEY_CURRENT_USER \ Software \ zeal softstudio \ askpro FTP \ lasttime key, and then read the value of the wol sub-key. Hkey; Char SZ [256]; Dwords 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 use the cregkey class to read and write the registry. For more information about how to call APIs in VB, see qa000226 "how to access the Windows Registry ". Open registration key Long regopenkeyex (hkey, // handle to open key Lptstr 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); // error_success is returned successfully, otherwise, an error Code is returned. disable the registration key long regclosekey (hkey // handle to key to close); example: regclosekey (HKEY_LOCAL_MACHINE ); or: regclosekey (HD); Create a registration key long regcreatekeyex (hkey, // handle to an open key lptstr 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 handle lpdword lpdwdisposition // address of disposition value buffer); example: char * sclass = ""; // The class name is null. dword nbf = 0; // accept the returned value, indicating whether to create a new key or open an existing key. (reg_opened_existing_key is always returned after the test. long II = regcreatekeyex (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 retained. if the security structure is null, a default value is automatically obtained. // error_success is returned. Otherwise, an error code is returned. enumeration key value long regenumvalue (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]; // assign a value name buffer strcpy (valuename, "Regular Pattern "); // specify which key value name DWORD nsize = 69; // value name buffer size dword k = REG_SZ; // specify the Data Type unsigned char vari [70]; // assign a numerical buffer DWORD ncbvari = 69; // numeric buffer size dinx = 0; // starts from 0 While (II = regenumvalue (HD, dinx, valuename, & nsize, null, & K, vari, & ncbvari )) ! = Error_no_more_items) { Dinx ++; // index + 1, ready to take the next value Nsize = 69; // restore the original size Ncbvari = 69; } The return value is 0. the return value of each variable is as follows: Valuename = Value Name, ending with 0; for example, using color Nsize = Value Name Length, 9 K = REG_SZ deskcolor is of the REG_SZ type. Vari = key value, 32768 bytes color = "32768 ", Ncbvari = key value length REG_SZ includes end 0, = 6, Read key value Long regqueryvalueex (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 Type Lpbyte lpdata, // address of Data Buffer Lpdword lpcbdata // address of data buffer size ); Example: Regqueryvalueex (HD, valuename, null, & K, vari, & ncbvari ); After the variable is defined and successfully defined, the value of each variable is the same as that of regenumvalueex. Write key value Long regsetvalueex (hkey, // handle to key to set value Lpctstr lpvaluename, // 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); Add a key value "Hello: REG_SZ: 1234567" under poker. write the integer variable: int HI = 8; regsetvalueex (PJ, valuename, 0, REG_BINARY, (unsigned char *) & hi, sizeof (INT); Add a key value hello2: REG_BINARY: 08 00 00 under poker. 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 )) Errorexit ("cocould 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 ("cocould not set the event message file ."); // Set the supported event types in the typessupported subkey. Dwdata = 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 Errorexit ("cocould not set the supported types ."); Regclosekey (HK ); } Run the following code to rewrite the shell key of the Registry to C: \ DK1 \ ATM \ harp \ exatm shell.exe: Hkey; Long res; DWORD datatype = REG_SZ; Unsigned char szvalue [_ max_path]; Strcpy (char *) szvalue, "C: \ DK1 \ ATM \ harp \ exatm shell.exe "); Res =: regopenkeyex (HKEY_LOCAL_MACHINE, "SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ WinLogon \", 0, 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 self-starting shell key value to C: \ DK1 \ ATM \ harp \ exatm shell.exe "); Else : Afxmessagebox ("setting failed: the target location does not have such a key! "); |