Reading values out of the Windows Registry

Anybody know how to write a C++ program that reads values from the windows registry? Specifically environment variables. I've found that reads environment variables but none of the code succesfully reads the PATH variable. It keeps getting truncated.
Unless things have changed radically in Windows 10, environment variables and the registry are different things.

It looks as though the function that NT3 referred you to will do what you want. If the PATH is being truncated, then I guess you should pay attention to the sizes if the strings you use.
Last edited on
To get the effective path for the process, either scan the parameter passed to main or use the function NT3 linked to.
This is the path from the environment that was set up by the caller of CreateProcess() or one of its variants.

It could be different from the the default path; the default environment variables have always been stored in the registry (all versions of windows).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <windows.h>
#include <cstring>

int main( int, char*[], char* envp[] )
{
    static const char path[] = "Path" ;

    {
        std::cout << "process-specific path for this process:\n----------------------------\n" ;
        for( std::size_t i = 0 ; envp[i] != nullptr ; ++i )
        {
            if( std::strncmp( envp[i], path, sizeof(path)-1 ) == 0 ) std::cout << envp[i] << '\n' ;
        }
    }

    static BYTE buffer[1000000] ; // lazy
    DWORD buffsz = sizeof(buffer) ;

    {
        std::cout << "\nsystem default path:\n----------------------------\n" ;

        // HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment Path=
        const char key_name[] = R"(System\CurrentControlSet\Control\Session Manager\Environment)" ;
        HKEY key ;
        if( RegOpenKeyExA( HKEY_LOCAL_MACHINE, key_name, 0, KEY_QUERY_VALUE, std::addressof(key) ) == 0 &&
            RegQueryValueExA( key, path, nullptr, nullptr, buffer, std::addressof(buffsz) ) == 0 )
        {
            std::cout << reinterpret_cast<const char*>(buffer) << '\n' ;
        }
    }


    {
        std::cout << "\nuser default path:\n----------------------------\n" ;

        // HKEY_CURRENT_USER\Environment Path=
        const char key_name[] = "Environment" ;
        HKEY key ;

        if( RegOpenKeyExA( HKEY_CURRENT_USER, key_name, 0, KEY_QUERY_VALUE, std::addressof(key) ) == 0 &&
            RegQueryValueExA( key, path, nullptr, nullptr, buffer, std::addressof(buffsz) ) == 0 )
        {
            std::cout << reinterpret_cast<const char*>(buffer) << '\n' ;
        }
    }
}
the default environment variables have always been stored in the registry (all versions of windows).

Oh, OK, I didn't know that. Does that mean that calls like GetEnvironmentVariable are effectively just wrappers to registry access functions?
> Does that mean that calls like GetEnvironmentVariable are effectively just wrappers to registry access functions?

No. GetEnvironmentVariable() reads the process environment block.

Each process has an environment block associated with it. The environment block consists of a null-terminated block of null-terminated strings (meaning there are two null bytes at the end of the block), where each string is in the form:
name=value
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682009(v=vs.85).aspx
Topic archived. No new replies allowed.