Windows knows two kinds of programs: GUI and console.
If a program is marked as a console program, a console is created for it when it is started, even if the console is not used (Windows doesn't know the console isn't going to be used).
There are two exceptions on this rule:
1. A console program can inherit the console of it's parent. This means that the parent must also be a console program.
2. A console program can be started with the DETACHED_PROCESS flag, in which case a console is not created. But if you don't have control over the parent program (might be Explorer), there is no way to set it as far as I know.
So the best option is: make a GUI program of it.
How to make a GUI out of a console program:
1. include <windows.h>
2. change the entrypoint from
- int main ( int argc, char * argv[] )
to :
- int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCommandLine, int nCmdShow )
Note: You do not have the argc and argv[] arguments, only a pointer (lpCommandLine) to the command line, which is WITHOUT the first argument, the program name. If you need the program name as well, you can get a pointer to the entire command line by calling the GetCommandLine ( ) API.
You have to parse or convert the command line to argc / argv[] yourself.
3. When the linker does not recognize the entrypoint, and gives an unresolved external error on _main, add the option "-subsystem:windows" (without the quotes) to the link command.
In order to maintain compatibility with gcc (which I don't know), you can use (when you use Visual C in Windows) the "/DWINDOWS" option when compiling in Visual C. This will define WINDOWS as if there were a #define WINDOWS statement prior to compilation. Your source will contain:
Code:
....
#ifdef WINDOWS
#include <windows.h>
#endif
...
#ifdef WINDOWS
#define MAX_ARGUMENTS 100
int argc;
char *argv[MAX_ARGUMENTS];
void ParseCommandLine ( LPSTR lpCommandLine )
{ // convert lpCommandLine to argc and argv[]
}
int WINAPI WinMain ( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCommandLine,
int nCmdShow )
{
ParseCommandLine ( lpCommandLine );
#else
int main ( int argc, char * argv[] )
{
#endif
// body of main
}
Marcel