How can I read the registry key value and print it on the screen using MessageBox ()

I am new to C ++ and WinCe.

I want to read a string from the registry and display using MessageBox() . I have tried the following.

 HKEY key; if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\GPS Intermediate Driver\\Drivers\\SiRFStar3HW"), 0, KEY_READ, &key) != ERROR_SUCCESS) { MessageBox(NULL,L"Can't open the registry!",L"Error",MB_OK); } char value[5]; DWORD value_length=5; DWORD type=REG_SZ; RegQueryValueEx(key,(LPCTSTR)"Baud", NULL, &type, (LPBYTE)&value, &value_length); wchar_t buffer[5]; _stprintf(buffer, _T("%i"), value); ::MessageBox(NULL,buffer,L"Value:",MB_OK); ::RegCloseKey(key); 

So, I know that something is wrong here, but how can I solve it?

+8
c ++ winapi
source share
5 answers

Win32 API navigation can be tricky. Registry APIs are some of the most complex. Here's a short program demonstrating how to read a registry string.

 #include <Windows.h> #include <iostream> #include <string> using namespace std; wstring ReadRegValue(HKEY root, wstring key, wstring name) { HKEY hKey; if (RegOpenKeyEx(root, key.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS) throw "Could not open registry key"; DWORD type; DWORD cbData; if (RegQueryValueEx(hKey, name.c_str(), NULL, &type, NULL, &cbData) != ERROR_SUCCESS) { RegCloseKey(hKey); throw "Could not read registry value"; } if (type != REG_SZ) { RegCloseKey(hKey); throw "Incorrect registry value type"; } wstring value(cbData/sizeof(wchar_t), L'\0'); if (RegQueryValueEx(hKey, name.c_str(), NULL, NULL, reinterpret_cast<LPBYTE>(&value[0]), &cbData) != ERROR_SUCCESS) { RegCloseKey(hKey); throw "Could not read registry value"; } RegCloseKey(hKey); size_t firstNull = value.find_first_of(L'\0'); if (firstNull != string::npos) value.resize(firstNull); return value; } int wmain(int argc, wchar_t* argv[]) { wcout << ReadRegValue(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion", L"CommonFilesDir"); return 0; } 

Notes:

  • I do not have CE, so this is a simple Win32 application compiled for Unicode. I took this route because CE does not execute ANSI characters.
  • I took advantage of a number of C ++ features. Most significantly std::wstring . This simplifies cinch string processing.
  • I used exceptions to handle errors. You could replace it with some other mechanism, but it was my goal to keep the error handling problems in the background.
  • Using exceptions makes closing a registry key a little dirty. The best solution would be to use the RAII class to end the life of the registry key. I skipped this for simplicity, but in production code you would like to take this extra step.
  • Normally RegQueryValueEx returns REG_SZ data with zero completion. This code deals with this by truncating beyond the first null character. If the return value is not terminated by zero, this truncation will not occur, but the value will still be good.
  • I just printed on my console, but it would be trivial for you to call MessageBox . For example: MessageBox(0, value.c_str(), L"Caption", MB_OK)
+19
source share

Here is the complete source code to read the registry key and print it on the screen:

 //Create C++ Win32 Project in Visual Studio //Project -> "project" Properties->Configuration Properties->C/C++->Advanced->Show Includes : YES(/ showIncludes) //Project -> "project" Properties->Configuration Properties->General->Project Defaults->Use of MFC : Use MFC in a shared DLL #include <iostream> #include <afx.h> using namespace std; int ReadRegistryKeyAttributes(CString ConstantKeyPath) { //Here ConstantKeyPath is considered as Registry Key Path to Read HKEY MyRegistryKey; if (RegOpenKeyEx(HKEY_CURRENT_USER, ConstantKeyPath, 0, KEY_READ, &MyRegistryKey) != ERROR_SUCCESS) { cout << "KeyOpen Failed" << endl; return -1; } DWORD type = REG_DWORD; DWORD cbData; unsigned long size = 1024; CString csVersionID; csVersionID = _T("VersionID"); //Here VersionID is considered as Name of the Key if (RegQueryValueEx(MyRegistryKey, csVersionID, NULL, &type, (LPBYTE)&cbData, &size) != ERROR_SUCCESS) { RegCloseKey(MyRegistryKey); cout << "VersionID Key Attribute Reading Failed" << endl; return -1; //Error } else { cout << "VersionID = " << cbData << endl; //Key value will be printed here. } return 1; //Success } int main() { int iResult; CString KeyPath = _T("Software\\RCD_Technologies\\Rajib_Test"); iResult = ReadRegistryKeyAttributes(KeyPath); if (iResult < 0) { cout << "ReadRegistryKeyAttributes operation Failed" << endl; return -1; } cout << "<--- ReadRegistryKeyAttribute Operation Successfull -->" << endl; getchar(); return 0; } 

We hope that this example will be useful to those who are looking for this problem.

+1
source share

This is untested (my device does not have your key / value), but it compiles for CE and gives you the gist of how you do what you need: #include

 int _tmain(int argc, _TCHAR* argv[]) { HKEY key; if(!RegOpenKeyEx( HKEY_LOCAL_MACHINE, _T("System\\CurrentControlSet\\GPS Intermediate Driver\\Drivers\\SiRFStar3HW"), 0, NULL, &key)) { MessageBox(NULL, _T("Failed to open key"), _T("Error"), 0); return -1; } DWORD length; // get the size - it going to be 4 for a DWORD, but this shows how to deal with REG_SZ, etc if(!RegQueryValueEx( key, _T("Baud"), NULL, NULL, NULL, &length)) { MessageBox(NULL, _T("Failed to get buffer size"), _T("Error"), 0); goto exit; } // allocate - again, if we know it a DWORD, this could be simplified BYTE *buffer = (BYTE*)LocalAlloc(LPTR, length); // query if(!RegQueryValueEx( key, _T("Baud"), NULL, NULL, buffer, &length)) { MessageBox(NULL, _T("Failed to get value data"), _T("Error"), 0); goto exit; } // assuming "baud" is a DWORD, not a string DWORD baud = *(DWORD*)buffer; // build an output TCHAR message[MAX_PATH]; _stprintf(message, _T("The baud value is %i"), baud); MessageBox(NULL, message, _T("Success"), 0); exit: RegCloseKey(key); return 0; } 
0
source share

When using the char array, you need to set not a buffer, but a pointer to a buffer, for example:

 MessageBox(0,&buffer,"Value:",MB_OK); 
-one
source share

just use RegQueryValueEx and put it in buf

-one
source share

All Articles