mardi 24 mars 2015

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




Aucun commentaire:

Enregistrer un commentaire