Batch file without DOS window coming up

Is there a way to make a batch file run w/out bring up a DOS
window?  I am creating a batch file in real time and need it to run, but I don't need it to bring up the stupid DOS window.
Is there a command line parameter I can pass.  I am using WinExec to start the batch file. Thanks
georitaAsked:
Who is Participating?
 
MusashiCommented:
OK, thank you Georg.
0
 
BonevCommented:
Try to pass SW_HIDE as a second parameter to WinExec.

0
 
georitaAuthor Commented:
I am sorry, but that did not work.  
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
MusashiCommented:

That's odd.  On NT, the following program gives me no DOS window.  I first used SW_HIDE instead of 0 for the second parm, with the same results.  Joke.bat simply does a dir >c:\joke2.txt, and running batch.exe always results in joke2.txt being created.  

Anyhow, perhaps if you go to DOS and type "Help Start" you will find what you need, perhaps with the /B switch.
----------------------------------------------------------
// batch.cpp : Defines the entry point for the application.
//

#include "stdafx.h"

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
      {
       WinExec ("C:\\Joke.bat", 0);
      return 0;
      }

0
 
arunmCommented:
One general solution that works with any console application is to execute the batch file as a minimised program. There is no simple way of doing this and any solution involves writing code. I find the easiest way is create a short application to execute your batch file. You can then useCreateProcess() to run the file and pass console minimised as a
parameter (see the argument list). The batch file will then only appear
briefly as a minimised application on your task bar.

/* @(#)launch.cpp
**
** This program is used  to hide the console windows generated
** by the subsystem.
**
**
*****************************************/


#include <afx.h>
#include <iostream.h>
#include <string.h>
#include <winbase.h>
#include <process.h>
#include <stdlib.h>


#define MAX_CHARS_IN_PID 16
#define MAX_FILENAME_LEN 256
static int ReportStatus(char *ProcessName, int status, FILE *handle);

int main(int argc, char *argv[])
{
      PROCESS_INFORMATION ProcessInfo;
      STARTUPINFO                  InfoToPass;
      BOOL                        status;
      char                        ParentProcessIdString[MAX_CHARS_IN_PID];
      char                        logfile[MAX_FILENAME_LEN];
      char                        *tmp;
      FILE                        *handle;


      tmp = getenv("TEMPDIR");
      if (tmp == NULL)
            return(-1);

      strcat(tmp, "\\launch.log");
      strcpy(logfile, tmp);
      handle = fopen(logfile, "w");

      if (handle == NULL)
            return(-1);

      if (argc < 2)
      {
            fprintf(handle, "Invalid arguments: Unable to attempt to spawn process\n");
            fclose(handle);
            return -1;      

      }
      else
      {
            fprintf(handle, "Program launcher initialised\n");
      }

      // Initialise Startup structure
      InfoToPass.cb = sizeof(InfoToPass);
      InfoToPass.lpReserved = NULL;
      InfoToPass.lpDesktop = NULL;
      InfoToPass.lpTitle = NULL;
      InfoToPass.dwFlags = STARTF_USESHOWWINDOW;
      InfoToPass.cbReserved2 = 0;
      InfoToPass.lpReserved2 = NULL;
      InfoToPass.wShowWindow = SW_SHOWMINIMIZED;

      // Spawn required process as a minimised console process

      status = CreateProcess(argv[1], "", NULL, NULL, FALSE,
            CREATE_NEW_CONSOLE, NULL, NULL, &InfoToPass, &ProcessInfo);


      // Report Process status
      ReportStatus(argv[2], status, handle);

      fclose(handle);
      return (0);

}

static int ReportStatus(char *ProcessName, int status, FILE *handle)
{
      int ErrorVal;

      // Errno is set by c runtime routines, but CreateProcess() is a
      // Windows API. check the passed return code and GetLastError()
      // instead of errno

      if (status)
      {      
            ErrorVal = 0;
            fprintf(handle, "launching %s\n",ProcessName);
      
      }
      else
      {
            ErrorVal = GetLastError();
            fprintf(handle, "Program initialisation failed for %s (Error %d)\n",
                  ErrorVal,ProcessName);


      }

      return (ErrorVal);

}


you would have to build this a application with  /entry:"mainCRTStartup" as link flag  if it is a console application  (i.e. main() as the main loop ). This is to stop it generating its own console window.

0
 
nietodCommented:
arunm's solution is a bit unnecessarily complex.  You can just use CreateProcess() as he showed from your program.  But the program to be run should be "command.com" and the batch file to be run should be listed as a parameter.  There is no need to create the additional program arunm suggested.
0
 
arunmCommented:
You are quite right nietod. I am describing a generic method of solving the problem of hiding console application subwindows. There is certainly no need to use this verbose version. (unless you need the extra stuff, such logging etc... )
 


0
 
BonevCommented:
Arunm, your solution is not much different than mine - WinExec is implemented as a call to CreateProcess under Windows 95/NT.

Georita, it works just fine under Windows 95. And since Musashi did it under NT, I think you should give us more details.

0
 
georitaAuthor Commented:
I am simply running a batch file inside of another program, which happens to be Install Shield.  The function call is LaunchAppAndWait(filename, commandline, WAIT) which encapsulates the WinExec Api function.  It needs to work in 95 and NT. i am looking for the value to pass in the commandline parameter.  
The proposed answer is not really what I am looking for, althought it could be hidden in there somewhere.  sorry.
0
 
BonevCommented:
If Install Shield does not provide you with means to hide the executed program, may be you will need to write a simple launcher application that will do that for you and to execute it from Install Shield.

Like (borrowed part of it from Musashi):
#include "stdafx.h"

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  WinExec (lpCmdLine, SW_HIDE);
  return 0;
}

0
 
georitaAuthor Commented:
I would prefer not to add a helper application.  I just want to pass in a command that will hide, or minimize the dos window.  Maybe there isn't one, but I have a feeling there is.
0
 
BonevCommented:
Another approach is to create a shortcut to this batch file and to change the Run property (under Program) to Minimize.
0
 
georitaAuthor Commented:
The Batch file is created and run at run time.  So I can't do that...
0
 
BonevCommented:
Of course, you can. The only thing you need to do is to keep the batch file name the same.
0
 
arunmCommented:
Yes, but the shortcut does not have to be. (I like this suggestion )

0
 
georitaAuthor Commented:
I am not sure that this is the route that I wanted to take.  InstallShield is not really the greatest scripting lang.
0
 
MusashiCommented:
I think the helper app is very simple and should work fine for you.  You are just trying to get around a problem creating by Installshield's encapsulation of the WinApi.

Also, did you try using cmd.exe for the filename and "Start /b Batchfile.bat" for the command line as I suggested earlier?  Or "cmd.exe start /b Batchfile.bat", if that is what they need.
0
 
georitaAuthor Commented:
These are all great suggestions.  submit as answer.  Thanks
0
 
MusashiCommented:
whom would you like to answer?
0
 
georitaAuthor Commented:
You guys decide.
0
 
MusashiCommented:
Sounds like none of us helped you.
Why would we want credit?
0
 
georitaAuthor Commented:
Because you gave me alot good ideas that , even if they don't directly solve the problem,
save me trouble of trying out what you guys already did.
0
 
arunmCommented:
I agree, but I think musashi & Bonev were closest.

0
 
arunmCommented:
I agree, but I think musashi & Bonev were closest.

0
 
BonevCommented:
Georita, it's your decision.

0
 
georitaAuthor Commented:
Everyone helped, but I guess I will give it to Musashi this time.
Thanks,
Georg Haugland
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.