Closed Thread
Results 1 to 4 of 4

Thread: Executing applications and CMD commands from C++

  1. #1
    N00bDaan is offline Newbie
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Executing applications and CMD commands from C++

    Hello,

    I have been looking for this for quite some time now, but I just can't seem to find a clear solution to the problem.

    I am trying to make a program which executed at the logon of Windows, and then run's a few programs in a specific order and also has to execute some commandlines. Now the easiest way is to use the SYSTEM() function, which enables you to do this up to a certain point. I am currently working in WxDev C++, altough what I am showing here is just some C programming really.

    Code:
    #include <wx/file.h>
    #include <wx/utils.h>
    #include <stdlib.h>
    
    int main (int argc, char* argv[])
      system("SUBST Y: E:\Tools");
      Sleep(100);
      system("SUBST W: H:\Series");   
      Sleep(100);
    // This works, but the following subst will not work
      system("SUBST Z: "F:\Temporary Files"");   
    // The extra quotes (""), around the path confuses the SYSTEM function
    // Now running some apps
       system("start mIRC");
       Sleep(4000);
       system("start ICQ");
       Sleep(11000);
       return 0;
    }
    In the directory where the program is located there are also the shortcuts to mIRC and ICQ, therefore the 'start ...' command works.

    There are however a few drawbacks, first of all with the system command, I can not subst the third dir and secondly the system command is not the most subtle way to run applications (this I read somewhere).

    I searched the web for a few days now, but I can not find a solution to this although I expect I should switch to C++ (Either Borland or Visual C++) or maybe C# (Microsoft). There should be a replacement for the SYSTEM command right, because I think the SYSTEM() function is actually C programming, not C++.

    I came acros this: System.Diagnostics.Process.Start which should be worth a shot, can someone help me on the way? Even books I looked at did't cover running applications, it was mostly on Win32 API, making windows and stuff...

    Cheers,
    Daan

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    The system()-function which can be find in cstdlib (stdlib.h), is the most common and most used function for doing command prompt commands. It's fast, cross-platform (though somebody thinks something else - explanation follows), and after all, the easiest to use.

    Many people don't think system() is cross-platform, but it actually is. But that doesn't mean that the commands for system() is cross-platform, because they are not! So system() is fully "valid".

    Usually, you don't use system() function much though. Many of the operations are better done in C (or C++), so the need for the function is not big. You don't use it to start other application neither. There are two good alternatives for starting other applications. There are ShellExecute and CreateProcess, which both can be found in the windows-header (windows.h)

    ShellExecute is probably the easiest to use, though there's a lot of different ways to use it. If you just want to start another application, the easiest way to that, is f.ex. to create a function like this:
    Code:
    void Start(LPCTSTR lpFile)
    {
    	ShellExecute(NULL, "open", lpFile, NULL, NULL, SW_SHOWNORMAL);
    }
    Then you can simply pass the full pathname of the application, to the function. Remember to escape the backslashes, and remember to include the windows header.
    Code:
    #include <windows.h>
    // ...
    Start("C:\\WINDOWS\\system32\\cmd.exe"); // Start a command prompt
    // The path can differ from system to system
    The other alternative, CreateProcess, is a lot more complex, and harder to use - but you'll get a lot more control. There's so many different ways to use CreateProcess, so I've hard to show you a single one. Instead I'll provide links, to get you further with both ShellExecute and CreateProcess.

    You can find a lot information about the two alternatives, at MSDN.
    Click here for information about ShellExecute.
    Click here for information about CreateProcess.

  4. #3
    N00bDaan is offline Newbie
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0
    Thanks v0id, that solved my problems as far as running some apps. I used the CreateProcess method which works quite well now. The remaining problem is executing some CMD commands (like ipconfig, subst, label or whatever).

    On the msdn website there is someone who has some trouble executing a batch file:

    * The library I used is from Dev-C++ 4.9.9.2. I haven't try the VC's. The OS is Windows XP, SP2 */

    The "Parameters lpApplicationName" section above says "To run a batch file, you must start the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine to the name of the batch file." But when I did so in a console program with:

    CreateProcess("C:\\Windows\\system32\\cmd.exe", "runBat.bat", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

    or

    CreateProcess(NULL, "C:\\Windows\\system32\\cmd.exe runBat.bat", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
    , the cmd.exe ignored the "runBat.bat" completely, i.e. the new cmd instance was booted smoothly while my "runBat.bat" wasn't executed. Furthermore, when I specified the cmd.exe without its complete path as:

    CreateProcess("cmd.exe", "runBat.bat", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
    CreateProcess() return with FALSE, and GetLastError() return 2.

    At last I found the followings work:

    CreateProcess(NULL, "runBat.bat file1.txt file2.txt", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

    and

    CreateProcess("runBat.bat ", "file1.txt file2.txt", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
    I tried it in the same ways he did however his solution to the problem does not work for me, or so it seems... Sure I can execute cmd.exe, but I get it in no way to execute a command like ipconfig or anything...

    Anyone had a suggestion?

  5. #4
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    If you want to use commands, like those in the command prompt - then system() is just fine. Actually, everything that goes to the system() function is like doing it in the command prompt.
    So something like this:
    Code:
    system("ipconfig /h");
    Is the same as typing this in the command prompt:
    Code:
    C:\...\> ipconfig /h
    So I see no reasons not to use the system() function.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Executing windows button presses
    By xXAlphaXx in forum C and C++
    Replies: 7
    Last Post: 12-24-2010, 03:57 PM
  2. Help me in executing MAKEFILE?
    By kashiqirphan in forum Java Help
    Replies: 1
    Last Post: 01-29-2009, 08:07 AM
  3. Executing java files
    By DivideByZero in forum Java Help
    Replies: 9
    Last Post: 05-06-2008, 05:06 AM
  4. Executing JavaScript in C#
    By dirkfirst in forum C# Programming
    Replies: 2
    Last Post: 01-31-2008, 06:36 PM
  5. Executing a command
    By Chan in forum PHP Development
    Replies: 1
    Last Post: 07-19-2006, 06:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts