Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-03-2007, 10:09 AM
N00bDaan N00bDaan is offline
Newbie
 
Join Date: Jul 2007
Posts: 2
Rep Power: 0
N00bDaan is on a distinguished road
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-03-2007, 11:26 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

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.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-03-2007, 01:41 PM
N00bDaan N00bDaan is offline
Newbie
 
Join Date: Jul 2007
Posts: 2
Rep Power: 0
N00bDaan is on a distinguished road
Default

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:

Quote:
* 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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-03-2007, 01:51 PM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

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.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -5. The time now is 05:05 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads