Print out the environment variables for the currently active user on the Windows system in C + +, as follows (QT environment):
[CPP]View PlainCopy
- void Getenvironmentvariables ()
- {
- DWORD sessionId = Wtsgetactiveconsolesessionid ();
- Qinfo () << "Session ID =" << sessionId;
- HANDLE token;
- if (! Wtsqueryusertoken (SessionId, &token))
- {
- Qcritical () << Failed to get the user token of the session << SessionId;
- }
- wchar_t* penv = NULL;
- if (Createenvironmentblock ((void**) &penv, token, TRUE))
- {
- While (*penv) {
- //printf ("%ls\n", penv);
- Qinfo () << Qstring::fromwchararray (penv);
- Penv + = Wcslen (penv) + 1;
- }
- }
- }
The above is the printing of the currently active user (active users).
If you are simply printing environment variables for the environment in which the current process is located, see the code for Visual Studio as follows:
[CPP]View PlainCopy
- #include <Windows.h>
- #include <UserEnv.h>
- #include <assert.h>
- #include <stdio.h>
- #pragma comment (lib, "Userenv.lib")
- int main ()
- {
- HANDLE htoken = NULL;
- BOOL OK = OpenProcessToken (GetCurrentProcess (), Token_read, &htoken);
- ASSERT (OK);
- wchar_t* penv = L"";
- OK = Createenvironmentblock ((void**) &penv, Htoken, TRUE);
- ASSERT (OK);
- While (*penv) {
- printf ("%ls\n", penv);
- Penv + = Wcslen (penv) + 1;
- }
- return 0;
- }
http://blog.csdn.net/nirendao/article/details/52040232
Print out the environment variables for the currently active user in C + + on a Windows system