void ImpersonateConsoleSession(DWORD dwSessionId)
{
PROCESSENTRY32 procEntry;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
procEntry.dwSize = sizeof(PROCESSENTRY32);
Process32First(hSnap, &procEntry);
DWORD winlogonPid = 0;
do {
if (!wcscmp(procEntry.szExeFile, L"winlogon.exe"))
{
DWORD winlogonSessId = 0;
if (ProcessIdToSessionId(procEntry.th32ProcessID, &winlogonSessId) && winlogonSessId == dwSessionId)
{
winlogonPid = procEntry.th32ProcessID;
break;
}
}
} while (Process32Next(hSnap, &procEntry) != 0);
CloseHandle(hSnap);
if (winlogonPid)
{
HANDLE hProcess = OpenProcess(MAXIMUM_ALLOWED, 0, winlogonPid);
HANDLE hPToken;
OpenProcessToken(hProcess, TOKEN_QUERY | TOKEN_DUPLICATE, &hPToken);
CloseHandle(hProcess);
HANDLE hUserTokenDup;
DuplicateTokenEx(hPToken, MAXIMUM_ALLOWED, 0, SecurityIdentification, TokenPrimary, &hUserTokenDup);
CloseHandle(hPToken);
ImpersonateLoggedOnUser(hUserTokenDup);
CloseHandle(hUserTokenDup);
}
}
Затем откройте ваше устройство с параметрами SQOS:
ImpersonateConsoleSession(WTSGetActiveConsoleSessionId());
HANDLE hdevice = CreateFile(..., SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION, 0);
RevertToSelf();