0

I want to build an application with Dart in Windows. How to hide the console window when application will be start? Like a background service.

Thanks.

MrRookoo
  • 3
  • 1
  • Q: Will your app have a UI? Q: Have you considered [Flutter Desktop](https://flutter.dev/multi-platform/desktop)? – paulsm4 Sep 15 '22 at 18:01
  • No. it's just like a console application but I want to hide The CMD. In C I am using gcc -mwindows And it will be right. – MrRookoo Sep 15 '22 at 18:03
  • Q: Will your app have a UI (either a graphical UI, or command-line interaction)? Q: Have you considered [Flutter Desktop](https://flutter.dev/multi-platform/desktop)? Q: Exactly how are you creating your project and running the program now? – paulsm4 Sep 15 '22 at 18:19
  • 1
    On Windows, an executable can be either a windowed application or a console application. Console-mode applications always show a console window. However, you can use a tool such as `editbin.exe` (included with the Windows SDK) to change the type to a windowed application, which won't show a console window. See https://stackoverflow.com/a/2424210/ – jamesdlin Sep 15 '22 at 18:46
  • Yes `editbin.exe` is my solution. Thanks. – MrRookoo Sep 15 '22 at 19:39

1 Answers1

3

It is kinda a hack where the console window will flash really quickly since the program will spawn the console window but the first line will then hide it again:

import 'package:win32/win32.dart';

void main() {
  ShowWindow(GetConsoleWindow(), SW_HIDE);
  // Your program...
}

But the rest of your program will then run in the background.

(Inspired by: https://stackoverflow.com/a/9618984/1953515)

Another ("hacky") solution could be to do what is suggested in the following where we do a change on the generated exe file from dart compile exe with editbin.exe: https://stackoverflow.com/a/2435907/1953515

julemand101
  • 28,470
  • 5
  • 52
  • 48
  • 1
    Your first suggestion is definitely as good option (+1). I'd discourage "editbin". MAIN POINT: I'd really like to get more details from the OP: 1) is this app truly "headless" (like a "service"), or is there *some* UI (even text-mode interaction with the user), 2) How far has the OP gotten with "Dart" (just a source file? Has he created a project to automate build/run? Etc. Etc.)? 3) Is "Dart only" a requirement, or can the OP leverage Flutter (e.g. Flutter Desktop)? – paulsm4 Sep 15 '22 at 19:24
  • Yes this is like a service. Without UI – MrRookoo Sep 15 '22 at 19:40
  • @MrRookoo - I guess one partial answer to three specific questions is the best I'm going to get today :) ANYWAY - I'm glad you have what looks like an acceptable solution :) – paulsm4 Sep 16 '22 at 00:12