Running a task list on Windows with popen in C without the cmd.exe popup

Welcome everyone and thank you for your time.

I am developing some kind of monitoring application in C, and I needed to get a list of current tasks. So I use tasklist and get the result thanks popen ();

ptr = popen("tasklist /V", "r"); while(1) { if(fgets(temp, 255, ptr) == NULL) break; fputs(temp, log); } 

The problem is that for some fractions of a second, the cmd.exe pop-up window appears and it really bothers because it switches focus to this new window, and this forces my application to switch to window mode instead of full-screen.

So, I spent days looking at both the pop-up path and Windows itself to start this process in a "hidden" mode / window, but did not get any result. Things I've already tried include:

 cmd.exe /c tasklist /V start /b cmd.exe /c tasklist /V start /min /b cmd.exe /c tasklist /V start /min cmd.exe /c tasklist /V tasklist > somefile 

I also tried the latter to read the result from some file, but it looks like the tasklist command prints to stdout, since no data is written, although the file is created.

I hope for your answer and thank you anyway.

+4
source share
2 answers

You can achieve this by calling CreateProcess , passing SW_HIDE as the field wShowWindow STARTUPINFO struct and including CREATE_NO_WINDOW in dwCreationFlags .

This method is a bit fragile because you can find your application on a machine with a tasklist version that has a different output format.

If you need a list of all running processes, you can call EnumProcesses .

+3
source

The task can be easily achieved with EnumProcesses .

An intuitive example is given here .

+1
source

Source: https://habr.com/ru/post/1337016/


All Articles