大家好,感谢抽出时间。
我正在用 C 语言开发某种监控应用程序,我需要获取当前的任务列表。所以我正在使用任务列表并通过 popen(); 获取输出
ptr = popen("tasklist /V", "r");
while(1)
{
if(fgets(temp, 255, ptr) == NULL) break;
fputs(temp, log);
}
问题是,在几分之一秒内会弹出一个 cmd.exe 窗口,这确实令人不安,因为它将焦点切换到新窗口上,并使我的应用程序进入窗口模式而不是全屏模式。
因此,我花了几天时间寻找流行方式或 Windows 本身的方式,以在“隐藏”模式/窗口中启动该过程,但没有结果。
我已经尝试过的事情包括:
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
我也尝试了最后一个,所以我会从该 somefile 读取输出,但似乎任务列表强制输出到标准输出,因为虽然创建了文件,但没有写入数据。
希望您的答复,无论如何谢谢您。
Hello everybody and thanks for your time.
I'm developing some kind of monitoring application in C and I fell in need of getting the current tasks list. So I'm using tasklist and getting the output thanks to 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 a cmd.exe window pops up and that's really disturbing, because it switches focus on that new window and it makes my application go to windowed-mode instead of fullscreen.
So, I've spent days looking on either popen ways or Windows itself ones to start that process in an 'hidden' mode/window but got no result.
Things I 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 tried last one too so I would read the output from that somefile but seems like tasklist forces output to stdout since no data is written though file is created.
Hope in your answer and thank you anyway.
发布评论
评论(2)
您可以通过调用
CreateProcess
将SW_HIDE
作为 wShowWindow 字段传递28VS.85%29.aspx" rel="nofollow">STARTUPINFO
结构并在dwCreationFlags
中包含CREATE_NO_WINDOW
。此方法有点脆弱,因为您可能会发现您的应用程序运行在具有不同输出格式的
tasklist
版本的计算机上。如果您想要所有正在运行的进程的列表,可以调用
枚举进程
。You can achieve this by calling
CreateProcess
passingSW_HIDE
as thewShowWindow
field of theSTARTUPINFO
struct and includingCREATE_NO_WINDOW
indwCreationFlags
.This method is a little brittle because you may find your app running on a machine with a version of
tasklist
that has a different output format.If you want a list of all processes that are running you can call
EnumProcesses
.使用 EnumProcesses 可以轻松完成任务。
此处给出了直观的示例。
Task can be easily achieved using EnumProcesses.
Intuitive example given here.