我一直在尝试开发一个C ++库,目的只是通过名称来杀死进程。如某处所述,我必须执行系统快照,然后列出每个进程(通过它的ID),打开进程并检查它的文件名是否与传递给函数的文件名相同,如果是,则将其删除。
这是我的代码: 的的main.cpp
#include <iostream>
#include "proman.hpp"
using namespace std;
int main(int argc, char* argv[])
{
cout << "INIT PRUBA" << endl;
cout << "Trying to kill cmd.exe" << endl;
const TCHAR* proc = L"cmd.exe";
killPBN(proc);
getchar(); getchar();
return 0;
}
proman.hpp
#ifndef INC_PROMAN_H
#define INC_PROMAN_H
#include <TCHAR.h> //http://www.codeproject.com/Articles/76252/What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc
#include <windows.h>
#include <tlhelp32.h>
#include <iostream>
#include "proman.cpp"
//Function prototypes
bool killPBN(const TCHAR *procname);
#endif
proman.cpp
bool killPBN(const TCHAR *procname)//
{
//Compiles, but doesnt work, probablemente el fallo este en el compare dentro del for(;;) ==
//Init some stuff
TCHAR processPath[MAX_PATH];
DWORD processId;
HANDLE tmpHandle = NULL;
BOOL b;
//Handle to store the snapshot
HANDLE systemSnap;
//Initialize to every process dump (no heap nor modules)
systemSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (systemSnap==INVALID_HANDLE_VALUE)
{
std::cout << "Can't snap system" << std::endl;
throw(NULL);
}
//We have our snapshot, we need to enumerate every process (get its ID)
//A custom handle to store process data
PROCESSENTRY32 procHandler;
//Initialize dwSize so Process32First won't fail
procHandler.dwSize = sizeof(PROCESSENTRY32);
//We are ready to call Process32First
Process32First(systemSnap, &procHandler);
//Got our first process, now, dump it's ID and ExeFile
//processPath = procHandler.szExeFile; //Exe Path
//processId = procHandler.th32ProcessID; //PID
std::cout << "First Proc: " << procHandler.szExeFile << std::endl;
if (procHandler.szExeFile == procname) //parameter here, use strncompare o como sea
{
//Try to open process
tmpHandle = OpenProcess(PROCESS_ALL_ACCESS, TRUE, procHandler.th32ProcessID);
//If opened, kill process
if (NULL != tmpHandle)
{
std::cout << "Killing: " << procHandler.szExeFile << std::endl;
TerminateProcess(tmpHandle, 0);
return 0;
}
//If not opened, throw expection of no admin rights
else
{
//no admin rights?
//throw(std::out_of_range);
std::cout << "Can't Kill Proc: " << procHandler.szExeFile << std::endl;
}
}
else
{
while(1==1)
{
//The process wasn't the one we were looking for, look next
b = Process32Next(systemSnap, &procHandler);
if (b==FALSE)
{/*throw(proc_not_found);*/
std::cout << "No more processes" << std::endl;
break;
} //no deberia hacer throw, eso solo si hay un error, aqui usar un return!!! si no hay mas procesos
else
{
if (procHandler.szExeFile == procname)
{
tmpHandle = OpenProcess(PROCESS_ALL_ACCESS, TRUE, procHandler.th32ProcessID);
if (NULL != tmpHandle)
{
TerminateProcess(tmpHandle, 0);
}
}
else
{
std::cout << "Can't kill " << procHandler.szExeFile <<std::endl;
//throw(std::out_of_range); //no admin rights?
}
}
}
}
std::cout << " Exit Success";
//Deletes snapshot and tmpHandle
CloseHandle(systemSnap);
CloseHandle(tmpHandle);
return 0;
}
我知道代码有点大。想法是看第一个过程,如果不是想要的过程,看下一个过程。但是,这是输出:
INIT PRUBA
Trying to kill cmd.exe
First Proc: 00C1F510
Can't kill 00C1F510
Can't kill 00C1F510
Can't kill 00C1F510
Can't kill 00C1F510
Can't kill 00C1F510
Can't kill 00C1F510
Can't kill 00C1F510
Can't kill 00C1F510
Can't kill 00C1F510
Can't kill 00C1F510
Can't kill 00C1F510
...
No more processes
Exit Success
正如您所看到的,似乎该程序读取的内容不仅仅是第一个过程。
谢谢你的时间, 佩德罗。
编辑:你可能看起来,szExeFile应该像cmd.exe(exe名称),但它可能是某种地址吗?EDIT2:另外,请注意TCHAR,它应该由ANSI / UNICODE char / wchart替换,具体取决于编译器设置。
答案 0 :(得分:1)
由@BarmakShemirani指定,我将使用Unicode函数作为wcout或wcscmp。 感谢。