how to hide console or terminal

Pages: 12
hi there ,
I want to hide a terminal appliction or console application
without using WinAPI programming or <windows.h> or something like "ShowWindow(hwnd_win,SW_HIDE);"

thanks,
Not sure I understand. There's either a console or there's not. How would a console program run without a console?

EDIT: or are you saying you don't want a console application? If that's the case you need to get into platform specific things like WinAPI or a 3rd party lib.
Last edited on
closed account (Dy7SLyTq)
no hes saying he wants to make it run as a background process that isnt stealing users attention. like a running cron daemon
Unless I'm mistaken, background processes don't use the console... but rather, run in the background.
I assume OP is using an IDE that generates an executable that opens a console. (That's what happened to me on Windows anyways, using code::blocks).

To remove this in C::B simply click your project->properties->build targets->type and edit it to GUI application or whatever you want.

Other than that I can't help you I guess.
@Bourgond Aries:

That's kind of what I assumed as well... but then he doesn't have a console application anymore, and has to have a different entry point (on Windows, anyway), which requires him to use <windows.h>, which he said he didn't want to do.
@Disch right, but I believe that is impossible (altho I may be wrong).

My assertion comes from looking at SFML, its windows component has a library object called "main", which simply wraps around int WINAPI WinMain(...), and calls int main();

I don't know if my declaration of WinMain is correct, I don't use windows.
mokhi64 wrote:
I want to hide a terminal appliction or console application
without using WinAPI programming or <windows.h>
The problem with this is that it has to be platform dependent. Each system would have proprietary ways to deal with the console window. So if you want to manipulate it in a non-standard way on windows, you must deal with the Windows API.
Last edited on
kefin wrote:
@Disch right, but I believe that is impossible (altho I may be wrong).


I agree.

The console is just a console. The console by itself is not an additional window. Any window that it has is being maintained by the windowing system which is not part of your program.

So to want a console program without the console doesn't really make any sense.

I think OP just wants to run something in the background with no on-screen I/O... in which case your first reply was correct... he needs to do 3 things (assuming he's on Windows):

1) He needs to change his program to be a make it a Windows program rather than a console program.

2) Change the entry point from main to WinMain int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int)

3) Simply do not create a window (no created window = your program will run in the background)
It'll make it Windows-only, but can't you just do this? (Visual Studio)

 
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup") 


Test:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <fstream>

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")

int main( int argc, char* argv[] )
{
  std::ofstream of( "C:\\outfile.txt" );

  for( int i=0; i < 200; ++i )
    of << i << std::endl;

  of.close();

  return 0;
}


outfile.txt still produced despite no console window appearing.
Last edited on
thanks a lot for your answers & editing my question's problem .
another point :can we conclude from all these points & answers ( mentioned here ) , it's impossible to write a program which runs in background in unix like platforms ?

& if it's possible ( i sure it's ) how we can write it & what's the similar things & ways between the unix like hide processes writing & win hide processes writing ?
@mokhi64

I'm on Linux Mint 15 x64 and using int main and nothing more, I can start programs without using the console. Hope it helps.
@mokhi64

Looking at your other posts, it seems you are running Ubuntu, so the issue is nothing to do with how to write the program - it's how to run in the background from the shell.

Under UNIX / Linux It is simply this :

./MyProgram &

There is something very similar in the Windows Command Line, but I haven't used Windows for bleems.

Hope that is what you were after.

Not sure why your Original question mentioned not using various Windows features, then your last post talks about UNIX. Would it have been easier to mention you wanted to use Linux to start with? It is just that from the way your question was worded, it sounded like you wanted a Windows solution.

@TheIdeasMan
dear friend ,
i have 2 systems, on my Desktop System There is Ubuntu & on my Laptop MSWindows .

did you mean : I can use
 
system("./MyProgram &");

or something similar to hide my running program ?
@TheIdeasMan

not just ubuntu , i have MSWindows on my another PC.

your point was great !

but I meant hiding program(or process) by itself .
mokhi64 wrote:
but I meant hiding program(or process) by itself .


But what do you mean by hiding the program or process? Something different to it running in the background?

By running in the backgorund, I mean compiling it so the executable name MyProgram, then from the shell (Command Prompt) type ./MyProgram &.

This means the program will run in the background until you do something to make the program run in the foreground again or kill the process. Meanwhile you can still do other things from the shell.

Now why do you want to do this any way? I am starting to get suspicious about you wanting to hide things & not knowing what background processing means.
@iHutch105

EDIT I've just checked the current implementation of the CRT startup routines and I can see why your approach works. For now. But you should code against the published interface/API, rather than relying on internal knowledge.

The entrypoint for an app using /SUBSYSTEM:windows should have the same signature as WinMain, i.e.

1
2
3
4
5
6
 int CALLBACK WinMain(
  _In_  HINSTANCE hInstance,
  _In_  HINSTANCE hPrevInstance,
  _In_  LPSTR lpCmdLine,
  _In_  int nCmdShow
);


so you should really use either

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <fstream>

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")

int main( HINSTANCE, HINSTANCE, LPSTR, int )
{
  std::ofstream of( "C:\\outfile.txt" );

  for( int i=0; i < 200; ++i )
    of << i << std::endl;

  of.close();

  return 0;
}


Or avoid the use for #pragma comment and follow the lead of wxWidgets, FLTK, Qt (and others, I expect) and do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>

// TODO
// For some reason or other Microsoft only provide the Unicode version
// of this function:
// CommandLineToArgvW function
// http://msdn.microsoft.com/en-us/library/bb776391%28VS.85%29.aspx
LPSTR* CommandLineToArgv(LPCSTR lpCmdLine, int *pNumArgs);

int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR lpCmdLine, int)
{
  int argc = 0;
  char* argv = CommandLineToArgv(lpCmdLine, &argc);
  return main(argc, argv);
}

int main( int argc, char* argv[] )
{
  std::ofstream of( "C:\\outfile.txt" );

  for( int i=0; i < 200; ++i )
    of << i << std::endl;

  of.close();

  return 0;
}


If you want to avoid including <windows.h>, you could just lift the declaration for WinMain and paste it directly into the source file.

1
2
3
4
5
6
7
8
9
10
11
#define CALLBACK __stdcall

typedef void* HINSTANCE;
typedef char* LPSTR;

int CALLBACK WinMain(
        HINSTANCE hInstance,
        HINSTANCE hPrevInstance,
        LPSTR lpCmdLine,
        int nCmdShow
);


Andy
Last edited on
@TheIdeasMan

dear friend i knew all of these ,
i just want to know how does a function like
 
ShowWindow(hwnd_win,SW_HIDE);

works & how we can do its works without depending our program to any kind or way of programming (like WINAPI) . (=> i mean to write some function like it or to use a similar one which don't need to windows or unix SPECIAL headers & libraries).


so we can compile & then run it both in unix-like & windows platforms.
isn't good idea?
;)
Last edited on
so we can compile & then run it both in unix-like & windows platforms.

Windows and Linux aren't binary compatible. A Windows binary will not run on Linux, nor vice versa. So you cannot compile a single binary for use on both O/S.

You can run 32-bit Windows binaries (with some restricitions) on Linux using Wine. But that's not Linux running it directly.
http://www.winehq.org/

I am not aware of a siilar way of running a Linux binary on Windows, but you can (in both cases) use a virtual machine.

Andy
Last edited on
@ Andy
i didn't mean to run ONE binary on both
i meant to compile ONE CODE on both without problems
Pages: 12