c++ - 获取 winlogon.exe 的 session ID 和进程 ID

标签 c++ process

我正在尝试创建一个进程来启动需要 UI 的应用程序。所以它不能在 session 0中。 我的想法是获取当前登录用户的winlogon.exe的进程ID。通过这种方式,我可以复制 winlogon token 并使用 CreateProcessAsUser 函数运行我的应用程序。 到目前为止我的代码:(当需要我想要运行的应用程序时调用此代码)

#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>

this function()
{
  HANDLE hProcessSnap;
  HANDLE hProcess;
  PROCESSENTRY32 pe32;
  DWORD dwPriorityClass;

  // Take a snapshot of all processes in the system.
  hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );

  // Set the size of the structure before using it.
  pe32.dwSize = sizeof( PROCESSENTRY32 );

  //get the active session id
  DWORD sessionID = WTSGetActiveConsoleSessionId();

  // Now walk through the snapshot of processes
  //I want to narrow this down to processes called winlogon
  //if multiple users logged on system i want to make sure the active user
  //will get the application run the their screen
  do
  {
  // Retrieve the priority class.
    dwPriorityClass = 0;

    //here i want to compare the sessionID with session IDs of each winlogon process
    //stuck for implementation here
    //when i find a match i can use the processID to gain the token and create
    //a duplicate so it can be used in CreateAsUser function.
  }while( Process32Next( hProcessSnap, &pe32 ) );

 }

所以基本上我需要一些帮助将进程快照缩小到“winlogon”,并迭代这些进程的 session ID 以匹配事件用户的 sessionID。 提前致谢:D

最佳答案

您可以使用ProcessIdToSessionId获取与“winlogon.exe”匹配的每个进程的 session ID,然后将结果与 WTSGetActiveConsoleSessionId 进行比较.

这是您可以在循环中使用的片段:

if (_wcsicmp(pe32.szExeFile, L"winlogon.exe") == 0)
{
    DWORD ProcessSessionId = 0;
    ProcessIdToSessionId(pe32.th32ProcessID, &ProcessSessionId);
    if (ProcessSessionId == sessionID)
    {
        DoYourMagic(pe32.th32ProcessID);
        break;
    }
}

关于c++ - 获取 winlogon.exe 的 session ID 和进程 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13480344/

相关文章:

c++ - 两个 std::vector 的 union 读取访问冲突

c++ - 如何克服 mmsystem.h 抛出的错误

C# 在窗体面板中打开其他程序

c - 增加正在运行的进程的堆大小

node.js - Node - 如何使用 Node 中的回调处理未捕获的异常?

c++ - 临时和表达行为

c++ - 将消息解析为结构

c# - 从 .NET 执行 Cygwin 进程?

ruby - 如何在 Ruby 中控制子进程 stdin、stdout 等?

c++ - 变量作用域大于for循环,为什么不改变值?