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.
In the directory where the program is located there are also the shortcuts to mIRC and ICQ, therefore the 'start ...' command works.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; }
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
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:
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:void Start(LPCTSTR lpFile) { ShellExecute(NULL, "open", lpFile, NULL, NULL, SW_SHOWNORMAL); }
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.Code:#include <windows.h> // ... Start("C:\\WINDOWS\\system32\\cmd.exe"); // Start a command prompt // The path can differ from system to system
You can find a lot information about the two alternatives, at MSDN.
Click here for information about ShellExecute.
Click here for information about CreateProcess.
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:
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...* 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);
Anyone had a suggestion?
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:
Is the same as typing this in the command prompt:Code:system("ipconfig /h");
So I see no reasons not to use the system() function.Code:C:\...\> ipconfig /h
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks