0

I have created my own header file in order to simply reading Regedit.

Here is my header file:

HKEY myHKEY;
long longResult;

long ReadValueRegedit(HKEY hKey, string strSubKey, string strValueName, DWORD dwValueType, int intArchitecture, string strValue)
{
    switch(intArchitecture)
    {
    case 32:
        {
            longResult = RegOpenKeyEx(hKey, strSubKey.c_str(), 0, KEY_READ | KEY_WOW64_32KEY, &myHKEY);
        }

    case 64:
        {
            longResult = RegOpenKeyEx(hKey, strSubKey.c_str(), 0, KEY_READ | KEY_WOW64_64KEY, &myHKEY);
        }
    }

    if(longResult == ERROR_SUCCESS)
    {
        char chValue[MAX_PATH];
        DWORD dwSize = MAX_PATH;

        longResult = RegQueryValueEx(myHKEY, strValueName.c_str(), 0, &dwValueType, (LPBYTE)chValue, &dwSize);

        if(longResult == ERROR_SUCCESS)
        {
           stringstream ssValue;
           ssValue << chValue;

           strValue = ssValue.str();
        }
    }

    RegCloseKey(hKey);

    return longResult;
}

I use my header file like this:

#include <conio.h>
#include <iostream>
#include <regedit.h> //My header file
using namespace std;

int main()
{
    string strDeger;

    long longSonuc = ReadValueRegedit(HKEY_CURRENT_USER, "SOFTWARE\\Disc Soft\\DAEMON Tools Lite", "InstallKey", REG_SZ, 32, strDeger);

    cout << "Okunan Deger:\t" << strDeger << "\nIslem Sonucu:\t" << longSonuc;

    getch();

    return 0;
}

The variable which is called strDeger, should show me content of the value which I want to regedit.

The variable which is called longSonuc, shows error code and it throws 0 (ERROR_SUCCESS).

But the problem is strDeger shows nothing. It should show c6045c6c-97c2-4429-a7d1-00b5bfd4d21e.

Click this link in order to display the output of my program.

So, what should I do in order to fix my issue?

  • "shows error code and it throws 0 (ERROR_SUCCESS)." -- can you include the actual observations which lead you to this conclusion? Also, it would help if you removed all code not necessary and merged it into a single file, i.e. a [mcve]. In general, you're lacking error handling. Maybe finding out what the actual error is would help. – Ulrich Eckhardt Jul 11 '21 at 07:17
  • @IInspectable Windows does not redirect HKEY_CURRENT_USER\Software. But Windows redirects HKEY_CURRENT_USER\Software\Classses to HKEY_CURRENT_USER\Software\WOW6432Node\Classes. And I also use KEY_WOW64_64KEY in order to bypass the redirection. – Ignacio Ponseti Jul 11 '21 at 07:21
  • that is no break after `case 32:` is ok ? – RbMm Jul 11 '21 at 08:05
  • 2
    strValue is passed by value, make it a reference param. Otherwise the value can't make its way to the caller. – David Heffernan Jul 11 '21 at 08:06
  • Make your functions return values (ideally a `std::wstring` here). Turn errors into exceptions. When doing C++ it's best to not avoid C++. – IInspectable Jul 11 '21 at 09:20
  • @DavidHeffernan Thank you so much. Actually, I did not use to know the meaning of "&" symbol. When I change "string strValue" to "string& strValue", my problem is fixed. – Ignacio Ponseti Jul 11 '21 at 09:24
  • Calling it a "header file" doesn't make it one. Don't use `#include` to pull in source files. That will lead to no end of problems. [Example](https://stackoverflow.com/questions/52062179/cpp-include-cpp-files-in-main-source-file-causes-duplicate-symbol-error). – Pete Becker Jul 11 '21 at 11:56
  • @PeteBecker So, what should I do? Should I convert this file to a dll file? – Ignacio Ponseti Jul 11 '21 at 12:11

1 Answers1

0

First of all, thanks for @DavidHeffernan in order to guide me.

Just adding ampersand symbol to in front of the strValue fixes the problem.

Here are corrected codes:

HKEY myHKEY;
long longResult;

long ReadValueRegedit(HKEY hKey, string strSubKey, string strValueName, DWORD dwValueType, int intArchitecture, string& strValue)
{
    switch(intArchitecture)
    {
    case 32:
        {
            longResult = RegOpenKeyEx(hKey, strSubKey.c_str(), 0, KEY_READ | KEY_WOW64_32KEY, &myHKEY);

            break;
        }

    case 64:
        {
            longResult = RegOpenKeyEx(hKey, strSubKey.c_str(), 0, KEY_READ | KEY_WOW64_64KEY, &myHKEY);

            break;
        }
    }

    if(longResult == ERROR_SUCCESS)
    {
        char chValue[MAX_PATH];
        DWORD dwSize = MAX_PATH;

        longResult = RegQueryValueEx(myHKEY, strValueName.c_str(), 0, &dwValueType, (LPBYTE)chValue, &dwSize);

        if(longResult == ERROR_SUCCESS)
        {
           stringstream ssValue;
           ssValue << chValue;

           strValue = ssValue.str();
        }
    }