How to use registry in VC ++

zhaozj2021-02-11  213

How to use registry in VC

Now modifying the registry to become another hot spot after overclocking, many CFANs make Win98 more personalized by modification of the registry, many newspapers and magazines have also pulling the flag of the registry, regretting, in the introduction In many articles modified by the registry, most of the articles have been manually modified, and very few articles only introduce the usage of modifying the registry software, as for how to implement in the program but not mentioned. To this end, I will search with the Registry keyword in the Visual Studio6.0 help file. With the help of the translation software and the continuous online practice, I finally explore some usage of the registry interface (API) function, now come out Sharing with everyone. Use registry in the program, nothing to create, open, read, set, and delete these five regular operations, (To get more detailed information) can refer to the Visual Studio6.0 Help Directory MSDN Library Visual Studio6.0 | Platform SDK | WINDOW BASE Services | General Library | Registry entry). Let's pay attention to the usage of these five interface functions: 1. Creating a key REGCREATEKEYEX can create a key in the registry through the RegreateKeyex function. If you need to create the button already exist, open the key. The following function prototype: LONG RegCreateKeyEx (HKEY hKey, LPCTSTR lpSubKey, DWORD Reserved, LPTSTR lpClass, DWORD dwOptions, REGSAM samDesired, LPSECURITY_ATTRIBUTESlpSecurityAttributes, PHKEY phkResult, LPDWORD lpdwDisposition); each of the parameters and return values ​​of the following meanings: · hKey main key, can take some of the following values: HKEY_CLASSES_ROOT, HKEY_CURRENT_CONFIG HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE HKEY_USER, HKEY_PERFORMANCE_DATA (WINNT OS) HKEY_DYN_DATA (WIN9X OS) · parameter lpSubKey as a pointer to a string of zeros at the end of which comprises a sub to be created or opened The name of the key. The subkey can not be started with a reverse slope (/). This parameter can be NULL. · Reserved reserves, must be set to 0. • Parameter lpclass a string pointing to the key type. This parameter is ignored if the key already exists. • Parameters dwoptions Set a certain attribute for the newly created key. You can take some of the following values: REG_OPTION_NON_VOLATILE The newly created key is a non-short-term key (data information is saved in the file, when the system restarts, data information recovery) REG_OPTION_VOLATILE newly created keys for a transient key (data Information is saved in memory). Windows95 ignores this value. REG_OPTION_BACKUP_RESTORE is only supported in WinNT and provides priority support.

· SamDesired parameter used to set permissions on the access key, can take some of the following values: KEY_CREATE_LINK symbol key generation permission grant KEY_CREATE_SUB_KEY generating subkeys generated permit the enumeration KEY_ENUMERATE_SUB_KEYS subkeys KEY_EXECUTE KEY_NOTIFY permits read operations permit replacement notice KEY_QUERY_VALUE permit inquiry subkeys KEY_ALL_ACCESS provide full access, it is a combination KEY_READ values ​​above are combinations of numerical values ​​the following: KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, KEY_NOTIFY KEY_SET_VALUE allowed to set the value KEY_WRITE sub-key is a combination of numerical values ​​the following: KEY_SET_VALUE, KEY_CREATE_SUB_KEY · parameter lpSecurityAttributes a pointer to SECURITY_ATTRIBUTES structure, Determine whether the returned handle is inherited. If the parameter is NULL, the handle cannot be inherited. In WinNT, this parameter can add security description for the newly created key. · Parameter phkResult is a pointer to a handle pointing to the newly created or open key. • Parameter lpdwdispition indicates whether it is created or opened, it can be some of the values: REG_CREATE_NEW_KEY button, no existence, now being created. The REG_OPENED_EXISTING_KEY button has now existed and is now opened. • Return Value If the function call is successful, return ERROR_SUCCESS. Otherwise, the return value is a non-zero error code defined in the file WINERROR.H, which can be called the formatMessage function by setting the format_message_from_system identity to acquire an overall description of the error. Second, opens a key RegOpenKeyEx RegOpenKeyEx function can open a specified key, the function prototype is as follows: LONG RegOpenKeyEx (HKEY hkey, LPCTSTR lpSubKey, DWORD ulOption, REGSAM samDesired, PHKEY phkResult); each of the parameters and the meaning of the return value is as follows: · by hKey The meaning of the HKEY parameter in the regreateKeyex function. • Parameter LPSubkey is a pointer to a string of zero end, which contains the name of the sub-key, and can separate different subkey names using the anti-sliding line (/). If the string is empty, create a new handle according to the HKEY parameter. In this case, the previously opened handle is not closed. · Uloption is reserved, usually must be set to 0. • The meaning of the parameter samdesired is the SamDesired parameter in the regcreateKeyex function. • The parameter phkResult is a pointer to point to the handle of the open key. This handle can be turned off through the Regclosekey function. • Return the value of the value of the regcreateKeyEx function. Third, the key is read by the RegQueryValueEx function RegQueryValueEx read from a data key has been opened, the following function prototype: LONG RegQueryValueEx (HKEY hKey, LPTSTR lpValueName, LPDWORD lpReserved, LPDWORD lpType, LPBYTE lpData, LPDWORD lpcbData); and various parameters The meaning of the return value is as follows: • Parameter HKEY is the handle of the currently open key, the specific value is the HKEY parameter of the RegreateKeyex function.

