简体   繁体   English

CreateProcess调用cmd.exe,包括 没有显示(闪烁)窗口的参数?

[英]CreateProcess calling cmd.exe incl. arguments with no showing (flashing) window?

The saga continues... 传奇继续...

I've searched the web, i've searched on StackOverflow, i found many hope giving answers/solutions, but somehow they have all failed (up)on me (including the ones related to ShellExecute(Ex) ). 我已经在网上搜索过,已经在StackOverflow上搜索过,我发现了很多希望给出答案/解决方案的方法,但是不知何故,它们对我来说都失败了(包括与ShellExecute(Ex)相关的问题)。

How to hide a (flashing) CMD window (incl. arguments) using CreateProcess?? 如何使用CreateProcess隐藏(闪烁的)CMD窗口(包括参数)?

I basically want to call/execute a set of conditional/native cmd.exe commands (ie FOR /F , and || ), but also an external command FIND(STR).exe . 我基本上想调用/执行一组条件/本机cmd.exe命令(即FOR /F|| ),还要调用一个外部命令FIND(STR).exe And this, without showing a (flashing) CMD window. 而且,这没有显示(闪烁的)CMD窗口。

But even hiding something as simple as "cmd.exe /C ECHO ...flashing window is bad..." seems impossible to do. 但是,即使隐藏诸如"cmd.exe /C ECHO ...flashing window is bad..."似乎也无法实现。

The code i've tried (including many variations related to the dwFlags and wShowWindow flags 我尝试过的代码(包括与dwFlagswShowWindow标志有关的许多变体

#include <windows.h>

int main()


{

    char cmdline[] = "cmd.exe /c ECHO ...flashing window is bad...";

    PROCESS_INFORMATION pi;
    STARTUPINFO si;

//  memset(&si,0,sizeof(STARTUPINFO));
    ZeroMemory(&si, sizeof(STARTUPINFO));
    si.cb = sizeof(STARTUPINFO);

//  si.dwFlags = STARTF_USESTDHANDLES;
//  si.dwFlags = CREATE_NO_WINDOW;
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;
//  si.wShowWindow = CREATE_NO_WINDOW;

    CreateProcess(NULL, (LPSTR) cmdline, NULL, NULL, 0, 0, NULL, NULL, &si, &pi);

    WaitForSingleObject(pi.hProcess, INFINITE);

    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);

//  ExitProcess;

    return 0;

}

I don't want to rely on external programs ie .vbs (Windows Scripting Host) or shortcut tricks, but simply a standalone compiled .exe. 我不想依赖外部程序,例如.vbs(Windows脚本宿主)或快捷方式,而只是依赖一个独立的已编译.exe。

Is this (really) too much to ask, or am i doing it (completely) wrong? 这是(真的)要问的太多吗,还是我(完全)做错了?

Thanks... 谢谢...

Update: You also seem to confuse CreateProcess flags (its dwCreationFlags argument) with the member of STARTUPINFO structure. 更新:您似乎还将CreateProcess标志(其dwCreationFlags参数)与STARTUPINFO结构的成员混淆了。 These are different flags, CREATE_NO_WINDOW should not be in STARTUPINFO . 这些是不同的标志, CREATE_NO_WINDOW不应在STARTUPINFO

You have to pass the CREATE_NO_WINDOW flag, then the console window won't show. 您必须传递CREATE_NO_WINDOW标志,然后控制台窗口将不会显示。 Originally I've answered that you have to redirect the standard handles which is not correct (but still highly recommanded). 最初我已经回答过,您必须重定向不正确的标准句柄(但仍然强烈建议)。

Set STARTF_USESTDHANDLES and fill in appropriate handles. 设置STARTF_USESTDHANDLES并填写适当的句柄。 If you are interested in the output of the process, create pipes, otherwise you can just open nul an pass that. 如果您对过程的输出感兴趣,请创建管道,否则可以通过nul打开该通道。

Try Using ProcessBuilder. 尝试使用ProcessBuilder。 Here is an example of some code that I have that seems to work just fine. 这是一些我看似可以正常工作的代码示例。 In my code below, the shellScript is a StringBuilder that I am dynamically creating that contains the command and it's parameters that I want to execute. 在下面的代码中,shellScript是我正在动态创建的StringBuilder,其中包含命令及其要执行的参数。

String[] scriptArray = shellScript.toString().split(" ");

ProcessBuilder builder = new ProcessBuilder(scriptArray);
File outputFile = new File("/logs/AgentOutputLog.txt");
File errorFile = new File("/logs/AgentErrorLog.txt");
builder.redirectOutput(outputFile);
builder.redirectError(errorFile);
Process process = builder.start();
int errCode = process.waitFor();

//errCode = 0 means online
if(errCode == 0){
    success = true;
    break;
//errCode = 1 means offline
} else if (errCode == 1){
    success = false;
    break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM