简体   繁体   English

如何运行 Windows 批处理文件但隐藏命令窗口?

[英]How can I run a windows batch file but hide the command window?

How can I run a windows batch file but hiding the command window?如何运行 Windows 批处理文件但隐藏命令窗口? I dont want cmd.exe to be visible on screen when the file is being executed.我不希望 cmd.exe 在执行文件时在屏幕上可见。 Is this possible?这可能吗?

If you write an unmanaged program and use CreateProcess API then you should initialize lpStartupInfo parameter of the type STARTUPINFO so that wShowWindow field of the struct is SW_HIDE and not forget to use STARTF_USESHOWWINDOW flag in the dwFlags field of STARTUPINFO .如果您编写非托管程序并使用CreateProcess API,那么您应该初始化STARTUPINFO类型的lpStartupInfo参数,以便结构的wShowWindow字段是SW_HIDE并且不要忘记在STARTUPINFOdwFlags字段中使用STARTF_USESHOWWINDOW标志。 Another method is to use CREATE_NO_WINDOW flag of dwCreationFlags parameter.另一种方法是使用dwCreationFlags参数的CREATE_NO_WINDOW标志。 The same trick work also with ShellExecute and ShellExecuteEx functions.同样的技巧也适用于ShellExecuteShellExecuteEx函数。

If you write a managed application you should follows advices from http://blogs.msdn.com/b/jmstall/archive/2006/09/28/createnowindow.aspx : initialize ProcessStartInfo with CreateNoWindow = true and UseShellExecute = false and then use as a parameter of .如果您编写托管应用程序,则应遵循http://blogs.msdn.com/b/jmstall/archive/2006/09/28/createnowindow.aspx 的建议:使用CreateNoWindow = trueUseShellExecute = false初始化ProcessStartInfo ,然后使用作为 的参数。 Exactly like in case of you can set property WindowStyle of ProcessStartInfo to ProcessWindowStyle.Hidden instead or together with CreateNoWindow = true .就像您可以将ProcessStartInfo属性WindowStyle设置为ProcessWindowStyle.Hidden或与CreateNoWindow = true一起CreateNoWindow = true

You can use a VBS script which you start with wcsript.exe.可以使用以 wcsript.exe 开头的 VBS 脚本 Inside the script you can use CreateObject("WScript.Shell") and then Run with 0 as the second ( intWindowStyle ) parameter.在脚本中可以使用CreateObject("WScript.Shell")然后运行以0作为第二( intWindowStyle )参数。 See http://www.robvanderwoude.com/files/runnhide_vbs.txt as an example.http://www.robvanderwoude.com/files/runnhide_vbs.txt为例。 I can continue with Kix, PowerShell and so on.我可以继续使用 Kix、 PowerShell等。

If you don't want to write any program you can use any existing utility like CMDOW /RUN /HID "c:\\SomeDir\\MyBatch.cmd" , hstart /NOWINDOW /D=c:\\scripts "c:\\scripts\\mybatch.bat" , hstart /NOCONSOLE "batch_file_1.bat" which do exactly the same.如果您不想编写任何程序,您可以使用任何现有的实用程序,例如CMDOW /RUN /HID "c:\\SomeDir\\MyBatch.cmd" , hstart /NOWINDOW /D=c:\\scripts "c:\\scripts\\mybatch .bat" , hstart /NOCONSOLE "batch_file_1.bat"完全一样。 I am sure that you will find much more such kind of free utilities.我相信您会发现更多此类免费实用程序。

In some scenario (for example starting from UNC path ) it is important to set also a working directory to some local path ( %SystemRoot%\\system32 work always).在某些情况下(例如从 UNC 路径开始),将工作目录设置为某个本地路径也很重要( %SystemRoot%\\system32始终工作)。 This can be important for usage any from above listed variants of starting hidden batch.这对于使用上面列出的启动隐藏批处理的任何变体都很重要。

Using C# it's very easy to start a batch command without having a window open.使用 C#,无需打开窗口即可轻松启动批处理命令。 Have a look at the following code example:看看下面的代码示例:

        Process process = new Process();
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.FileName = "doSomeBatch.bat";
        process.Start();

For any executable file, you can run your program using cmd with "c" parameter:对于任何可执行文件,您都可以使用带有“c”参数的 cmd 运行您的程序:

cmd /c "your program address"\"YourFileName".bat  