• Parameter LPVAULENAME is a string pointer to a name that contains query values. · LPRESERVED reserves, must be NULL. · LpType parameter is a pointer to a pointer data type, the data type is one of the following types: data REG_BINARY binary data REG_DWORD 32-bit integer REG_DWORD_LITTLE_ENDIAN little-endian format, such as 0X12345678 to (0X78 0X56 0X34 0X12) save mode format REG_DWORD_BIG_ENDIAN big-endian Data, such as 0x12345678 Save REG_EXPAND_SZ in (0x12 0x34 0x56 0x78) mode, a string REG_LINK, a Unicode type, REG_MULIT_SZ, REG_NON, Nothing value REG_RESOURCE_LIST device, use, REG_NONE, no type value REG_RESOURCE_SZ The string of the Unicode or ANSI type is set to the Unicode or ANSI type string with the character set type used by the zero end, and the parameter LPDATA is a pointer to the variable to save the return value. If the return value is not required, this parameter can be NULL. • Parameter lpcbdata is a pointer to a variable that holds the return value length. The length is in bytes. If the data type is REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ, then the length also includes a zero character ending, and only when the parameter LPDATA is NULL, the parameter LPCBDATA can be NULL. • Return the value of the value of the regcreateKeyEx function. Fourth, the setting key value RegSetValueex RegSetValueEx function can set the value of the key in the registry, the function prototype is as follows: Long RegsetValueEx (HKEY HKEY, LPCTSTSTLPVALUENAME, DWORD RESERVED, DWORD DWTYPE, Const Byte * LPDATA, DWORD CBDATA); Various parameters and return values The meanings are as follows: • The meaning of the parameter HKEY is the HKEY parameter in the regreateKeyex function. • Parameter LPVALUENAME is a string pointer to which the value is included. · Reserved reserves, usually set to 0. • Parameter DWTYPE determines the type of the set value with the lytype parameter of RegQueryValueKeyex. • Parameter LPDATA is a pointer to a buffer containing data. • Parameter CBDATA specifies the length of the data in bytes. • Return the value of the value of the regcreateKeyEx function. 5. Delete key value regdeKeteKey function regdeKetekey deletes a key and all sub-keys. The function prototype is as follows: Long RegdeleteKey (HKEY HKEY, LPCTSTR LPSUBKKKEY); the meaning of each parameter and return value is as follows: • The meaning of the parameter HKEY is the HKEY parameter in the regreateKeyex function. • The meaning of the parameter LPSUBKEY is the LPSUBKEY parameter in the RegcreateKeyex function. 6. Example The following we have newly built a dialog-based project in the Visual C 6.0 or 5.0 environment. Set two command buttons, named "Query User Information" and "Modify User Information" to query and modify the user name and company name in the registry.

It should be noted that the user's information is located in the system registry / key-current-user / software / micrSoft / MS setup (ACME) / user info / position, key-value name DefName and Defcompany represent the user's name and user company, respectively The name. 1. Query the code hkey hkey; // definition of the user information, and turn it off at the end of the query. LPCTSTR PATH = "Software // MicrSoft // MS Setup (ACME) // User Info //"; long return0 = (: regopenkeyex (HKEY_CURRENT_USER, PATH, 0, Key_Read, & HKey); if (return0! = Error_success) {MessageBox ("Error: Unable to open the related key!"); Return;} lpbyte username_get = new byte [80]; dword type_1 = reg_sz; dword cbdata_1 = 80; long return1 = :: RegQueryValueex (HKEY, "DefName :, NULL, & TYPE_1, UserName_Get, & CBData_1); if (Return1! = Error_Success) {MessageBox ("Error: Unable to check the registry information!"); Return;} lpbyte company_get = new byte [80]; dword type_2 = reg_sz; dword CBDATA_2 = 80; long return2 = :: regQueryValueex (HKEY, "DEFCompany", NULL, & TYPE_2, Company_GET, & CBDATA_2); if (Return2! = Error_Success) {MessageBox ("Error: Unable to query about checkp table information!"); Return ;} // convert username_Get and company_Get CString string is to display the output CString str_username = CString (username_Get); CString str_company = CString (company_Get); delete [] username_Get; delete [] company_Get; // end program has been closed Opened HKEY :: RegcloseKey (...... String str_username and str_company indicate the name of the user of the query and the name of the company. 2. Modify the code of the user information because the user outputs the string of the cstring type To convert it to the LPByte type, you should first call the function. The following is a conversion function: lpbyte cstring_to_lpbyte (cstring str) {lpbyte lpb = new byte [str.getLength () 1; for (int i = 0; ILPB [str.getlength ()] = 0;

Return LPB;

}

The following is the specific modification of the code for registry user information:

CString str_username, str_company;

HKEY HKEY;

LPCTSTR PATH = "Software // MicrSoft // MS Setup (ACME) // User Info //"; long return0 (: regopenkeyex (HKEY_CURRENT_USER, PATH, 0, KEY_WRITE, & HKEY);

if (Return0! = Error_Success)

{

MessageBox ("Error: Unable to open the relevant button!");

Return;

}

Lpbyte username_set = cstring_to_lpbyte (str_username);

DWORD TYPE_1 = REG_SZ;

DWORD CBDATA_1 = STR_USERNAME.GETLENGTH () 1;

Long return1 = :: regsetalueex (HKEY, "DefName", NULL, TYPE_1, Username_Set, CBDATA_1);

IF (Return1! = Error_Success)

{

MessageBox ("Error: Unable to modify the checkp table information!");

Return;

}

LPBYTE company_set = cstring_to_lpbyte (str_company);

DWORD TYPE_2 = REG_SZ;

DWORD CBDATA_2 = STR_Company.getLength () 1;

Long Return2 = :: Regsetalueex (HKEY, "Defcompany", NULL, TYPE_2, Company_SET, CBDATA_2);

IF (Return2! = Error_Success)

{

MessageBox ("Error: Unable to modify the checkp table information!");

Return;

}

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

New Post(0)