Print out the environment variables for the currently active user in C + + on a Windows system

Source: Internet
Author: User
Tags assert session id

Print out the environment variables for the currently active user on the Windows system in C + +, as follows (QT environment):

[CPP]View PlainCopy
  1. void Getenvironmentvariables ()
  2. {
  3. DWORD sessionId = Wtsgetactiveconsolesessionid ();
  4. Qinfo () << "Session ID =" << sessionId;
  5. HANDLE token;
  6. if (! Wtsqueryusertoken (SessionId, &token))
  7. {
  8. Qcritical () << Failed to get the user token of the session << SessionId;
  9. }
  10. wchar_t* penv = NULL;
  11. if (Createenvironmentblock ((void**) &penv, token, TRUE))
  12. {
  13. While (*penv) {
  14. //printf ("%ls\n", penv);
  15. Qinfo () << Qstring::fromwchararray (penv);
  16. Penv + = Wcslen (penv) + 1;
  17. }
  18. }
  19. }


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
    1. #include <Windows.h>
    2. #include <UserEnv.h>
    3. #include <assert.h>
    4. #include <stdio.h>
    5. #pragma comment (lib, "Userenv.lib")
    6. int main ()
    7. {
    8. HANDLE htoken = NULL;
    9. BOOL OK = OpenProcessToken (GetCurrentProcess (), Token_read, &htoken);
    10. ASSERT (OK);
    11. wchar_t* penv = L"";
    12. OK = Createenvironmentblock ((void**) &penv, Htoken, TRUE);
    13. ASSERT (OK);
    14. While (*penv) {
    15. printf ("%ls\n", penv);
    16. Penv + = Wcslen (penv) + 1;
    17. }
    18. return 0;
    19. }

http://blog.csdn.net/nirendao/article/details/52040232

Print out the environment variables for the currently active user in C + + on a Windows system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.