从 Windows 7 服务读取 HID 设备时出现问题(会话 0)

发布于 2024-10-08 03:19:10 字数 248 浏览 2 评论 0原文

我在 Windows 7 中的会话 0 服务下读取 HID 设备时遇到问题。 在普通桌面用户下,我可以从设备上正常读取。但是,当我尝试在会话 0 下使用重叠 I/O 读取它时,WaitForSingleObject() 始终返回 WAIT_TIMEOUT,并且 ReadFile 的缓冲区从不包含任何有效数据。 对我来说,这似乎是一个 Session 0 隔离问题,但我找不到任何有关如何解决此问题的信息。在会话 0 下从 HID 设备读取数据是否不起作用?

谢谢。

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 from 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心头的小情儿 2024-10-15 03:19:10
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();
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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文