I would like to know how to read a value from the registry. And store that value in a variable...
I use Bloodshed ide and ?mingw? compiler, NOT visual c++ so please do not give answers if they only apply to vc++.

Recommended Answers

All 8 Replies

The only requirement is that you should be working on a Windows OS and the compiler you use should have the required header files ( windows.h)

Also read this.

The only requirement is that you should be working on a Windows OS and the compiler you use should have the required header files ( windows.h)

Also read this.

To reinforce the information ~S.O.S~ has provided on the post linked to by 'this', you can referrence Microsofts website. Here is the linke: http://msdn2.microsoft.com/en-us/library/ms838625.aspx

Good luck, LamaBot

Okay, I appreciate the help, but could someone please just give me a quick example, say I want to read from the value
"HKEY_CURRENT_USER\something\more\value"
This is a made up value of course, but how would I do it...
I want the value stored in a variable called TheValue

Okay, I appreciate the help, but could someone please just give me a quick example, say I want to read from the value
"HKEY_CURRENT_USER\something\more\value"
This is a made up value of course, but how would I do it...
I want the value stored in a variable called TheValue

Bernt.tc did you care to click the link I'd provided above? The page contains sample code. Here is an example nevertheless:

#include <windows.h>
#include <malloc.h>

#define TOTALBYTES    8192
#define BYTEINCREMENT 1024

DWORD BufferSize = TOTALBYTES;
PPERF_DATA_BLOCK PerfData = NULL;

while( RegQueryValueEx( HKEY_PERFORMANCE_DATA,
                        TEXT("Global"),
                        NULL,
                        NULL,
                        (LPBYTE) PerfData,
                        &BufferSize ) == ERROR_MORE_DATA )
{
// Get a buffer that is big enough.

    BufferSize += BYTEINCREMENT;
    PerfData = (PPERF_DATA_BLOCK) realloc( PerfData, BufferSize );
}

The RegQueryValueEx retrievs the type and data of the value specified for what ever key you want to read from. Also, and just my opinion, when ever I have to write a program or script that is relatively simple I just write batch scripts using the REG command and associated operations (i.e. REG QUERY) and just check the %ERRORLEVEL% Global variable to see if a key exists. Anyway just a thought. Here is a link to the Microsoft website where you can find the above example as well as a more thorough description of RegQueryValueEx and other related Registry functions used in C\C++: http://msdn2.microsoft.com/en-us/library/ms724911.aspx

Good luck, LamaBot

Here is another example

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
    HKEY hKey = 0;
    char buf[255] = {0};
    DWORD dwType = 0;
    DWORD dwBufSize = sizeof(buf);
    const char* subkey = "Software\\Microsoft\\Office\\PowerPoint\\AddIns\\MultiMgrAddIn.AddInDesigner1";

    if( RegOpenKey(HKEY_LOCAL_MACHINE,subkey,&hKey) == ERROR_SUCCESS)
    {
        dwType = REG_SZ;
        if( RegQueryValueEx(hKey,"Description",0, &dwType, (BYTE*)buf, &dwBufSize) == ERROR_SUCCESS)
        {
            cout << "key value is '" << buf << "'\n";
        }
        else
            cout << "can not query for key value\n";
        RegCloseKey(hKey);

    }
    else
        cout << "Can not open key\n";
    cin.ignore();
       
    return 0;
}

Bernt.tc did you care to click the link I'd provided above? The page contains sample code. Here is an example nevertheless:

Yes I did read your link, but it provided a very unhelpful link, by this I mean, it gave an example, but no real explanation.
Your's is better.

A new problem has arrived, the key I want to read has spaces and for some reason it won't read it with the spaces

You can convert the key you receive into a C++ string and then do something like:

int main (void)
{
    using namespace std ;

    string key = "something     \ with   \ spaces           ." ;
    for ( int i = 0, j ; i < key.length( ) ; ++ i )
    {
        if ( key [i] == ' ' )
        {
            for ( j = i + 1; j < key.length ( ) ; ++j )
            {
                if ( key [j] != ' ' )
                    break ;
            }

            key = key.erase ( i, (j - i) ) ;
        }
    }

    cout << key ;
    getchar () ;
    return 0;
}

Though its a bit crude, but I think should serve your purpose.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.