Solved

Batch file without DOS window coming up

Posted on 1998-10-14
26
284 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:georita
  • 9
  • 6
  • 5
  • +2
26 Comments
 
LVL 1

Expert Comment

by:Bonev
ID: 1415243
Try to pass SW_HIDE as a second parameter to WinExec.

0
 

Author Comment

by:georita
ID: 1415244
I am sorry, but that did not work.  
0
 
LVL 1

Expert Comment

by:Musashi
ID: 1415245

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
 
LVL 3

Expert Comment

by:arunm
ID: 1415246
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
 
LVL 22

Expert Comment

by:nietod
ID: 1415247
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
 
LVL 3

Expert Comment

by:arunm
ID: 1415248
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
 
LVL 1

Expert Comment

by:Bonev
ID: 1415249
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
 

Author Comment

by:georita
ID: 1415250
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
 
LVL 1

Expert Comment

by:Bonev
ID: 1415251
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
 

Author Comment

by:georita
ID: 1415252
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
 
LVL 1

Expert Comment

by:Bonev
ID: 1415253
Another approach is to create a shortcut to this batch file and to change the Run property (under Program) to Minimize.
0
 

Author Comment

by:georita
ID: 1415254
The Batch file is created and run at run time.  So I can't do that...
0
 
LVL 1

Expert Comment

by:Bonev
ID: 1415255
Of course, you can. The only thing you need to do is to keep the batch file name the same.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Expert Comment

by:arunm
ID: 1415256
Yes, but the shortcut does not have to be. (I like this suggestion )

0
 

Author Comment

by:georita
ID: 1415257
I am not sure that this is the route that I wanted to take.  InstallShield is not really the greatest scripting lang.
0
 
LVL 1

Expert Comment

by:Musashi
ID: 1415258
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
 

Author Comment

by:georita
ID: 1415259
These are all great suggestions.  submit as answer.  Thanks
0
 
LVL 1

Expert Comment

by:Musashi
ID: 1415260
whom would you like to answer?
0
 

Author Comment

by:georita
ID: 1415261
You guys decide.
0
 
LVL 1

Expert Comment

by:Musashi
ID: 1415262
Sounds like none of us helped you.
Why would we want credit?
0
 

Author Comment

by:georita
ID: 1415263
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
 
LVL 3

Expert Comment

by:arunm
ID: 1415264
I agree, but I think musashi & Bonev were closest.

0
 
LVL 3

Expert Comment

by:arunm
ID: 1415265
I agree, but I think musashi & Bonev were closest.

0
 
LVL 1

Expert Comment

by:Bonev
ID: 1415266
Georita, it's your decision.

0
 

Author Comment

by:georita
ID: 1415267
Everyone helped, but I guess I will give it to Musashi this time.
Thanks,
Georg Haugland
0
 
LVL 1

Accepted Solution

by:
Musashi earned 50 total points
ID: 1415268
OK, thank you Georg.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now