Flutter桌面端开发使用process_run插件问题

2022-04-18  本文已影响0人  Eania

Flutter桌面端开发使用process_run插件在Windows平台下的Release版本中执行命令会弹出CMD窗口,
问题解决:
将以下代码添加到windows/runner/main.cppwWinMain方法中

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);
      }
  }
上一篇 下一篇

猜你喜欢

热点阅读