6 Replies - 4772 Views - Last Post: 19 July 2012 - 10:29 PM Rate Topic: -----

#1 joshuapoole293@gmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-July 12

Executing Dos Commands In C++ with no window

Posted 19 July 2012 - 06:13 AM

Hi I am executing DOS Commadns in C++ using the system() command. I am actually using XCOPY but i want to ensure the command prompt does not display.

I have tried WINEXEC() this does not work either.
Ant one got any ideas or any examples? I am new to C++.

My code at the minute:

updateReport(source, "N/A");
string command = "xcopy " + source + " " + dest;
const char * cmd = command.c_str();
system(cmd);



Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Executing Dos Commands In C++ with no window

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 678
  • View blog
  • Posts: 2,339
  • Joined: 31-December 10

Re: Executing Dos Commands In C++ with no window

Posted 19 July 2012 - 07:18 AM

I'm not too familiar with calling DOS commands in C++ as I am running Linux, but one suggestion is to check the return value of the system call to see if succeeded or failed. Check this page for more info: http://cplusplus.com...cstdlib/system/.
Was This Post Helpful? 0
  • +
  • -

#3 joshuapoole293@gmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-July 12

Re: Executing Dos Commands In C++ with no window

Posted 19 July 2012 - 07:49 AM

System works fine ! Its just that with System i get a DOS Box
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: Executing Dos Commands In C++ with no window

Posted 19 July 2012 - 07:58 AM

Checking the return value of system() will not do anything from the CMD window flashing open while the command that you pass in runs.

Is your objective to recursively copy files from source to destination, or are you asking the generic question of how to prevent the CMD window from flashing open?

If the former, you can use the FindFirstFile() and FindNextFile() Win32 APIs to find filenames, the CopyFile() Win32 API to copy the file.

If the latter, you can try using the ShellExecute() Win32 API and try passing in SW_HIDE, or SW_MINIMIZE. In my experience, though, if the program being run is console program, the CMD window still flashes open.

This post has been edited by Skydiver: 19 July 2012 - 08:10 AM

Was This Post Helpful? 1
  • +
  • -

#5 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 678
  • View blog
  • Posts: 2,339
  • Joined: 31-December 10

Re: Executing Dos Commands In C++ with no window

Posted 19 July 2012 - 08:11 AM

Are these plain text files you're copying?
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,744
  • Joined: 19-February 09

Re: Executing Dos Commands In C++ with no window

Posted 19 July 2012 - 07:00 PM

You can try creating a shortcut.

Right-click on the executable and choose Create Shortcut.
Right-click on the shortcut select Properties.
The Shortcut tab should be visible, in the Run box select Minimized and then Apply.

Use the shortcut to run the program.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: Executing Dos Commands In C++ with no window

Posted 19 July 2012 - 10:29 PM

Okay, I have a definitive answer... At least on Windows 7 and Vista. ShellExecute() with SW_HIDE will not show the window for xcopy.exe. I can't vouch for other console programs.

    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    HINSTANCE hinstRun = 
    ShellExecute(NULL,
                 L"open",
                 L"xcopy.exe",
                 L"C:\\z\\Test\\*.* C:\\z\\Test\\Dummy",
                 L"",
                 SW_HIDE);
    CoUninitialize();



Also, I have no idea what is going to happen if you have a hidden console window and the console app happens to prompt you for information or a confirmation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1