Hi everyone,
this is my first post. I'll try to be concise.

It's been a while since I've worked with C++, since I usually work in Java. I'm writing a program that I want to have run in the background while the system (Windows XP) starts up. But I want it to be invisible, e.g., not have the DOS command window appear.
The program is in C++ (Borland C++ 3.1, old history!). Of course, it does not include any output to the screen, only works with files.

Thanks to anybody who can provide any insight!!

Recommended Answers

All 7 Replies

PS. My problem is just avoiding the command window coming up, in case I didn't mak myself clear. Thanks!

Windows executables have a flag in the header that causes that console window to appear or not.

Try changing

int main( int argc, char **argv )

to

#include <windows.h>
#include <shellapi.h>

int WndMain() {
  int argc;
  wchar_t **argv = CommandLineToArgvW( GetCommandLine(), &argc );

Notice that by compiling a Windows application your command line is now an array of wchar strings.

If that doesn't work, post back and I'll give you a little program to change the exe subsystem type manually.

Hope this helps.

> The program is in C++ (Borland C++ 3.1, old history!).
Then you're stuck, because all you can create are DOS programs.

If you want something smarter, you need to create a proper win32 service, and for that you'll need a compiler more in tune to the operating system you're using.

Sometimes backward compatibility is nice, but some people cling to it like some "security blanket".

[EDIT]
I didn't read to carefully. The compiler is so old it doesn't create Win32 exes. You'll have to use a utilit like HiddenStart: http://www.ntwind.com/software/utilities/hstart.html.

The following changes the subsystem type for Win32 exes.
[/EDIT]

#include <stdio.h>
#include <stdlib.h>

#define then

int main( int argc, char **argv ) {

  FILE *f;
  unsigned long  v = 0;
  unsigned short i = 0;
  char sig[ 4 ];

  if (argc != 2) {
    printf( "%s",
      "usage:\n"
      "  chss EXEFILE\n\n"

      "  Changes the subsystem type of the named exe file and reports the new\n"
      "  status: console or gui.\n\n"

      "  The file MUST be a PE32 or PE32+. No checking is done to verify that!\n"
      );
    return 0;
    }

  if ((f = fopen( argv[ 1 ], "rb+" )) == NULL) {
    fprintf( stderr, "Couldn't open %s", argv[ 1 ] );
    return 1;
    }

  /* addr of PE header signature */
  fseek( f, 60, SEEK_SET );
  fread( &v, 4, 1, f );

  fseek( f, v, SEEK_SET );
  fread( sig, 4, 1, f );
  if ((sig[ 0 ] != 'P') || (sig[ 1 ] != 'E') || (sig[ 2 ] != 0) || (sig[ 3 ] != 0)) {
    fprintf( stderr, "%s is not a PE32 or PE32+", argv[ 1 ] );
    return 1;
    }

  /* addr of subsystem code */
  v += 4 + 20 + 68;

  /* get current code */
  fseek( f, v, SEEK_SET );
  fread( &i, 2, 1, f );

  if (i == 3) 
    then {
      i = 2;
      printf( "%s converted from CONSOLE to GUI subsystem\n", argv[ 1 ] );
      }
    else {
      i = 3;
      printf( "%s converted from GUI to CONSOLE subsystem\n", argv[ 1 ] );
      }

  fseek( f, v, SEEK_SET );
  fwrite( &i, 2, 1, f );

  fclose( f );

  return 0;
  }

If it complains that your exe file is not a PE32 or PE32+ post back and I'll update the program to work with some more ancient Win32 exes.

You should consider using the GCC.

> #define then
I feel ill just looking at it.

>>The following changes the subsystem type for Win32 exes
And why in the world would he want to do all that? It isn't necessary just to hide the command window when a console program starts. As Salem mentioned, he can't do it with the compiler he is using and needs to get into the 21st century compiler.

Once you have a compiler that can access win32 api (32-bit compiler) its pretty easy to remove the command window. Your program will need to link with user32.lib, which is supplied with the free-to-download Windows SDK (Software Development Kit)

#include "windows.h"

...
int main()
{
    HWND hWnd = GetConsoleWindow();
    ShowWindow(hWnd, SW_HIDE);

    return 0;
}

Thank you very much everyone!! I guess I've been away from the C++ development clique too long :P
I think I'll start by getting myself a 21st Century compiler and going from there!
Thanks again!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.