简体   繁体   中英

Check if process is running - Windows

i'm using QT to check if process is running and i used the same code in msdn site.

It worked fine on Visual Studio but i'm having a problem making it work on QT.

Here's the code :

bool matchProcessName( DWORD processID, std::string processName)
{
    TCHAR szProcessName[MAX_PATH] = TEXT(L"notepad.exe");

// Get a handle to the process.

HANDLE 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) )
    {
        GetModuleBaseName( hProcess, hMod, szProcessName,
                           sizeof(szProcessName)/sizeof(TCHAR) );
    }
}

// Compare process name with your string
bool matchFound = !_tcscmp(szProcessName, processName.c_str() );

// Release the handle to the process.
CloseHandle( hProcess );

return matchFound;
}

The Error i'm getting is this :

error: cannot convert 'TCHAR*' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'

How can i make this work on QT ?

Thanks alot.

Update

I also tried this code :

DWORD FindProcessId(char* processName)
{

char* p = strrchr(processName, '\\');
if(p)
    processName = p+1;

PROCESSENTRY32 processInfo;
processInfo.dwSize = sizeof(processInfo);

HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,     NULL);
if ( processesSnapshot == INVALID_HANDLE_VALUE )
    return 0;

Process32First(processesSnapshot, &processInfo);
if ( !strcmp(processName, processInfo.szExeFile) )
{
    CloseHandle(processesSnapshot);
    return processInfo.th32ProcessID;
}

while ( Process32Next(processesSnapshot, &processInfo) )
{
    if ( !strcmp(processName, processInfo.szExeFile) )
    {
      CloseHandle(processesSnapshot);
      return processInfo.th32ProcessID;
    }
}

CloseHandle(processesSnapshot);
return 0;
}

I'm also getting an error :cannot convert 'WCHAR*' to 'const char*' for argument '2' to 'int strcmp(const char*, const char*)'

I prefer if i can make 2nd method work !

Thanks again

It has nothing to do with Qt.

In the updated code, PROCESSENTRY32.szExeFile is a TCHAR[] ,

ie it is a WCHAR[] if the macro _UNCODE is defined else is a char[] .

So you have to transfer your char *processName to TCHAR[] and use _tcscmp(...) to compare TCHAR[] . Modified code:

#include <Windows.h>
#include <TlHelp32.h>
#include <tchar.h>
#include <Psapi.h>
#include <cstring>
#include <string>

#define MIN(x, y) ((x) > (y)) ? (y) : (x)

void cstringToTCHAR(TCHAR *dst, const char *src, size_t l) {
#if defined(_UNICODE) || defined(UNICODE
    mbstowcs(dst, src, l);
#else
    memcpy(dst, src, l);
#endif
}

bool matchProcessName( DWORD processID, std::string processName)
{
    TCHAR szProcessName[MAX_PATH] = _T("notepad.exe");

    // Get a handle to the process.

    HANDLE 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) )
        {
            GetModuleBaseName( hProcess, hMod, szProcessName,
                sizeof(szProcessName)/sizeof(TCHAR) );
        }
    }

    // Compare process name with your string
    TCHAR systemEncodeProcessName[30];
    size_t processNameLen = MIN((processName.size() + 1), 30);
    cstringToTCHAR(systemEncodeProcessName, processName.c_str(), processNameLen);

    bool matchFound = !_tcscmp(szProcessName, systemEncodeProcessName);

    // Release the handle to the process.
    CloseHandle( hProcess );

    return matchFound;
}

DWORD FindProcessId(char* processName) {

    char* p = strrchr(processName, '\\');

    if(p) {
        processName = p+1;
    }

    PROCESSENTRY32 processInfo;
    processInfo.dwSize = sizeof(processInfo);

    HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

    if (processesSnapshot == INVALID_HANDLE_VALUE) {
        return 0;
    }

    //Transfer char array to TCHAR array.
    TCHAR systemEncodeProcessName[30];//Maybe need more or dynamic allocation.
    size_t processNameLen = MIN((strlen(processName) + 1), 30);
    cstringToTCHAR(systemEncodeProcessName, processName, processNameLen);

    Process32First(processesSnapshot, &processInfo);

    if (!_tcscmp(systemEncodeProcessName, processInfo.szExeFile)) {
        CloseHandle(processesSnapshot);
        return processInfo.th32ProcessID;
    }

    while ( Process32Next(processesSnapshot, &processInfo) ) {
        if ( !_tcscmp(systemEncodeProcessName, processInfo.szExeFile) ) {
            CloseHandle(processesSnapshot);
            return processInfo.th32ProcessID;
        }
    }

    CloseHandle(processesSnapshot);
    return 0;
}

Update

In Qt creator(3.3.0), the _UNICODE macro looks like missing in Windows platform, just add

DEFINES += _UNICODE

in your .pro file and then run qmake && build.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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