C + + Read the registry

Source: Internet
Author: User

Was asked by a friend yesterday to help write a program that reads the registry in C + +. This friend is engaged in Java, certainly do not know how to write C + +! His demand is also strange: call a browser plugin with JS, through the plug-in to get the value of the specified key in the registry, the plugin must be written in C + +.

So I looked it up on the Internet "C + + Read the Registry", a search a large but are some beginners C + + people write, most can not use, and that the program is really powerless to spit groove ... Rage I spent one hours writing a, crap don't say, directly on the code:

C + + Read the registry

GetRegValue.h:

#ifndef __getregvalue_h__#define __GETREGVALUE_H__#include <string>//---------------------------------------------------------------//function://GetRegValue Gets the value of the specified key in the registry//access:// public//parameter://[in] int nkeytype-The type of the registry key, the passed-in parameter may be the following value only://0:hkey_classes_root//1:hkey_current_user//2:hkey_local_machine//3:hkey_users//4:hkey_performance_data//5:hkey_current_config//6:hkey_dyn_data//7:hkey_current_user_local_settings//8:hkey_performance_text//9:hkey_performance_nlstext//[in] const std::string & strURL-The path of the key to find//[in] const std::string & strkey-specified key//returns://std::string-Specify the value of the key//remarks://          ...//author:luoweifu//---------------------------------------------------------------STD::stringGetRegValue (intNkeytype,Const STD::string& strURL,Const STD::string& Strkey);//Portable version wstring + = stringSTD::stringWs2s (Const STD::wstring& ws);//Portable Version string = wstringSTD:: Wstring S2ws (Const STD::string& S);#endif //__getregvalue_h__ 

GetRegValue.cpp

#include "stdafx.h"#include <Windows.h>#include "GetRegValue.h"//Portable version wstring + = stringSTD::stringWs2s (Const STD::wstring& ws) {STD::stringCurlocale = setlocale (Lc_all,"");Const wchar_t* _source = Ws.c_str (); size_t _dsize = wcstombs (NULL, _source,0) +1;Char*_dest =New Char[_dsize];memset(_dest,0, _dsize); Wcstombs (_dest,_source,_dsize);STD::stringresult = _dest;Delete[]_dest; SetLocale (Lc_all, Curlocale.c_str ());returnResult;}//Portable Version string = wstringSTD:: Wstring S2ws (Const STD::string& S) {STD::stringCurlocale = setlocale (Lc_all,"");Const Char* _source = S.c_str (); size_t _dsize = mbstowcs (NULL, _source,0) +1;wchar_t*_dest =New wchar_t[_dsize]; Wmemset (_dest,0, _dsize); MBSTOWCS (_dest,_source,_dsize);STD:: wstring result = _dest;Delete[]_dest; SetLocale (Lc_all, Curlocale.c_str ());returnResult;}STD::stringGetRegValue (intNkeytype,Const STD::string& strURL,Const STD::string& Strkey) {STD::stringstrvalue ("");    HKEY HKEY = NULL;    HKEY Hkeyresult = NULL; DWORD dwsize =0; DWORD Dwdatatype =0;STD:: Wstring wstrurl = S2ws (strURL);STD:: Wstring Wstrkey = S2ws (strkey);Switch(Nkeytype) { Case 0: {HKey = HKEY_CLASSES_ROOT; Break; } Case 1: {HKey = HKEY_CURRENT_USER; Break; } Case 2: {HKey = HKEY_LOCAL_MACHINE; Break; } Case 3: {HKey = HKEY_USERS; Break; } Case 4: {HKey = Hkey_performance_data; Break; } Case 5: {HKey = hkey_current_config; Break; } Case 6: {HKey = Hkey_dyn_data; Break; } Case 7: {HKey = hkey_current_user_local_settings; Break; } Case 8: {HKey = Hkey_performance_text; Break; } Case 9: {HKey = Hkey_performance_nlstext; Break; }default:        {returnstrvalue; }    }//Open the registration form    if(Error_success = =:: RegOpenKeyEx (HKey, Wstrurl.c_str (),0, Key_query_value, &hkeyresult)) {//Gets the length of the cache dwsize and type Dwdatatype:: RegQueryValueEx (Hkeyresult, Wstrkey.c_str (),0, &dwdatatype, NULL, &dwsize);Switch(Dwdatatype) { CaseREG_MULTI_SZ: {//Allocate memory sizebyte* Lpvalue =NewByte[dwsize];//Gets the value corresponding to the key specified in the registryLONG Lret =:: RegQueryValueEx (Hkeyresult, Wstrkey.c_str (),0, &dwdatatype, Lpvalue, &dwsize);Delete[] lpvalue; Break; } CaseREG_SZ: {//Allocate memory size                wchar_t* Lpvalue =New wchar_t[dwsize];memset(Lpvalue,0, dwsize *sizeof(wchar_t));//Gets the value corresponding to the key specified in the registry                if(Error_success = =:: RegQueryValueEx (Hkeyresult, Wstrkey.c_str (),0, &dwdatatype, (LPBYTE) Lpvalue, &dwsize)) {STD:: Wstring wstrvalue (Lpvalue);                strvalue = Ws2s (Wstrvalue); }Delete[] lpvalue; Break; }default: Break; }    }//Close the registration form:: RegCloseKey (Hkeyresult);returnstrvalue;}

Test code:

#include "stdafx.h"#include <string>#include "GetRegValue.h"int _tmain(int argc, _TCHAR* argv[]){    std::string strValue = GetRegValue(2"SOFTWARE\\360Safe\\Liveup""mid");    return0;}

Results:
strvalue:
"EBD1360403764C9D48C585EF93A6EACBD89DED596F043F78E54EB0ADEBA7251D"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permitted not for any commercial use, reproduced please indicate the source.

C + + Read 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.