Registry Operation Summary

Source: Internet
Author: User

Example 1: Read the registry to obtain information about programs installed on the computer. In Windows, you can obtain information about the installer in the Registry HKEY_LOCAL_MACHINE \ SoftWare \ Microsoft \ Windows \ CurrentVersion \ Uninstall, in addition, xp, vista, win7, and win8 are all the same in the following example program: The structure ApplicationInfoA is used to record the specific information of each Installer. As to why A is added after the name, it is mainly used to indicate that all the information under it is recorded with a string. The GetAllInstalledAppInfoA function is used to obtain all information about the installed program on the computer. It accepts the parameter of the vector <ApplicationInfoA> reference type and stores all information in the vector. If the program is successfully executed, 0 is returned. If the program fails to be executed,-1 is returned. The main () function demonstrates how to use: vector <ApplicationInfoA> vAppInfo; GetAllInstalledAppInfoA (vAppInfo); after obtaining the information of the installer, output to the installedappinfo.txt file in the ddrive. [Cpp] # include <windows. h> # include <iostream> # include <TCHAR. H ># include <vector> using namespace std; /// structure used to record Installation Software Information // struct ApplicationInfoA {string strName; // software name string strDisplayName; // display the software name string strPublisher; // publisher string strVersion; // version string strDisplayVersion; // display version string strInstallLocation; // installation location }; //// obtain the key value of a specific program Data // hKey [in] // --- pointing to HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall sub-key handle // lpszKeyValueName [in] // --- the sub-key name under hKey // lpszKeyValueName [in] // --- just like its name, key Value Name // strKeyValue [out] // --- key value Data // int _ GetAppInfoA _ (HKEY hKey, LPCSTR lpszAppName, LPCSTR lpszKeyValueName, string & strKeyValue) {int ret; // open the registry key of the installed software ---------------------------------------------- HKEY hInstallAppKey; ret = RegOpenKeyEx (hKey, l PszAppName, 0, KEY_ALL_ACCESS, & hInstallAppKey); if (ret! = ERROR_SUCCESS) {return-1;} // obtain the key of the registry key of the installed software ---------------------------------------- // 1. obtain the string size (REG_SZ by default) DWORD dwKeyValueType = REG_SZ; DWORD dwKeyValueDataSize = 0; ret = values (hInstallAppKey, lpszKeyValueName, NULL, & dwKeyValueType, NULL, & dwKeyValueDataSize ); if (ret = ERROR_FILE_NOT_FOUND) {RegCloseKey (hInstallAppKey); return 0;} else if (ret! = ERROR_SUCCESS) {RegCloseKey (hInstallAppKey); return-1 ;}// 2. Obtain the string value if (dwKeyValueType! = REG_SZ) // if it is not of the string type, it is returned. Some installer items are not of the string type but other types. Ignore {RegCloseKey (hInstallAppKey); return 0 ;} LPSTR values = new char [dwKeyValueDataSize + 1]; memset (values, 0, dwKeyValueDataSize + 1); ret = values (hInstallAppKey, lpszKeyValueName, NULL, & dwKeyValueType, (LPBYTE) values, & dwKeyValueDataSize); if (ret! = ERROR_SUCCESS) {delete [] lpszKeyValueData; RegCloseKey (hInstallAppKey); return-1 ;}strkeyvalue = lpszKeyValueData; delete [] secret; // disable registry key secret RegCloseKey ); return 0 ;} //// obtain information about the program installed by the system and store it in the vector parameter. // if the execution succeeds, 0 is returned. // if the execution fails,-1 is returned. // int GetAllInstalledAppInfoA (vector <ApplicationInfoA> & vAppInfo) {int ret; // open the registry key- Export HKEY hKey; LPCSTR lpszSubKey = "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall"; ret = RegOpenKeyExA (HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, & hKey ); if (ret! = ERROR_SUCCESS) {return-1 ;}// obtain the subkey & key value information mongodword dwSubKeysCnt; // Number of subkeys DWORD dwMaxSubKeyNameLen; // the maximum length (not including the terminating null character) of the subkey name DWORD dwKeyValueCnt; // the number of key values DWORD dwMaxKeyValueNameLen; // the maximum length of the key value name (not including the terminating null character) DWORD dwMaxKeyValueDataLen; // the maximum length of the key value data (in Bytes) ret = RegQueryInfoKe Y (hKey, NULL, & dwSubKeysCnt, & dwMaxSubKeyNameLen, NULL, & dwKeyValueCnt, & dwMaxKeyValueNameLen, & dwMaxKeyValueDataLen, NULL, NULL); if (ret! = ERROR_SUCCESS) {RegCloseKey (hKey); return-1 ;}// list the sub-key information. Repeated DWORD dwIndex; LPSTR lpszSubKeyName = new char [dwMaxSubKeyNameLen + 1]; DWORD dwNameLen = forward + 1; for (dwIndex = 0; dwIndex <dwSubKeysCnt; ++ dwIndex) {dwNameLen = dwMaxSubKeyNameLen + 1; memset (lpszSubKeyName, 0, forward + 1 ); ret = RegEnumKeyEx (hKey, DwIndex, lpszSubKeyName, & dwNameLen, NULL); if (ret! = ERROR_SUCCESS) {RegCloseKey (hKey); delete [] lpszSubKeyName; return-1 ;} // ************* obtain the installation information of the specific program BEG **************** ApplicationInfoA appInfo; appInfo. strName = lpszSubKeyName; _ GetAppInfoA _ (hKey, lpszSubKeyName, "DisplayName", appInfo. strDisplayName); _ GetAppInfoA _ (hKey, lpszSubKeyName, "Publisher", appInfo. strPublisher); _ GetAppInfoA _ (hKey, lpszSubKeyName, "Version", appInfo. strVersion); _ GetAp PInfoA _ (hKey, lpszSubKeyName, "DisplayVersion", appInfo. strDisplayVersion); _ GetAppInfoA _ (hKey, lpszSubKeyName, "InstallLocation", appInfo. strInstallLocation); vAppInfo. push_back (appInfo ); // ************ obtain the installation information of a specific program END ***************} delete [] lpszSubKeyName; // close the registry key ---------------------------------------------------------- RegCloseKey (hKey); return 0;} int main () {cout <"Reg Demo Test" <Endl; vector <ApplicationInfoA> vAppInfo; cout <GetAllInstalledAppInfoA (vAppInfo) <endl; // output to the file vector <ApplicationInfoA >:: iterator iter = vAppInfo. begin (); FILE * fp = fopen ("D: \ InstalledAppInfo.txt", "a"); while (iter! = VAppInfo. end () {fprintf (fp, "---------------- \ n"); fprintf (fp, "Name: % s \ n DisplayName: % s \ n Publisher: % s \ n Version: % s \ n DisplayVersion: % s \ n InstallLocation: % s \ n ", iter-> strName. c_str (), iter-> strDisplayName. c_str (), iter-> strPublisher. c_str (), iter-> strVersion. c_str (), iter-> strDisplayVersion. c_str (), iter-> strInstallLocation. c_str (); fprintf (fp, "---------------- \ n"); + + iter;} fclose (fp); return 0 ;}

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.