<tfoot id='zgsHl'></tfoot>

      <small id='zgsHl'></small><noframes id='zgsHl'>

    1. <legend id='zgsHl'><style id='zgsHl'><dir id='zgsHl'><q id='zgsHl'></q></dir></style></legend>
        • <bdo id='zgsHl'></bdo><ul id='zgsHl'></ul>
        <i id='zgsHl'><tr id='zgsHl'><dt id='zgsHl'><q id='zgsHl'><span id='zgsHl'><b id='zgsHl'><form id='zgsHl'><ins id='zgsHl'></ins><ul id='zgsHl'></ul><sub id='zgsHl'></sub></form><legend id='zgsHl'></legend><bdo id='zgsHl'><pre id='zgsHl'><center id='zgsHl'></center></pre></bdo></b><th id='zgsHl'></th></span></q></dt></tr></i><div id='zgsHl'><tfoot id='zgsHl'></tfoot><dl id='zgsHl'><fieldset id='zgsHl'></fieldset></dl></div>

        如何在 C++ 中按名称获取进程句柄?

        时间:2023-08-02

            <small id='etRKJ'></small><noframes id='etRKJ'>

              <bdo id='etRKJ'></bdo><ul id='etRKJ'></ul>
              <legend id='etRKJ'><style id='etRKJ'><dir id='etRKJ'><q id='etRKJ'></q></dir></style></legend>
                <tbody id='etRKJ'></tbody>
              <tfoot id='etRKJ'></tfoot>
                <i id='etRKJ'><tr id='etRKJ'><dt id='etRKJ'><q id='etRKJ'><span id='etRKJ'><b id='etRKJ'><form id='etRKJ'><ins id='etRKJ'></ins><ul id='etRKJ'></ul><sub id='etRKJ'></sub></form><legend id='etRKJ'></legend><bdo id='etRKJ'><pre id='etRKJ'><center id='etRKJ'></center></pre></bdo></b><th id='etRKJ'></th></span></q></dt></tr></i><div id='etRKJ'><tfoot id='etRKJ'></tfoot><dl id='etRKJ'><fieldset id='etRKJ'></fieldset></dl></div>

                • 本文介绍了如何在 C++ 中按名称获取进程句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试获取例如 example.exe 的进程句柄,以便我可以在其上调用 TerminateProcess.我怎样才能做到这一点?请注意,它没有窗口,所以 FindWindow 将不起作用.

                  I'm trying to get the process handle of, say example.exe, so I can call TerminateProcess on it. How can I do this? Notice, it doesn't have a window so FindWindow won't work.

                  推荐答案

                  #include <cstdio>
                  #include <windows.h>
                  #include <tlhelp32.h>
                  
                  int main( int, char *[] )
                  {
                      PROCESSENTRY32 entry;
                      entry.dwSize = sizeof(PROCESSENTRY32);
                  
                      HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
                  
                      if (Process32First(snapshot, &entry) == TRUE)
                      {
                          while (Process32Next(snapshot, &entry) == TRUE)
                          {
                              if (stricmp(entry.szExeFile, "target.exe") == 0)
                              {  
                                  HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);
                  
                                  // Do stuff..
                  
                                  CloseHandle(hProcess);
                              }
                          }
                      }
                  
                      CloseHandle(snapshot);
                  
                      return 0;
                  }
                  

                  另外,如果你想在 OpenProcess 中使用 PROCESS_ALL_ACCESS,你可以试试这个:

                  Also, if you'd like to use PROCESS_ALL_ACCESS in OpenProcess, you could try this:

                  #include <cstdio>
                  #include <windows.h>
                  #include <tlhelp32.h>
                  
                  void EnableDebugPriv()
                  {
                      HANDLE hToken;
                      LUID luid;
                      TOKEN_PRIVILEGES tkp;
                  
                      OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
                  
                      LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid);
                  
                      tkp.PrivilegeCount = 1;
                      tkp.Privileges[0].Luid = luid;
                      tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
                  
                      AdjustTokenPrivileges(hToken, false, &tkp, sizeof(tkp), NULL, NULL);
                  
                      CloseHandle(hToken); 
                  }
                  
                  int main( int, char *[] )
                  {
                      EnableDebugPriv();
                  
                      PROCESSENTRY32 entry;
                      entry.dwSize = sizeof(PROCESSENTRY32);
                  
                      HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
                  
                      if (Process32First(snapshot, &entry) == TRUE)
                      {
                          while (Process32Next(snapshot, &entry) == TRUE)
                          {
                              if (stricmp(entry.szExeFile, "target.exe") == 0)
                              {  
                                  HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);
                  
                                  // Do stuff..
                  
                                  CloseHandle(hProcess);
                              }
                          }
                      }
                  
                      CloseHandle(snapshot);
                  
                      return 0;
                  }
                  

                  这篇关于如何在 C++ 中按名称获取进程句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 C++ (Unicode) 中将 std::string 转换为 LPCWSTR 下一篇:C/C++ 中有哪些不同的调用约定,每个约定是什么

                  相关文章

                  最新文章

                    <legend id='wn17g'><style id='wn17g'><dir id='wn17g'><q id='wn17g'></q></dir></style></legend>
                      <bdo id='wn17g'></bdo><ul id='wn17g'></ul>

                    <i id='wn17g'><tr id='wn17g'><dt id='wn17g'><q id='wn17g'><span id='wn17g'><b id='wn17g'><form id='wn17g'><ins id='wn17g'></ins><ul id='wn17g'></ul><sub id='wn17g'></sub></form><legend id='wn17g'></legend><bdo id='wn17g'><pre id='wn17g'><center id='wn17g'></center></pre></bdo></b><th id='wn17g'></th></span></q></dt></tr></i><div id='wn17g'><tfoot id='wn17g'></tfoot><dl id='wn17g'><fieldset id='wn17g'></fieldset></dl></div>

                    <small id='wn17g'></small><noframes id='wn17g'>

                  1. <tfoot id='wn17g'></tfoot>