(->if it's a batch file!) As a final solution, I suggest that you create a .cmd file and put this command in it: (->如果是批处理文件!)作为最终解决方案,我建议您创建一个 .cmd 文件并将此命令放入其中:

cmd /c "your program address"\"YourFileName".bat
exit

Now just run this .cmd file.现在只需运行这个 .cmd 文件。

To self-hide already running script you'll need getCmdPid.bat and windowoMode.bat要自我隐藏已经运行的脚本,您需要getCmdPid.batwindowoMode.bat

@echo off

echo self minimizing
call getCmdPid.bat
call windowMode.bat -pid %errorlevel% -mode hidden

echo --other commands--
pause

Here I've compiled all ways that I know to start a hidden process with batch without external tools.With a ready to use scripts (some of them rich on options) , and all of them form command line.Where is possible also the PID is returned .Used tools are IEXPRESS,SCHTASKS,WScript.Shell,Win32_Process and JScript.Net - but all of them wrapped in a .bat files. 在这里,我编译了所有我知道的无需外部工具即可使用批处理启动隐藏进程的方法。准备好使用脚本(其中一些具有丰富的选项),并且所有这些都形成命令行。PID 在哪里也可能返回。使用的工具是 IEXPRESS、SCHTASKS、WScript.Shell、Win32_Process 和 JScript.Net - 但它们都包含在.bat文件中。

Native C++ codified version of Oleg's answer -- this is copy/pasted from a project I work on under the Boost Software License. Oleg 答案的本机 C++ 编码版本——这是从在 Boost 软件许可证下工作的项目中复制/粘贴的。

BOOL noError;
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInformation;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
startupInfo.dwFlags = STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = SW_HIDE;
noError = CreateProcess(
    NULL,                                           //lpApplicationName
    //Okay the const_cast is bad -- this code was written a while ago.
    //should probably be &commandLine[0] instead. Oh, and commandLine is
    //a std::wstring
    const_cast<LPWSTR>(commandLine.c_str()),        //lpCommandLine
    NULL,                                           //lpProcessAttributes
    NULL,                                           //lpThreadAttributes
    FALSE,                                          //bInheritHandles
    CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT,  //dwCreationFlags
    //This is for passing in a custom environment block -- you can probably
    //just use NULL here.
    options.e ? environment : NULL,                 //lpEnvironment
    NULL,                                           //lpCurrentDirectory
    &startupInfo,                                   //lpStartupInfo
    &processInformation                             //lpProcessInformation
);

if(!noError)
{
    return GetLastError();
}

DWORD exitCode = 0;

if (options.w) //Wait
{
    WaitForSingleObject(processInformation.hProcess, INFINITE);
    if (GetExitCodeProcess(processInformation.hProcess, &exitCode) == 0)
    {
        exitCode = (DWORD)-1;
    }
}

CloseHandle( processInformation.hProcess );
CloseHandle( processInformation.hThread );

This little VBScript from technet does the trick:这个来自technet 的小 VB​​Script 可以解决这个问题:

Const HIDDEN_WINDOW = 12

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")

Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create("mybatch.bat", null, objConfig, intProcessID)

Edit mybatch.bat to your bat file name, save as a vbs, run it.mybatch.bat编辑为您的 bat 文件名,另存为 vbs,运行它。

Doc says it's not tested in Win7, but I just tested it, it works fine. Doc 说它没有在 Win7 中测试,但我只是测试了它,它工作正常。 Won't show any window for whatever process you run不会为您运行的任何进程显示任何窗口

You could write a windows service that does nothing but execute your batch file.您可以编写一个只执行批处理文件的 Windows 服务。 Since services run in their own desktop session, the command window won't be visible by the user.由于服务在它们自己的桌面会话中运行,因此用户将看不到命令​​窗口。

Create a shortcut to your bat file by using the right-click and selecting Create shortcut .通过使用right-click并选择“ Create shortcutCreate shortcut bat文件的Create shortcut Right-click on the shortcut you created and click on properties. Right-click您创建的快捷方式,然后单击属性。 Click on the Run drop-down box and select Minimized.单击运行下拉框并选择最小化。

Use Bat To Exe Converter and compile the Bat file as an executable.使用Bat To Exe Converter并将 Bat 文件编译为可执行文件。

Steps:步骤:

  1. Open Bat to Exe Converter打开 Bat to Exe 转换器
  2. Select your Bat file选择您的 Bat 文件
  3. In the options select "Invisible Application"在选项中选择“隐形应用程序”
  4. Finish by pressing the compile button按编译按钮完成

1、下载bat转exe转换器并安装 2、运行bat转exe应用程序 3、下载.pco图片如果你想制作好看的exe 4、指定bat文件位置(c:\\my.bat) 5、指定保存 exe 的位置(例如:c:/my.exe) 6、选择版本信息选项卡 7、选择图标文件(下载的 .pco 图像) 8、如果要填写版本、公司名称等信息 9 ,将选项卡更改为选项 10,选择不可见的应用程序(这将在运行应用程序时隐藏命令提示符) 11,选择 32 位(如果您选择 64 位 exe 将仅适用于 32 位操作系统) 12,编译 13,复制exe到bat文件正确执行的位置 14、运行exe

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

相关问题 Windows XP 或更高版本 Windows:如何在不显示窗口的情况下在后台运行批处理文件? - Windows XP or later Windows: How can I run a batch file in the background with no window displayed? 我可以运行带有隐藏cmd窗口的Windows批处理文件但是看到批处理启动的程序吗? - Can I run a windows batch file with hidden cmd window but see the programs started by the batch? 从命令行运行 m 文件时如何隐藏“MATLAB 命令行窗口”? - How do I hide “MATLAB Command Window” when I run an m-file from command line? 可以在命令提示符下运行Windows批处理文件吗? - Can you run Windows batch file not in a command prompt? Windows:如何在新命令窗口中以某种方式在进程完成或崩溃时关闭窗口? - Windows: how can I run something in a new command window in such a way that the window closes when the process finishes or crashes? 如果命令位于外部文件中,如何在Windows上运行npm脚本 - How can I run npm script on Windows if command is in an external file Windows批处理:在由一个称为“批处理”的cmd窗口中运行命令? - Windows Batch: Run command in cmd window opened by a called batch? 批量文件在循环中运行Windows命令 - Batch file to run windows command in loop Windows批处理读取文件并运行命令 - windows batch read file and run command 如何静默运行批处理文件? - How can I run a batch file silently?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM