Hide Console Window in Dart on windows - dart

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.

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

Related

VS Code - Running dart console application ends in error and opens errors_patch.dart, how to stop this behavior?

I decided to rewrite some of my js stuff with dart in VS Code.
When I launch the dart console app in debug mode, and there is a bug somewhere, an errors_patch.dart file opens up showing line 27. How can I stop this file from opening?
#patch
#pragma("vm:external-name", "Error_throwWithStackTrace")
external static Never _throw(Object error, StackTrace stackTrace);
}
Try the following.
Open [Debug and Run] View
Expand [BREAKPOINTS]
Uncheck [Uncaught Exceptions]

How to show/hide a console window app?

I have a small console app. I want to hide its window when it is called from my main program (with -hide as command line parameter) and show it when the user starts it (no command line param).
This question suggests that using {$APPTYPE GUI} instead of {$APPTYPE CONSOLE} will hide the window. And indeed it works. But how do I make the window visible when ran by user?
Purpose: I want my main program to interact with the console app silently in the background (console is invisible). So, when the user starts the console app alone, I just want to give him a warning: 'This console app is doing x task. You cannot start it manually'.
Leave the program alone, as a console application. Do not make it into a GUI application because that means that when users start it directly, it will not be given a console.
When you start the program from your main app, use CreateProcess to do so, passing the CREATE_NO_WINDOW flag. That flag ensures that no console window will be created.

Dart: debugging dart app doesn't work from within WebStorm, running works fine

I am using webstorm. And I have setup a small sample app. Running the app displays the following in the output window
C:/dart-sdk/bin/dart.exe --ignore-unrecognized-flags C:/Users/testuser/WebstormProjects/TestDart/TestMe.dart
You are using Windows
Process finished with exit code 0
but when debugging it just sits there with the following output
C:/dart-sdk/bin/dart.exe --ignore-unrecognized-flags --debug:57939 C:/Users/testuser/WebstormProjects/TestDart/TestMe.dart
I have a breakpoint set in the main() funciton on this line, but it never hits it
stdout.write("You are using ");
I have no firewall and also I don't think i need dartium installed as this is just a standard console application that prints things out.
Anyone have any success?
thanks
Debugging Dart console application doesn't currently work:( Please watch WEB-9937 to be notified on any progress

Run OpenCV application without console window

How can i start a OpenCV application without the console window in background? I have a app that uses the webcam and i would like to see only the webcam window.
Thank you.
You get a console window because the program you created is a console program, this has nothing to do with OpenCV. You need to change the type of your program from console to standard (windowed) Windows.
This SO post lists the different solutions to do this, but the easiest is to change your main() function name to WinMain().
You should use this
int main(){
FreeConsole();
}
it work fine, checked :)

Program both as Console and GUI [duplicate]

This question already has answers here:
Can one executable be both a console and GUI application?
(9 answers)
Closed 6 years ago.
Is it possible to (and if so, how do I) make a single program work both as a console application and a GUI version using Delphi 2007?
What I am after is that if the program is run with the appropriate command-line options, it should function as a console program, printing output to the console using WRITELN, but if no command line arguments are given it should run as a normal Delphi GUI application?
The catch is that when running as a console application, the command line interpreter waits for the application to terminate before allowing you to enter a new command, whereas a GUI application started from the command line immediately returns you to the command line and the GUI application is started in a detached process. I want this behaviour retained.
I don't mind something like this:
IF GUI THEN StartApplicationAsGUI(ParamStr(0))
ie. I don't mind that I'll have to restart the application using some form of EXECUTE call to start it in GUI mode if needed, as long as the command line interface returns to the command line input when the GUI version is started.
I'd prefer a solution/suggestion that is along the lines of:
<Parse Comnand Line>
IF ConsoleMode THEN
RunConsole(Parameters)
ELSE BEGIN
Application.Initialize;
Application.CreateForm(...)
Application.Run;
END
(or vice-versa, ie. doing things a special way if GUI mode)
so that I can still use Delphi's IDE and VCL when making the GUI interface...
On Windows this is a little bit tricky. Actually the distinction between a console application and a GUI one is a single flag in the PE header. You can easily write console applications that create windows but that way you always have the console window around (you could hide it, though, but that wouldn't be nice when people run your program from cmd).
You can, however write a GUI application that creates a console if it needs to, using the AllocConsole function:
A process can be associated with only one console, so the AllocConsole function fails if the calling process already has a console. A process can use the FreeConsole function to detach itself from its current console, then it can call AllocConsole to create a new console or AttachConsole to attach to another console.
If the calling process creates a child process, the child inherits the new console.
AllocConsole initializes standard input, standard output, and standard error handles for the new console. The standard input handle is a handle to the console's input buffer, and the standard output and standard error handles are handles to the console's screen buffer. To retrieve these handles, use the GetStdHandle function.
This function is primarily used by graphical user interface (GUI) application to create a console window. GUI applications are initialized without a console. Console applications are initialized with a console, unless they are created as detached processes (by calling the CreateProcess function with the DETACHED_PROCESS flag).
However, when run from cmd this will likely cause another console window to appear instead of re-using the existing one. I don't know whether a good solution exists there.
IMO, the best approach here is to have non-visual classes that actually do the work of the program. Then you can call that from a GUI program, and you can also call it from a separate command line program. Both programs are just wrappers around the functionality of your class(es).
This forces the design to be clean too - your classes necessarily are separated from the GUI layer of your application.
http://blogs.msdn.com/oldnewthing/archive/2009/01/01/9259142.aspx
Windows has different values in executable's header for console and UI application (see more details here). So it seems to be impossible to make the same executable to work in both modes.
As an alternative, you can open a console in you UI app, but it will be new console, not the one you've started app from.
AttachConsole() may be used to get a hold of the parents console.
E.g. if the application is started from a cmdline shell, AllocConsole() can be avoided:
if not AttachConsole(ATTACH_PARENT_PROCESS)
then AllocConsole;
More info here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681952(v=vs.85).aspx

Resources