Discussions related to Visual Prolog
Harrison Pratt
VIP Member
Posts: 443
Joined: 5 Nov 2000 0:01

How to hide the console window

Unread post by Harrison Pratt »

How can I run a console application without showing the console window?
User avatar
Thomas Linder Puls
VIP Member
Posts: 1401
Joined: 28 Feb 2000 0:01

Re: How to hide the console window

Unread post by Thomas Linder Puls »

It seems you can do like this, but the console will however exist/flicker for a moment:

Code: Select all

implement main     open core   clauses     run() :-         R = freeConsole(),         if b_false = R then             E = exception::getLastError(),             exception::raise_nativeCallException("FreeConsole", E)         end if,         stdio::inputStream := inputStream_null::null,         stdio::outputStream := outputStream_null::null,         multiThread_native::sleep(2000),         nothing().   class predicates     freeConsole : () -> booleanInt Result language apicall.   end implement main   goal     mainExe::run(main::run).
The main things are don't use console:: (notice the goal) and call freeConsole.

You should also consider how to properly deal with exceptions (setting mainExe::exceptionDumper to something sensible), a program like this will bring-up our "last-resort-exception" dialog and that it not completely user friendly.

(Windows Services also run without window, but are started and stopped and should figure out what to do in-between).
Regards Thomas Linder Puls
PDC
Harrison Pratt
VIP Member
Posts: 443
Joined: 5 Nov 2000 0:01

Re: How to hide the console window

Unread post by Harrison Pratt »

The freeConsole works nicely although, as you said, it does flicker a console screen briefly, much like a console app.

What I was trying to do was to tidy up a tiny application that converts a clipboard string copied from medical journal titles to a valid Windows file name (i.e., remove ':' and similar characters) and put the cleaned string back on the clipboard. One day I got the notion that perhaps I could make the application (unncessarily) smaller by using a console app ... and went down a rabbit hole of exploration.

It looks like my original strategy works better for implementing a keyboard click-and-clean without screen flicker:
Create a GUI project and replace the TaskWindow creation in main.pro as shown in the code below.

Code: Select all

% DEFAULT CODE removed clauses     run() :-         TaskWindow = taskWindow::new(),         TaskWindow:show().

Code: Select all

    % NEW CODE         run() :-         if clipboard::putString(fileNameCleaner::cleanFName(clipboard::tryGetString())) then             _ = environment_native::beep(2100, 100)         else             _ = environment_native::beep(1600, 1000)         end if.
Also, I removed the TaskWindow and AboutDialog packages from the project. I probably could remove some of the unused PFC packages as well if I wanted to shrink the EXE size further.

Thomas, do you have any more thoughts on this?
User avatar
Thomas Linder Puls
VIP Member
Posts: 1401
Joined: 28 Feb 2000 0:01

Re: How to hide the console window

Unread post by Thomas Linder Puls »

I think that is a fine solution. (Except for the beeping).

(Out of curiosity) What will run the application?
Regards Thomas Linder Puls
PDC
Harrison Pratt
VIP Member
Posts: 443
Joined: 5 Nov 2000 0:01

Re: How to hide the console window

Unread post by Harrison Pratt »

I double-click the app's desktop icon to activate the link. I could also have created a keyboard shortcut, but chose not to do that.

It works like this:
  • I open the article (web page or PDF), highlight & copy the title text
  • click the icon (the beep lets me know it's converted the text),
  • then I save the file with "print to PDF" using the cleaned text that's on the clipboard for the file name.
User avatar
Thomas Linder Puls
VIP Member
Posts: 1401
Joined: 28 Feb 2000 0:01

Re: How to hide the console window

Unread post by Thomas Linder Puls »

OK, makes sense (as a personal tool).
Regards Thomas Linder Puls
PDC
Post Reply