开发者

Problems reading HID device from Windows 7 service (Session 0)

开发者 https://www.devze.com 2023-01-30 09:36 出处:网络
I have an problem reading from a HID device under a Session 0 service in Windows 7. Under a normal desktop user, I can read from the device fine. However, when I try to read fro开发者_JAVA技巧m it usi

I have an problem reading from a HID device under a Session 0 service in Windows 7. Under a normal desktop user, I can read from the device fine. However, when I try to read fro开发者_JAVA技巧m it using Overlapped I/O under Session 0, WaitForSingleObject() always returns WAIT_TIMEOUT, and the buffer to ReadFile never contains any valid data. This seems like a Session 0 isolation problem to me, but I can't find anything about how to work around this. Is reading from HID devices non-functional under Session 0?

Thanks.


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);
    }
}

Then open your device with the SQOS parameters:

ImpersonateConsoleSession(WTSGetActiveConsoleSessionId());
HANDLE hdevice = CreateFile(..., SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION, 0);
RevertToSelf();
0

精彩评论

暂无评论...
验证码 换一张
取 消