6

Possible Duplicate:
Profiler and Memory Analysis Tools for Delphi
How do I hide the console window?

I'm reposting this to make it more clear. So, here is my console application:

enter image description here

That opens a socket to 127.0.0.1:81, when the console application is visible it works fine, now how do I keep it working fine as a console but make the console invisible?

I am using Delphi 2007 (7).

Thanks.

Community
  • 1
  • 1
user1770015
  • 185
  • 2
  • 3
  • 9
  • 6
    User, you already asked how to hide the console window, and you got your answer. If your program can't connect to the socket, then *that's* the question you need to ask about. The presence of a console window should have no effect on sockets. Please post a new question describing the *actual* problem you have. Consider including some code that demonstrates the problem. – Rob Kennedy Oct 24 '12 at 21:04
  • 2
    I don't think the screenshot will help answer this question... We all know how a console window looks like! What you need to do, instead, is to post the code of a minimal project displaying the issue you are having. – Andreas Rejbrand Oct 24 '12 at 22:22
  • 1
    How do you figure, @Bensiu? I see no similarities between hiding console windows and profiling. – Rob Kennedy Oct 25 '12 at 03:24

1 Answers1

9

You can use ShowWindow and the GetConsoleWindow WinAPi functions.

Try this sample

{$APPTYPE CONSOLE}

uses
  Windows,
  SysUtils;

function GetConsoleWindow: HWND; stdcall; external kernel32;


begin
  try
    Writeln('Press enter to hide console the window');
    Readln;
    //hide the console window
    ShowWindow(GetConsoleWindow, SW_HIDE);

    //do something
    Sleep(5000);

    Writeln('Press enter to exit');
    //show the console window
    ShowWindow(GetConsoleWindow, SW_SHOW);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • 1
    Far better is just not to show the console window in the first place. – David Heffernan Oct 24 '12 at 20:49
  • @DavidHeffernan what do you mean? call `ShowWindow(GetConsoleWindow, SW_HIDE);` directly? – RRUZ Oct 24 '12 at 20:51
  • 3
    I mean that if you don't want a console, don't make the app a console app. – David Heffernan Oct 24 '12 at 20:54
  • Maybe the OP is using a Console APP to get an smaller exe. – RRUZ Oct 24 '12 at 20:56
  • @DavidHeffernan What? are you compared the size of a plain vanilla console app against a vcl form app? – RRUZ Oct 24 '12 at 21:01
  • The program will be the same size no matter which subsystem it's configured to use. Programs configured with the console subsystem *always* start with a console. Programs configured with the GUI subsystem don't necessarily have GUIs; it's their responsibility to provide GUIs for themselves. You can start with the "console app" project template, and then remove the `$apptype console` directive, just as the user was told in the previous, duplicate question. – Rob Kennedy Oct 24 '12 at 21:01
  • 7
    No. If you don't want to see the console window, you don't want VCL forms either. Start from the default console app and remove the $APPTYPE CONSOLE. Now you have an app that targets the GUI subsystem. It is $APPTYPE GUI. But it's not any bigger than the console app. – David Heffernan Oct 24 '12 at 21:03
  • @DavidHeffernan, Now I understand your point :) – RRUZ Oct 24 '12 at 21:06
  • 4
    Why did you and @David delete [the other question](http://stackoverflow.com/q/13042036/33732)? It was direct and to the point. It wasn't muddled by confusion with sockets, and it had a good answer. – Rob Kennedy Oct 24 '12 at 21:49
  • 1
    Fascinating. If you do a `Readln` while the console window is hidden, the input is actually recorded if only the console window still has 'focus' in some sense... This probably one shouldn't rely on... – Andreas Rejbrand Oct 24 '12 at 22:44
  • @Rob that was a mistake, now remedied – David Heffernan Oct 25 '12 at 06:22
  • 4
    I removed '{$APPTYPE CONSOLE}' in Delphi XE7, and it didn't work. Then I replaced it with '{$APPTYPE GUI}', and it worked. The console then stopped working completely of course. I needed the console for reporting possible errors, and reenabled it on demand with 'if GetConsoleWindow = 0 then AllocConsole;' – Bent Tranberg Dec 09 '14 at 20:14