Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;Process32Next只返回相同的进程 我一直试图开发一个C++的LIB,目的是只通过名字来杀死进程。如某些地方所述,我必须执行系统快照,然后列出每个进程(按其ID),打开该进程并检查其文件名是否与传递给函数的文件名相同,如果是,则将其杀死_C++_Winapi_Process_Kill - Fatal编程技术网

C++;Process32Next只返回相同的进程 我一直试图开发一个C++的LIB,目的是只通过名字来杀死进程。如某些地方所述,我必须执行系统快照,然后列出每个进程(按其ID),打开该进程并检查其文件名是否与传递给函数的文件名相同,如果是,则将其杀死

C++;Process32Next只返回相同的进程 我一直试图开发一个C++的LIB,目的是只通过名字来杀死进程。如某些地方所述,我必须执行系统快照,然后列出每个进程(按其ID),打开该进程并检查其文件名是否与传递给函数的文件名相同,如果是,则将其杀死,c++,winapi,process,kill,C++,Winapi,Process,Kill,这是我的代码: 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" <<

这是我的代码: 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;
}
#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
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;
    
}
正如您所看到的,程序的读取量似乎不超过第一个进程

谢谢你抽出时间

编辑:正如您可能看到的,szExeFile应该是类似cmd.exe(exe名称)的东西,但它可能是某种地址


EDIT2:另外,请注意TCHAR,根据编译器设置,它应该被ANSI/UNICODE字符/wchart替换。

根据@BarmakShemirani的指定,我将使用UNICODE函数作为wcout或wcscmp。
谢谢。

如果(prochHandler.szExeFile==procname)
没有达到预期效果,请尝试使用调试器并单步执行代码。您的代码有大量错误。我不能让自己去看这个,因为太多了,太多了。我无法面对它。尤其是当他们被指出来的时候,你没有注意到。我没有注意到吗?来自msdn:szExeFile进程的可执行文件的名称,因此对于它返回的内容没有明显的解释。我要再次指出,仅仅说“有这么多”是绝对没有帮助的,最好不要评论。第一条评论指出==是错误的。它甚至是用你自己的代码写在注释中的。如果你打算使用TCHAR,那就错了。您不应该使用TCHAR。使用Unicode。为了澄清,您应该将
TCHAR
替换为
WCHAR
WCHAR\u t
@HarryJohnston。因此,对于支持Unicode的程序,我将使用WCHAR/WCHAR\u t和Unicode函数,对于标准的非Unicode函数,只使用char和normal函数?谢谢。@PedroJavierFernández:其实没那么简单。您应该始终在Unicode模式下构建,并且几乎总是对Win32 API函数使用
wchar\u t
。您的程序是否支持Unicode,例如,它使用的任何数据文件,以及如果支持Unicode,您应该如何在内部对其进行编码,这是一个完全不同的问题。您随时可以根据需要在内部编码和UTF-16之间进行转换。(一个警告:如果您需要可靠地处理文件名,例如,如果您正在编写文件系统实用程序,则需要将文件名保留在UTF-16中,因为转换它们可能会丢失信息。)
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