user7877793
user7877793

Reputation:

c++ get process owner / current user token

I'm trying to get the current user token from a normal process (not elevated, not SYSTEM, etc). I've tried this using WTSQueryUserToken, but you need SYSTEM access for that so that won't work. And the only thing I find on the internet is getting the user SID or name but I couldn't find anything about getting the current user token. Is this even possible?

Upvotes: 3

Views: 6506

Answers (1)

Harry Johnston
Harry Johnston

Reputation: 36308

So, you want the token associated with the current process. This is straightforward:

if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &token_handle)) fail();

Note that you may need to specify different permissions, depending on what you are intending to do with the token once you've got it. The TOKEN_READ permission is appropriate if you are querying the token, e.g., to extract the username. If you want to enable privileges, another common use for the current process token, you need TOKEN_ADJUST_PRIVILEGE instead.

Upvotes: 4

Related Questions