如何使用 c++ 代码终止在不同用户下运行的进程
How to kill Processes running under different users using c++ code
以下代码可以很好地显示在不同用户下运行的所有进程(例如:记事本.exe)的进程ID。但是当前用户下的进程单独被杀死。我需要杀死在不同用户下运行的所有进程。
#define SAMPLEAPP "notepad.exe"
void main()
{
KillProcessByName(SAMPLEAPP);
system("pause");
}
void KillProcessByName(const char *filename)
{
// Taking snapshot of all processes
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
//structure to capture each entry in snapshot
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof (pEntry);
//capture the first process in the list
BOOL hRes = Process32First(hSnapShot, &pEntry);
while (hRes)
{
char tempProcess[PROCESS_SIZE];// = pEntry.szExeFile;
wcstombs(tempProcess, pEntry.szExeFile, PROCESS_SIZE);
//if process name is equal to the process passed as argument to be killed
if (strcmp(tempProcess, filename) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
(DWORD) pEntry.th32ProcessID);
std::cout << "Process ID of the Process " << tempProcess << " is : " << pEntry.th32ProcessID;
if (hProcess != NULL)
{
// Kill the process
TerminateProcess(hProcess, 9);
CloseHandle(hProcess);
}
}
//Capture the next process in process snapshot
hRes = Process32Next(hSnapShot, &pEntry);
}
CloseHandle(hSnapShot);
}
如何终止进程,即使它属于其他用户?
右键单击您的程序,然后选择"以管理员身份运行"。
相关文章:
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 如何根据用户在C++中的输入运行不同数量的 for 循环
- 我可以根据用户输入在运行时生成"循环"吗?
- 使用 (cin) 用户输入将其粘贴到 std::system 中,并在另一个终端中运行带有输入的命令
- 是否可以在运行时调用用于选择要调用的用户定义文本的逻辑?
- C++ .我正在尝试通过使用开关内的数组来获取用户输入,但是当我运行代码时,它显示分段错误?
- 如何让程序只运行用户输入的号码?
- 在运行时使用用户定义的文本
- 是否有任何 C 函数或 API 来获取当前登录用户下运行的进程列表
- 指定在用户固定我的应用程序时用于运行应用程序的命令参数
- 如何让访问www-data用户运行使用 /dev /ttys1的流程
- 如何在背景中运行vtkrenderwindow而不向用户显示
- 根据用户输入或条件重新运行 c++ 程序
- 以下代码运行后,如何在程序再次运行之前清除用户声明的变量 empName
- 根据用户的输入在运行时创建对象
- 作为不同用户运行的两个exe如何使用COM(组件对象模型)进行通信
- Ubuntu,libftdi特权,非root用户运行程序时seg错误
- 直接阻止用户运行程序
- 以另一个用户运行程序
- 尝试执行"cc1"时 gcc 错误:execvp:使用非 root 用户运行时没有这样的文件或目录