The QT library does not seem to have a complete cross-platform solution, and you may need to judge by your own platform.
Windows can use OpenProcess to obtain relevant information using the methods mentioned above. Termination is simple:
ShellExecute (0, "open", "TASKKILL", (Processname,null, sw_hide);
Other platforms need to find a way to locate the PID of the process (such as invoking the system's PS command for filtering lookups),
Linux opens/proc/pid/cmdline view process status.
Sun System opens/proc/pid/psinfo view process status.
---------------------------Win Platform------------------------------------------------
Attached code:
const char DESTPROC[19] = "cmd.exe";
DWORD pid = processtopid (Destproc);
return to 0 is no
DWORD processtopid(const char *inputprocessname)
{
DWORD aprocesses[1024x768], cbneeded, cprocesses;
unsigned int i;
HANDLE hprocess = NULL;
hmodule hmod = NULL;
Char szprocessname[max_path] = "unknownprocess";
Addprivilege(se_debug_name);
//Calculate how many processes are currently in place, aprocesses[] is used to store valid processes PIDs
if ( ! EnumProcesses( aprocesses, sizeof(aprocesses), & cbneeded ) )
{
return 0;
}
Cprocesses = cbneeded / sizeof(DWORD);
//To traverse all processes by a valid PID
for ( i = 0; i < cprocesses; i++ )
{
//Open a specific PID process
hprocess = openprocess( process_query_information |
Process_vm_read,
FALSE , aprocesses[i]);
//Get the process name of the specific PID
if ( hprocess )
{
if ( enumprocessmodules( hprocess, &hmod, sizeof( Hmod), &cbneeded ) )
{
GetModuleBaseName( hprocess, hmod,
szProcessName, sizeof(szprocessname) );
//The process name to be obtained is compared with the input process name, as the same will return the process PID
if (! stricmp(szprocessname, inputprocessname))
{
CloseHandle( hprocess );
return aprocesses[i];
}
}
}//end of If (hprocess)
}//end of For
//did not find the corresponding process name, returned 0
CloseHandle( hprocess );
return 0;
}
You can also enumerate the application names of all the processes and then judge the name of the application.
DWORD getprocessidfromname(lpctstr name)
{
PROCESSENTRY32 PE;
DWORD id=0;
HANDLE hsnapshot=createtoolhelp32snapshot(th32cs_snapprocess,0);
PE. dwsize=sizeof(PROCESSENTRY32);
if (! Process32First(hsnapshot,&pe))
return 0;
while (1)
{
PE. dwsize=sizeof(PROCESSENTRY32);
if (process32next(hsnapshot,&pe)==FALSE )
Break ;
if (strcmp(pe. Szexefile,name)==0)
{
ID=pe. Th32processid;
Break ;
}
}
CloseHandle(hsnapshot);
return ID;
}
QT Process Basics (i) how QT determines that another process is running