Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does QT know that another process is running?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how does QT judge another process is running". In daily operation, I believe many people have doubts about how to judge another process in QT. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt that "how does QT judge another process is running?" Next, please follow the editor to study!

The Qt library does not seem to have a complete cross-platform solution, and you may need to judge by platform yourself.

Windows can use the methods mentioned above to use openprocess to get the relevant information. The termination is simple:

ShellExecute (0, "open", "TASKKILL", (processname,NULL, SW_HIDE)

Other platforms need to find ways to find the pid of the process (for example, call the system's ps command to filter)

Linux opens / proc/pid/cmdline to view the process status.

The sun system opens / proc/pid/psinfo to view the process status.

-WIN platform-

Attached code:

Const char DESTPROC [19] = "cmd.exe"

DWORD pid = ProcessToPID (DESTPROC)

If it is returned to 0, it means no.

DWORD ProcessToPID (const char * InputProcessName)

{

DWORD aProcesses [1024], 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 available. AProcesses [] is used to store valid process PIDs.

If (! EnumProcesses (aProcesses, sizeof (aProcesses), & cbNeeded))

{

Return 0

}

CProcesses = cbNeeded / sizeof (DWORD)

/ / iterate through all processes by valid PID

For (I = 0; I < cProcesses; iTunes +)

{

/ / Open the process of a specific PID

HProcess = OpenProcess (PROCESS_QUERY_INFORMATION |

PROCESS_VM_READ

FALSE, aProcesses [I])

/ / get the process name of a specific PID

If (hProcess)

{

If (EnumProcessModules (hProcess, & hMod, sizeof (hMod), & cbNeeded))

{

GetModuleBaseName (hProcess, hMod

SzProcessName, sizeof (szProcessName))

/ / compare the obtained process name with the entered process name, and return process PID if the same

If (! stricmp (szProcessName, InputProcessName))

{

CloseHandle (hProcess)

Return aProcesses [i]

}

}

} / / end of if (hProcess)

} / / end of for

/ / the corresponding process name was not found. 0 is returned.

CloseHandle (hProcess)

Return 0

}

You can also enumerate the application names of all processes and compare them with known application names.

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.th42ProcessID

Break

}

}

CloseHandle (hSnapshot)

Return id

}

At this point, the study on "how QT determines that another process is running" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report