QT Process Basics (i) how QT determines that another process is running

Source: Internet
Author: User

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


  1. DWORD processtopid(const char *inputprocessname)

  2. {

  3. DWORD aprocesses[1024x768], cbneeded, cprocesses;

  4. unsigned int i;

  5. HANDLE hprocess = NULL;

  6. hmodule hmod = NULL;

  7. Char szprocessname[max_path] = "unknownprocess";


  8. Addprivilege(se_debug_name);


  9. //Calculate how many processes are currently in place, aprocesses[] is used to store valid processes PIDs

  10. if ( ! EnumProcesses( aprocesses, sizeof(aprocesses), & cbneeded ) )

  11. {

  12. return 0;

  13. }


  14. Cprocesses = cbneeded / sizeof(DWORD);

  15. //To traverse all processes by a valid PID

  16. for ( i = 0; i < cprocesses; i++ )

  17. {

  18. //Open a specific PID process

  19. hprocess = openprocess( process_query_information |

  20. Process_vm_read,

  21. FALSE , aprocesses[i]);

  22. //Get the process name of the specific PID

  23. if ( hprocess )

  24. {

  25. if ( enumprocessmodules( hprocess, &hmod, sizeof( Hmod), &cbneeded ) )

  26. {

  27. GetModuleBaseName( hprocess, hmod,

  28. szProcessName, sizeof(szprocessname) );

  29. //The process name to be obtained is compared with the input process name, as the same will return the process PID

  30. if (! stricmp(szprocessname, inputprocessname))

  31. {

  32. CloseHandle( hprocess );

  33. return aprocesses[i];

  34. }

  35. }

  36. }//end of If (hprocess)

  37. }//end of For

  38. //did not find the corresponding process name, returned 0

  39. CloseHandle( hprocess );

  40. return 0;

  41. }




You can also enumerate the application names of all the processes and then judge the name of the application.




  1. DWORD getprocessidfromname(lpctstr name)

  2. {

  3. PROCESSENTRY32 PE;

  4. DWORD id=0;

  5. HANDLE hsnapshot=createtoolhelp32snapshot(th32cs_snapprocess,0);

  6. PE. dwsize=sizeof(PROCESSENTRY32);

  7. if (! Process32First(hsnapshot,&pe))

  8. return 0;

  9. while (1)

  10. {

  11. PE. dwsize=sizeof(PROCESSENTRY32);

  12. if (process32next(hsnapshot,&pe)==FALSE )

  13. Break ;

  14. if (strcmp(pe. Szexefile,name)==0)

  15. {

  16. ID=pe. Th32processid;


  17. Break ;

  18. }



  19. }

  20. CloseHandle(hsnapshot);

  21. return ID;

  22. }


QT Process Basics (i) how QT determines that another process is running

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.