I have pretty much the same problem as discussed in WTSQueryUserToken always throws "An attempt was made to reference a token that does not exist" on Windows 7 in C#, but unlike the OP of that question, I'm using C++ on Windows 10 and read the docs properly before starting to code my solution. So my service is most-definitely running under the LocalSystem
account.
Here's the relevant part of my SvcInit()
function:
HANDLE hToken;
// Returns 1, just like in the linked question
DWORD sessionId = WTSGetActiveConsoleSessionId();
if (!WTSQueryUserToken(sessionId, &hToken)) {
// LogError() takes the name of an error-causing function and calls
// GetLastError() and FormatMessage() to get the system-defined error
// message, then logs all of that to a file
LogError("WTSQueryUserToken");
return;
}
The docs also mention a need for your service process to have the SE_TCB_NAME
privilege. The same paragraph that the other question references:
Obtains the primary access token of the logged-on user specified by the session ID. To call this function successfully, the calling application must be running within the context of the LocalSystem account and have the SE_TCB_NAME privilege.
But by reading https://learn.microsoft.com/en-us/windows/win32/services/localsystem-account, it seems to me as though any process running under the LocalSystem
account would automatically have this privilege:
The LocalSystem account has the following privileges:
- SE_ASSIGNPRIMARYTOKEN_NAME (disabled)
- SE_AUDIT_NAME (enabled)
- SE_BACKUP_NAME (disabled)
- ...many others
- SE_TCB_NAME (enabled)
So do I need to explicitly add this privilege to my process or not? And what else may be the cause of my issue? MTIA! :-)