0

I have built a networking app for MacOS for private usage. It uses shell commands and runs bash files. Most of my colleagues are using MacOS, but still need to publish the same app for Windows.

Is there any ways to hide a cmd window when I runs shell script?

Thanks in advance.

2 Answers2

1

On Windows app, you can do that changing the folowing lines in windows/runner/resources/main.cpp

if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
   CreateAndAttachConsole();
}

to this:

if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
   CreateAndAttachConsole();
}else {
   STARTUPINFO si = { 0 };
   si.cb = sizeof(si);
   si.dwFlags = STARTF_USESHOWWINDOW;
   si.wShowWindow = SW_HIDE;

   PROCESS_INFORMATION pi = { 0 };
   WCHAR lpszCmd[MAX_PATH] = L"cmd.exe";
   if (::CreateProcess(NULL, lpszCmd, NULL, NULL, FALSE, CREATE_NEW_CONSOLE | CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
  do {
    if (::AttachConsole(pi.dwProcessId)) {
      ::TerminateProcess(pi.hProcess, 0);
      break;
    }
  } while (ERROR_INVALID_HANDLE == GetLastError());
  ::CloseHandle(pi.hProcess);
  ::CloseHandle(pi.hThread);
 }
}

More information here: https://github.com/flutter/flutter/issues/47891

Rafael Bartz
  • 308
  • 2
  • 10
0

There are some options to run a cmd hidden on Windows for example:

  1. start a cmd window as hidden by using the "hide" option on a bat script or programmatically using the Windows API ShowWindow(SW_HIDE). Hidden in this case means you won't see the GUI and it has no presence on the task-bar.
  2. run it through "Task Scheduler"
  3. run it as background service

Some examples:

IODEV
  • 1,706
  • 2
  • 17
  • 20