简体   繁体   English

C ++无法获取进程ID(窗口)

[英]C++ Can't get process id (windows)

I have a function like this to get a process' id by it's name, however it always returns 0 with every process i try: 我有一个像这样的函数来获取进程'id的名字,但它总是返回0我尝试的每个进程:

DWORD GetProcessId(std::string ProcessName)
{
HANDLE hsnap;
PROCESSENTRY32 pt;
DWORD PiD;
hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
pt.dwSize = sizeof(PROCESSENTRY32);
do {
    if (!strcmp(pt.szExeFile, ProcessName.c_str())) {
        CloseHandle(hsnap);
        PiD = pt.th32ProcessID;
        return PiD;
        if (PiD != NULL) {
            return 0;
        }
    }
} while (Process32Next(hsnap, &pt));
return 1;
}

and main function: 主要功能:

int main()
{
DWORD pid = GetProcessId("calc.exe");
std::cout << pid;
if (pid == 0) { printf("error 1"); getchar(); }//error
return 0;
}

There are a couple of problems here. 这里有几个问题。

Firstly, GetProcessId is the name of a Windows API function, that takes a single HANDLE as a parameter. 首先, GetProcessId是Windows API函数的名称,它将单个HANDLE作为参数。 HANDLE is usually defined as void* and so what this means is that any pointer will satisfy the function signature. HANDLE通常被定义为void* ,因此这意味着任何指针都将满足函数签名。

Your own GetProcessId takes a std::string but you're calling it by passing it a pointer to a string constant. 你自己的GetProcessId接受一个std::string但是你通过向它传递一个指向字符串常量的指针来调用它。 Normally this would be OK, a std::string can be constructed from that, but because the Windows API version of GetProcessId already matches the function signature the compiler calls that in preference. 通常这样就可以了,可以从中构造一个std::string ,但是因为GetProcessId的Windows API版本已经匹配了编译器优先调用的函数签名。 So basically your own function is never actually being called. 所以基本上你自己的函数实际上从未被调用。

There are also a couple of problems with your own function. 你自己的功能也存在一些问题。 Firstly, your first iteration through the loop is comparing against garbage memory - you're forgetting to call Process32First , and so pt is uninitialised the first time through. 首先,你通过循环的第一次迭代是与垃圾记忆进行比较 - 你忘记调用Process32First ,所以pt在第一次没有初始化。 Secondly, if the process isn't found you're leaking the hsnap handle. 其次,如果找不到进程,则会泄漏hsnap句柄。

Try the following: 请尝试以下方法:

DWORD MyGetProcessId(LPCTSTR ProcessName) // non-conflicting function name
{
    PROCESSENTRY32 pt;
    HANDLE hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    pt.dwSize = sizeof(PROCESSENTRY32);
    if (Process32First(hsnap, &pt)) { // must call this first
        do {
            if (!lstrcmpi(pt.szExeFile, ProcessName)) {
                CloseHandle(hsnap);
                return pt.th32ProcessID;
            }
        } while (Process32Next(hsnap, &pt));
    }
    CloseHandle(hsnap); // close handle on failure
    return 0;
}

int main()
{
    DWORD pid = MyGetProcessId(TEXT("calc.exe"));
    std::cout << pid;
    if (pid == 0) { printf("error 1"); getchar(); }//error
    return 0;
}

Edit: I've changed the function to not use a std::string any more, and using lstrcmpi means it will work as either an Ansi or Unicode build. 编辑:我已经将函数更改为不再使用std::string ,并且使用lstrcmpi意味着它将作为Ansi或Unicode构建工作。 As the commentators below have suggested, you really should be using Unicode these days. 正如下面的评论员所建议的,这些天你真的应该使用Unicode。

Here is the c code, works fine on windows. 这是c代码,在windows上运行正常。 Calling PID_GetProcessId with process name, returns process id of process name 使用进程名称调用PID_GetProcessId,返回进程名称的进程ID

int PID_GetProcessId(char * name, PDWORD pid)
{
    DWORD aProcesses[1024], cbNeeded, cProcesses;
    UINT32 i;

    if (!EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
    {
        //Error 
    }

    /* Calculate how many process identifiers were returned. */
    cProcesses = cbNeeded / sizeof(DWORD);

   /* Print the name and process identifier for each process. */
    for ( i = 0; i < cProcesses; i++ )
    {
        if( aProcesses[i] != 0 )
        {
            if(PID_PrintProcessNameAndID( aProcesses[i], name ) == 1)
            {
               *pid = aProcesses[i];
               break;
            }
         }
    }
    return 0;
}

int PID_PrintProcessNameAndID(DWORD processID,char * name)
{

    char szProcessName[MAX_PATH];
    HANDLE hProcess=NULL;
    char buff[200]="";

    hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |PROCESS_VM_READ,FALSE, processID );
    /* Get the process name. */
    if (NULL != hProcess )
    {
        HMODULE hMod;
        DWORD cbNeeded;

        if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
        {
            GetModuleBaseNameA( hProcess, hMod, (LPSTR)szProcessName, sizeof(szProcessName)/sizeof(TCHAR) );
        }
    }
    if(lstrcmpiA(szProcessName, name) == 0) 
    {
        CloseHandle(hProcess);
        return 1;
    }

    return 0;

    CloseHandle( hProcess );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM