Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. hide the console window while using system(...)

hide the console window while using system(...)

Scheduled Pinned Locked Moved C / C++ / MFC
20 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C chandu004

    hai all, in one of my applications, i have to automate a dos level command. so i construct the command into an str and execute it using system(str). but in this case, the console window is getting flashed while the command is being executed. could any one suggest any other alternative to avoid the display of console. thanks in advance.

    -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

    R Offline
    R Offline
    Rajesh R Subramanian
    wrote on last edited by
    #2

    Hello, Is it a Windowed application or a console application? Which dos level command are you executing?

    It is a crappy thing, but it's life -^ Carlo Pallini

    C 1 Reply Last reply
    0
    • C chandu004

      hai all, in one of my applications, i have to automate a dos level command. so i construct the command into an str and execute it using system(str). but in this case, the console window is getting flashed while the command is being executed. could any one suggest any other alternative to avoid the display of console. thanks in advance.

      -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #3

      You can hide the console if you use CreateProcess by setting the STARTF_USESHOWWINDOW flag and SW_HIDE in the wShowwindow members of the STARTUPINFO parameter. By I don't think you can run an internal DOS command using CreateProcess.

      «_Superman_» I love work. It gives me something to do between weekends.

      C 2 Replies Last reply
      0
      • R Rajesh R Subramanian

        Hello, Is it a Windowed application or a console application? Which dos level command are you executing?

        It is a crappy thing, but it's life -^ Carlo Pallini

        C Offline
        C Offline
        chandu004
        wrote on last edited by
        #4

        my application is windowed. actually, there are some Third party command mode tools which should be used in a sequence to perform some operations on wav files and text ifles. so i wanted to automate them using a single button click. and each of those tools print some output also, and i have to process and display that output also. as of now, iam achieving that by redirecting the output to a text file and reading it back. i would be pleased if you can suggest any easier method also. bu my immediate problem is to hide or suppress the console window. if my questin is not clear please let me know. i will try to explain it better. thank you.

        -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

        R 1 Reply Last reply
        0
        • _ _Superman_

          You can hide the console if you use CreateProcess by setting the STARTF_USESHOWWINDOW flag and SW_HIDE in the wShowwindow members of the STARTUPINFO parameter. By I don't think you can run an internal DOS command using CreateProcess.

          «_Superman_» I love work. It gives me something to do between weekends.

          C Offline
          C Offline
          chandu004
          wrote on last edited by
          #5

          «_Superman_» wrote:

          You can hide the console if you use CreateProcess by setting the STARTF_USESHOWWINDOW flag and SW_HIDE in the wShowwindow members of the STARTUPINFO parameter

          oh thank you. i will try it now.

          -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

          1 Reply Last reply
          0
          • C chandu004

            my application is windowed. actually, there are some Third party command mode tools which should be used in a sequence to perform some operations on wav files and text ifles. so i wanted to automate them using a single button click. and each of those tools print some output also, and i have to process and display that output also. as of now, iam achieving that by redirecting the output to a text file and reading it back. i would be pleased if you can suggest any easier method also. bu my immediate problem is to hide or suppress the console window. if my questin is not clear please let me know. i will try to explain it better. thank you.

            -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

            R Offline
            R Offline
            Rajesh R Subramanian
            wrote on last edited by
            #6

            chandu004 wrote:

            actually, there are some Third party command mode tools which should be used in a sequence to perform some operations on wav files and text ifles.

            So, you are not working with any DOS commands. Like the other poster suggested, you can use CreateProcess to hide the window that the new process will spawn. There are ways to capture and process output from a console window: Creating a child process with Redirected input and output[^] Redirecting an arbitrary Console's Input/Output[^] Writing to and read from the console - From a GUI application using the same cout/cin and printf/scanf[^] Generally, you need not use the system() command. It has several disadvantages over the APIs and can affect the performance of your program, for what it's worth. Consider using CreateProcess, ShellExecute, or a similar API function instead.

            It is a crappy thing, but it's life -^ Carlo Pallini

            C 2 Replies Last reply
            0
            • R Rajesh R Subramanian

              chandu004 wrote:

              actually, there are some Third party command mode tools which should be used in a sequence to perform some operations on wav files and text ifles.

              So, you are not working with any DOS commands. Like the other poster suggested, you can use CreateProcess to hide the window that the new process will spawn. There are ways to capture and process output from a console window: Creating a child process with Redirected input and output[^] Redirecting an arbitrary Console's Input/Output[^] Writing to and read from the console - From a GUI application using the same cout/cin and printf/scanf[^] Generally, you need not use the system() command. It has several disadvantages over the APIs and can affect the performance of your program, for what it's worth. Consider using CreateProcess, ShellExecute, or a similar API function instead.

              It is a crappy thing, but it's life -^ Carlo Pallini

              C Offline
              C Offline
              chandu004
              wrote on last edited by
              #7

              Rajesh R Subramanian wrote:

              So, you are not working with any DOS commands.

              no the sequence of command which i was talking about may also have them. Yes, i tried to use the create process. but..

              CString str;
              str.Format("copy database\\\\\*.\* %s>>c:\\\\output.txt",tempfolder);
              DeleteFile("output.txt");
              STARTUPINFO s;
              s.wShowWindow=STARTF\_USESHOWWINDOW&&SW\_SHOW ;
              int ret=CreateProcess(str.GetBuffer(str.GetLength()),NULL,NULL,NULL,NULL,NULL,NULL,NULL,&s,NULL);
              //system(str);
              

              there is no result. what is the mistake here.

              _ D 2 Replies Last reply
              0
              • _ _Superman_

                You can hide the console if you use CreateProcess by setting the STARTF_USESHOWWINDOW flag and SW_HIDE in the wShowwindow members of the STARTUPINFO parameter. By I don't think you can run an internal DOS command using CreateProcess.

                «_Superman_» I love work. It gives me something to do between weekends.

                C Offline
                C Offline
                chandu004
                wrote on last edited by
                #8

                iam still unable to achieve it. the return value of CreateProcess is 0. can you please give some clues? thank you.

                -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                _ 1 Reply Last reply
                0
                • C chandu004

                  Rajesh R Subramanian wrote:

                  So, you are not working with any DOS commands.

                  no the sequence of command which i was talking about may also have them. Yes, i tried to use the create process. but..

                  CString str;
                  str.Format("copy database\\\\\*.\* %s>>c:\\\\output.txt",tempfolder);
                  DeleteFile("output.txt");
                  STARTUPINFO s;
                  s.wShowWindow=STARTF\_USESHOWWINDOW&&SW\_SHOW ;
                  int ret=CreateProcess(str.GetBuffer(str.GetLength()),NULL,NULL,NULL,NULL,NULL,NULL,NULL,&s,NULL);
                  //system(str);
                  

                  there is no result. what is the mistake here.

                  _ Offline
                  _ Offline
                  _Superman_
                  wrote on last edited by
                  #9

                  chandu004 wrote:

                  STARTUPINFO s; s.wShowWindow=STARTF_USESHOWWINDOW&&SW_SHOW ;

                  s.dwFlags = STARTF_USESHOWWINDOW;
                  s.wShowWindow = SW_SHOW;

                  «_Superman_» I love work. It gives me something to do between weekends.

                  C 1 Reply Last reply
                  0
                  • C chandu004

                    iam still unable to achieve it. the return value of CreateProcess is 0. can you please give some clues? thank you.

                    -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                    _ Offline
                    _ Offline
                    _Superman_
                    wrote on last edited by
                    #10

                    I think you're not doing it right. See my earlier post here - http://www.codeproject.com/Messages/3100157/Re-hide-the-console-window-while-using-system.aspx[^]

                    «_Superman_» I love work. It gives me something to do between weekends.

                    1 Reply Last reply
                    0
                    • _ _Superman_

                      chandu004 wrote:

                      STARTUPINFO s; s.wShowWindow=STARTF_USESHOWWINDOW&&SW_SHOW ;

                      s.dwFlags = STARTF_USESHOWWINDOW;
                      s.wShowWindow = SW_SHOW;

                      «_Superman_» I love work. It gives me something to do between weekends.

                      C Offline
                      C Offline
                      chandu004
                      wrote on last edited by
                      #11

                      no difference still iam using it this way.

                      STARTUPINFO s;
                      s.dwFlags=STARTF\_USESHOWWINDOW;
                      s.wShowWindow=SW\_SHOW ;
                      int ret=CreateProcess(str,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&s,NULL);
                      

                      here, str contains the command to be executed. which is working perfectly when i use it with system(). what is the mistake here?

                      _ 1 Reply Last reply
                      0
                      • C chandu004

                        no difference still iam using it this way.

                        STARTUPINFO s;
                        s.dwFlags=STARTF\_USESHOWWINDOW;
                        s.wShowWindow=SW\_SHOW ;
                        int ret=CreateProcess(str,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&s,NULL);
                        

                        here, str contains the command to be executed. which is working perfectly when i use it with system(). what is the mistake here?

                        _ Offline
                        _ Offline
                        _Superman_
                        wrote on last edited by
                        #12

                        Try this.

                        STARTUPINFO si;
                        PROCESS_INFORMATION pi;

                        ::SecureZeroMemory(&si, sizeof(STARTUPINFO));
                        ::SecureZeroMemory(&pi, sizeof(PROCESS_INFORMATION));

                        si.dwFlags = STARTF_USESHOWWINDOW;
                        si.wShowWindow = SW_SHOW;

                        ::CreateProcess(NULL, str, NULL, NULL, NULL, NULL, NULL, NULL, &si, &pi);

                        «_Superman_» I love work. It gives me something to do between weekends.

                        C 1 Reply Last reply
                        0
                        • _ _Superman_

                          Try this.

                          STARTUPINFO si;
                          PROCESS_INFORMATION pi;

                          ::SecureZeroMemory(&si, sizeof(STARTUPINFO));
                          ::SecureZeroMemory(&pi, sizeof(PROCESS_INFORMATION));

                          si.dwFlags = STARTF_USESHOWWINDOW;
                          si.wShowWindow = SW_SHOW;

                          ::CreateProcess(NULL, str, NULL, NULL, NULL, NULL, NULL, NULL, &si, &pi);

                          «_Superman_» I love work. It gives me something to do between weekends.

                          C Offline
                          C Offline
                          chandu004
                          wrote on last edited by
                          #13

                          securezeromemory undeclared identifier. this is how the vc++6.0 bangs me. though i included winbase.h. here i will try to simplify my problem. say,

                          str="dir >>c:\\output.txt";

                          now i want this command to be executed . can we work in this direction?

                          -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                          _ 1 Reply Last reply
                          0
                          • C chandu004

                            securezeromemory undeclared identifier. this is how the vc++6.0 bangs me. though i included winbase.h. here i will try to simplify my problem. say,

                            str="dir >>c:\\output.txt";

                            now i want this command to be executed . can we work in this direction?

                            -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                            _ Offline
                            _ Offline
                            _Superman_
                            wrote on last edited by
                            #14

                            Use memset instead.

                            «_Superman_» I love work. It gives me something to do between weekends.

                            1 Reply Last reply
                            0
                            • C chandu004

                              Rajesh R Subramanian wrote:

                              So, you are not working with any DOS commands.

                              no the sequence of command which i was talking about may also have them. Yes, i tried to use the create process. but..

                              CString str;
                              str.Format("copy database\\\\\*.\* %s>>c:\\\\output.txt",tempfolder);
                              DeleteFile("output.txt");
                              STARTUPINFO s;
                              s.wShowWindow=STARTF\_USESHOWWINDOW&&SW\_SHOW ;
                              int ret=CreateProcess(str.GetBuffer(str.GetLength()),NULL,NULL,NULL,NULL,NULL,NULL,NULL,&s,NULL);
                              //system(str);
                              

                              there is no result. what is the mistake here.

                              D Offline
                              D Offline
                              David Crow
                              wrote on last edited by
                              #15

                              chandu004 wrote:

                              what is the mistake here.

                              The "mistake" is that you are not using SHFileOperation() to copy the folder contents.

                              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                              C 1 Reply Last reply
                              0
                              • D David Crow

                                chandu004 wrote:

                                what is the mistake here.

                                The "mistake" is that you are not using SHFileOperation() to copy the folder contents.

                                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                C Offline
                                C Offline
                                chandu004
                                wrote on last edited by
                                #16

                                thanks for your reply. can you pleaase explain me how and where to use this function? thanks in advance.

                                -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                                D 1 Reply Last reply
                                0
                                • C chandu004

                                  thanks for your reply. can you pleaase explain me how and where to use this function? thanks in advance.

                                  -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                                  D Offline
                                  D Offline
                                  David Crow
                                  wrote on last edited by
                                  #17

                                  chandu004 wrote:

                                  can you pleaase explain me how and where to use this function?

                                  See here and here and here.

                                  "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                  1 Reply Last reply
                                  0
                                  • C chandu004

                                    hai all, in one of my applications, i have to automate a dos level command. so i construct the command into an str and execute it using system(str). but in this case, the console window is getting flashed while the command is being executed. could any one suggest any other alternative to avoid the display of console. thanks in advance.

                                    -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                                    J Offline
                                    J Offline
                                    Joe Woodbury
                                    wrote on last edited by
                                    #18

                                    I often use ShellExecuteEx (look it up to know what libraries to link.) This wraps CreateProcess in an easier to use form. I've cut and paste a modified version here.) To execute a copy; pass "cmd.exe" as the app and "/c command" as the args. Set pDir to the dir where you want the operation to take place. Pass false for show. (The /c tells cmd.exe to execute the command and then exit.)

                                    bool Execute(LPCTSTR pApp, LPCTSTR pArgs, bool show, LPCTSTR pDir)
                                    {
                                    if (!pApp)
                                    {
                                    SetLastError(ERROR_FILE_NOT_FOUND);
                                    return false;
                                    }

                                    SHELLEXECUTEINFO execInfo;
                                    memset(&execInfo, 0, sizeof(execInfo));
                                    
                                    execInfo.cbSize       = sizeof(execInfo);
                                    execInfo.fMask        = SEE\_MASK\_NOCLOSEPROCESS | SEE\_MASK\_FLAG\_NO\_UI;
                                    execInfo.lpFile       = pApp;
                                    execInfo.lpParameters = pArgs;
                                    execInfo.nShow        = show ? SW\_SHOW : SW\_HIDE;
                                    
                                    char path\[MAX\_PATH\];
                                    
                                    if (pDir)
                                    {
                                    	execInfo.lpDirectory = pDir;
                                    }
                                    else
                                    {
                                    	lstrcpyn(path, pApp, MAX\_PATH);
                                    	if (PathRemoveFileSpec(path))
                                    	{
                                    		if (path\[0\])
                                    			execInfo.lpDirectory = path;
                                    	}
                                    }
                                    

                                    // DTDirExits is an internal function that uses GetFileAttributes() to test if a path is an existing directory
                                    if (execInfo.lpDirectory && !DTDirExists(execInfo.lpDirectory))
                                    {
                                    SetLastError(ERROR_PATH_NOT_FOUND);
                                    return false;
                                    }

                                    if (!ShellExecuteEx(&execInfo))
                                    	return false;
                                    
                                    if (execInfo.hProcess)
                                    	CloseHandle(execInfo.hProcess);
                                    
                                    return true;
                                    

                                    }

                                    modified on Tuesday, August 4, 2009 11:15 AM

                                    C 1 Reply Last reply
                                    0
                                    • J Joe Woodbury

                                      I often use ShellExecuteEx (look it up to know what libraries to link.) This wraps CreateProcess in an easier to use form. I've cut and paste a modified version here.) To execute a copy; pass "cmd.exe" as the app and "/c command" as the args. Set pDir to the dir where you want the operation to take place. Pass false for show. (The /c tells cmd.exe to execute the command and then exit.)

                                      bool Execute(LPCTSTR pApp, LPCTSTR pArgs, bool show, LPCTSTR pDir)
                                      {
                                      if (!pApp)
                                      {
                                      SetLastError(ERROR_FILE_NOT_FOUND);
                                      return false;
                                      }

                                      SHELLEXECUTEINFO execInfo;
                                      memset(&execInfo, 0, sizeof(execInfo));
                                      
                                      execInfo.cbSize       = sizeof(execInfo);
                                      execInfo.fMask        = SEE\_MASK\_NOCLOSEPROCESS | SEE\_MASK\_FLAG\_NO\_UI;
                                      execInfo.lpFile       = pApp;
                                      execInfo.lpParameters = pArgs;
                                      execInfo.nShow        = show ? SW\_SHOW : SW\_HIDE;
                                      
                                      char path\[MAX\_PATH\];
                                      
                                      if (pDir)
                                      {
                                      	execInfo.lpDirectory = pDir;
                                      }
                                      else
                                      {
                                      	lstrcpyn(path, pApp, MAX\_PATH);
                                      	if (PathRemoveFileSpec(path))
                                      	{
                                      		if (path\[0\])
                                      			execInfo.lpDirectory = path;
                                      	}
                                      }
                                      

                                      // DTDirExits is an internal function that uses GetFileAttributes() to test if a path is an existing directory
                                      if (execInfo.lpDirectory && !DTDirExists(execInfo.lpDirectory))
                                      {
                                      SetLastError(ERROR_PATH_NOT_FOUND);
                                      return false;
                                      }

                                      if (!ShellExecuteEx(&execInfo))
                                      	return false;
                                      
                                      if (execInfo.hProcess)
                                      	CloseHandle(execInfo.hProcess);
                                      
                                      return true;
                                      

                                      }

                                      modified on Tuesday, August 4, 2009 11:15 AM

                                      C Offline
                                      C Offline
                                      chandu004
                                      wrote on last edited by
                                      #19

                                      thank you, i would try your logic also. thanks again.

                                      -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                                      1 Reply Last reply
                                      0
                                      • R Rajesh R Subramanian

                                        chandu004 wrote:

                                        actually, there are some Third party command mode tools which should be used in a sequence to perform some operations on wav files and text ifles.

                                        So, you are not working with any DOS commands. Like the other poster suggested, you can use CreateProcess to hide the window that the new process will spawn. There are ways to capture and process output from a console window: Creating a child process with Redirected input and output[^] Redirecting an arbitrary Console's Input/Output[^] Writing to and read from the console - From a GUI application using the same cout/cin and printf/scanf[^] Generally, you need not use the system() command. It has several disadvantages over the APIs and can affect the performance of your program, for what it's worth. Consider using CreateProcess, ShellExecute, or a similar API function instead.

                                        It is a crappy thing, but it's life -^ Carlo Pallini

                                        C Offline
                                        C Offline
                                        chandu004
                                        wrote on last edited by
                                        #20

                                        Rajesh R Subramanian wrote:

                                        Redirecting an arbitrary Console's Input/Output[^]

                                        dear rajesh, this seems to be a perfect source match for my type of requirement. today iam planning to integrate this feature into my app. but i found it in vs 2008. i have to use it in vc 6.0. i will try it and come here if iam facing any problems. many more thanks again.

                                        -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups