RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 1257 | Replies: 7 | Thread Tools  Display Modes
Reply
Join Date: Oct 2007
Location: Guadalajara, Mexico
Posts: 3
Reputation: guaild is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
guaild guaild is offline Offline
Newbie Poster

How to make the command window not come up while running an .exe

  #1  
Oct 23rd, 2007
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!!
Alejandro Ramírez Lovering
Java Developer
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Guadalajara, Mexico
Posts: 3
Reputation: guaild is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
guaild guaild is offline Offline
Newbie Poster

Re: How to make the command window not come up while running an .exe

  #2  
Oct 23rd, 2007
PS. My problem is just avoiding the command window coming up, in case I didn't mak myself clear. Thanks!
Alejandro Ramírez Lovering
Java Developer
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,884
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 197
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: How to make the command window not come up while running an .exe

  #3  
Oct 24th, 2007
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.
Reply With Quote  
Join Date: Dec 2005
Posts: 4,215
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 26
Solved Threads: 493
Colleague
Salem's Avatar
Salem Salem is offline Offline
Industrious Poster

Re: How to make the command window not come up while running an .exe

  #4  
Oct 24th, 2007
> 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".
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,884
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 197
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: How to make the command window not come up while running an .exe

  #5  
Oct 24th, 2007
[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.
Last edited by Duoas : Oct 24th, 2007 at 8:01 am.
Reply With Quote  
Join Date: Dec 2005
Posts: 4,215
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 26
Solved Threads: 493
Colleague
Salem's Avatar
Salem Salem is offline Offline
Industrious Poster

Re: How to make the command window not come up while running an .exe

  #6  
Oct 24th, 2007
> #define then
I feel ill just looking at it.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,862
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 1013
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: How to make the command window not come up while running an .exe

  #7  
Oct 24th, 2007
>>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;
}
<<Hire Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Location: Guadalajara, Mexico
Posts: 3
Reputation: guaild is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
guaild guaild is offline Offline
Newbie Poster

Re: How to make the command window not come up while running an .exe

  #8  
Oct 24th, 2007
Thank you very much everyone!! I guess I've been away from the C++ development clique too long
I think I'll start by getting myself a 21st Century compiler and going from there!
Thanks again!!
Alejandro Ramírez Lovering
Java Developer
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the C++ Forum
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:07 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC