C++ - GetUserName() when process is run as administrator

前端 未结 2 1836
抹茶落季
抹茶落季 2020-11-28 16:11

I have a simple C++ program that prompt the user name

#include 
#include 
#include 

int _tmain(int argc, _T         


        
相关标签:
2条回答
  • 2020-11-28 16:49

    I believe the question you want to ask Windows is "which user is logged into the current session".

    To do this, call ProcessIdToSessionId() with your own process's ID to determine the current session ID.

    Then call WTSQuerySessionInformation() with the WTSUserName option to fetch the user name.

    0 讨论(0)
  • 2020-11-28 16:51

    The problem is not a thread which is impersonating. You're running the entire application under the administrator login. That's why Windows asked you to login with an administrator account, when you started it from a non-admin account.

    Thus, the result you get from GetUserName() is correct. That name is the real logged-in user of your app.

    If you wanted the other name, the standard solution is to start as a normal user, and have an "elevate" button which restarts your application with elevated privileges. Look as Task Manager, it does this if you want to see all running processes. At this point you can of course pass anything you want to the new process, including that user name.

    0 讨论(0)
提交回复
热议问题