Detailed description of c ++ reading the Registry

Source: Internet
Author: User

1. Read the Registry

// Read the Registry

CString ReadRegisteTable (CString root, CString path, CString key)

{

HKEY hAppKey;

LPCTSTR WINDS_SERVICE_REGISTRY_KEY = path;

LPCTSTR DATA_FILE_SUB_KEY = key;

Char szDataFile [80];

If (root = "HKEY_LOCAL_MACHINE ")

{

If (ERROR_SUCCESS = RegOpenKeyEx (

HKEY_LOCAL_MACHINE,

WINDS_SERVICE_REGISTRY_KEY,

0,

KEY_READ,

& HAppKey ))

{

ULONG cbSize = MAX_PATH * sizeof (TCHAR );

DWORD dwFlag = RegQueryValueEx (

HAppKey,

DATA_FILE_SUB_KEY,

NULL,

NULL,

(LPBYTE) szDataFile, & cbSize );

RegCloseKey (hAppKey );

If (ERROR_SUCCESS = dwFlag)

{

CString strDate = szDataFile; // MessageBox (strDate );

If (strDate. GetLength ()> 0) return strDate;

Else return "";

}

Return "";

}

}

If (root = "HKEY_CURRENT_USER ")

{

If (ERROR_SUCCESS = RegOpenKeyEx (

HKEY_CURRENT_USER,

WINDS_SERVICE_REGISTRY_KEY,

0,

KEY_READ,

& HAppKey ))

{

ULONG cbSize = MAX_PATH * sizeof (TCHAR );

DWORD dwFlag = RegQueryValueEx (

HAppKey,

DATA_FILE_SUB_KEY,

NULL,

NULL,

(LPBYTE) szDataFile, & cbSize );

RegCloseKey (hAppKey );

If (ERROR_SUCCESS = dwFlag)

{

CString strDate = szDataFile; // MessageBox (strDate );

If (strDate. GetLength ()> 0) return strDate;

Else return "";

}

Return "";

}

}

Return "";

} [Nextqage]

2. Write the Registry

// Modify the Registry

BOOL ModifyRegisteTable (CString root, CString path, CString key, CString value)

{

HKEY hAppKey;

DWORD dwDisposition = MAX_PATH * sizeof (TCHAR );

LPCTSTR WINDS_SERVICE_REGISTRY_KEY = path;

LPCTSTR DATA_FILE_SUB_KEY = key;

Char szDataFile [80]; strcpy (szDataFile, value );

If (root = "HKEY_LOCAL_MACHINE ")

{

If (ERROR_SUCCESS! = RegCreateKeyEx (

HKEY_LOCAL_MACHINE,

WINDS_SERVICE_REGISTRY_KEY,

0,

NULL,

REG_OPTION_NON_VOLATILE,

KEY_WRITE,

NULL,

& HAppKey,

& DwDisposition ))

{

Return false;

}

Else

{

If (ERROR_SUCCESS! = RegSetValueEx (hAppKey,

DATA_FILE_SUB_KEY,

0,

REG_SZ,

(LPBYTE) szDataFile,

(Lstrlen (szDataFile) + 1) * sizeof (TCHAR )))

{

Return false;

}

RegCloseKey (hAppKey );

}

}

If (root = "HKEY_CURRENT_USER ")

{

If (ERROR_SUCCESS! = RegCreateKeyEx (

HKEY_CURRENT_USER,

WINDS_SERVICE_REGISTRY_KEY,

0,

NULL,

REG_OPTION_NON_VOLATILE,

KEY_WRITE,

NULL,

& HAppKey,

& DwDisposition ))

{

Return false;

}

Else

{

If (ERROR_SUCCESS! = RegSetValueEx (hAppKey,

DATA_FILE_SUB_KEY,

0,

REG_SZ,

(LPBYTE) szDataFile,

(Lstrlen (szDataFile) + 1) * sizeof (TCHAR )))

{

Return false;

}

RegCloseKey (hAppKey );

}

}

Return true;

}

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.