C + + Write registry

Source: Internet
Author: User

First, Description:
The registry is an important part of Windows, and the registry records a large amount of information about computer hardware and software. The value in the registry is identified by its name. The value name consists of characters that are the same as the key name. The value itself can be a string, binary data, or a 32-bit unsigned value. Here we mainly use the following techniques:

(a), pre-defined registry keys. The registry contains several predefined keys:

    1. The HKEY_LOCAL_MACHINE contains entries that describe the computer and its configuration. This includes information about the processor, system motherboard, memory, and installed software and hardware.
    2. HKEY_CLASSES_ROOT is a support key for information related to document types and ole\com. This key is the subordinate key of the HKEY_LOCAL_MACHINE.
    3. HKEY_USERS is used as the default user preference and as a preferred setting for individual users.
    4. Hkey_classes_user is information that is used for the current (logged on) user.
    5. The hkey_current_config contains information about the current system configuration.
    6. There is another, generally do not appear, only the configuration of the local area network will have

(ii), CRegKey class and main use and function description

  1. Required Header files: atlbase.h
  2. Common functions
    ☆ function to open a key: RegOpenKeyEx   function Definition:
    LONG RegOpenKeyEx (HKEY HKEY,/ /Handle of the key already opened, or directly above a few root keys                               lpctstr Lpsubkey,//  To open the address of the subkey name                               DWORD uloptions,// reserved value must be 0                               regsam samdesired,// open mode, Read or write                               phkey phkresult// return the handle of the Open subkey                              );

    ☆ Query A key value: RegQueryValueEx   function Definition:
    LONG RegQueryValueEx (HKEY HKEY,// handle of key to query                                  LPCTSTR lpvaluename,// name of key value to query                                  Lpdword lpreserved,// reserved Values                                  Lpdword Lptype,// type of data to query                                  lpbyte lpdata,// data to be returned for the query                                  lpdword Lpcbdata// Preset length of the data                                 );

    ☆ Set a key value RegSetValueEx   function definition:
    LONG RegSetValueEx (HKEY HKEY,// handle of the key to be set                                lpctstr lpvaluename,// name of the key value to access                                Lpdword lpreserved,// reserved Values                                DWORD dwtype,// type of data to set                                const BYTE *lpdata,// Health value to set                                DWORD cbdata// length of data                               );

Second, example:

  1. Creating dialog-based programs with AppWizard Regedit
  2. Set up two command buttons, called "Query" (id_query) and "Modify" (Id_change), to query and modify the user name and company name in the registry. Note: Information Location 9x series: \hkey_local_machine\software\microsoft\windows\currntversion NT series: \hkey_local_machine\software\ Microsoft\Windows nt\currntversion Key values: registeredownertkg and registeredorganization indicate user name and company name, respectively
  3. Set up two edit boxes for displaying and modifying information. Defines a CString variable of two m_strowner,m_company.
  4. The "Query" button code is as follows:
    voidCregeditdlg::onquery () {UpdateData (true); HKEY HKEY;//define the HKEY to close at the end of the query//Open hkey associated with path Data_setLPCTSTR data_set="software\\microsoft\\windows nt\\currentversion\\"; //to access the registry, HKEY holds the handle to the key opened by this function    LongRet0= (:: RegOpenKeyEx (Hkey_local_machine,data_set,0,key_read,&HKEY)); if(ret0!=error_success)//aborts execution of the program if HKEY cannot be opened{AfxMessageBox ("Error: Unable to open related hkey"); return; }    //querying the data aboutLpbyte owner_get=Newbyte[ the];//Define user name Owner_getDWORD TYPE_1=REG_SZ;//Defining data TypesDWORD cbdata_1= the;//Define data length    LongRet1=::regqueryvalueex (HKEY,"RegisteredOwner",null,&type_1,owner_get,&cbdata_1); if(ret1!=error_success) {AfxMessageBox ("Error: Unable to query registry information about"); return; }    //Search company NameLpbyte company_get=Newbyte[ the];//Define company name Company_getDWORD TYPE_2=REG_SZ;//Defining data TypesDWORD cbdata_2= the;//Define data length    LongRet2=::regqueryvalueex (HKEY,"RegisteredOrganization",null,&type_2,company_get,&cbdata_2); if(ret2!=error_success) {AfxMessageBox ("Error: Unable to query registry information about"); return; }    //Display InformationM_strowner=CString (Owner_get); M_strcompany=CString (Company_get); Delete[] owner_get; Delete[] company_get; //end of program, close open hkey:: RegCloseKey (HKEY); UpdateData (false);}
  5. //The "Settings" button code is as follows:
    voidcregeditdlg::onmodify () {UpdateData (true); HKEY HKEY;//define the HKEY to close at the end of the query//Open hkey associated with path Data_setLPCTSTR data_set="software\\microsoft\\windows nt\\currentversion\\"; //to access the registry, HKEY holds the handle to the key opened by this function LongRet0= (:: RegOpenKeyEx (Hkey_local_machine,data_set,0,key_read,&HKEY)); if(ret0!=error_success)//aborts execution of the program if HKEY cannot be opened{AfxMessageBox ("Error: Unable to open related hkey"); return; } //set the data about//Cstring_to_lpbyte, please refer to the following functionLpbyte owner_set=cstring_to_lpbyte (M_strowner);//Define user name Owner_setDWORD TYPE_1=REG_SZ;//Defining data TypesDWORD cbdata_1=m_strowner.getlength () +1;//Define data length LongRet1=::regsetvalueex (HKEY,"RegisteredOwner", null,type_1,owner_set,cbdata_1); if(ret1!=error_success) {AfxMessageBox ("Error: Unable to set registry information about"); return; } //Search company NameLpbyte company_set=cstring_to_lpbyte (M_strcompany);//Define company name Company_setDWORD TYPE_2=REG_SZ;//Defining data TypesDWORD cbdata_2=m_strcompany.getlength () +1;//Define data length LongRet2=::regsetvalueex (HKEY,"RegisteredOrganization", null,type_2,company_set,cbdata_2); if(ret2!=error_success) {AfxMessageBox ("Error: Unable to set registry information about"); return; } Else{AfxMessageBox ("Registry modification completed"); } //end of program, close open hkey:: RegCloseKey (HKEY); UpdateData (false);}

  6. reference function
       
     lpbyte cstring_to_lpbyte (CString str) {lpbyte LPB  =new  byte[str.    GetLength () +1   for  (int  i= 0 ; i<str.    GetLength (); I++) Lpb[i]  =str[i]; Lpb[str.    GetLength ()]  =0  ;  return   LPB;}  

    Iii. Note: The above code can only be passed in Windows NT/2000/XP, and Windows 9x/me will be implemented by changing Windows NT in the registry path to Windows. This program source code in Windows 2000+vc6.0 passed the test. This program source code sample file can only be used under Windows NT/2000/XP, please modify it in Windows 9x/me.

C + + writes to the registry

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.