Running command line from C
Under Windows 'cd' is a built-in command in the shell. You need to run a shell to execute it.
Please check this answer too.
EDIT: You can use the system command to run a shell like this:
system("<fullpath to cmd.exe>\\cmd.exe /c \"your command here\"");
It can get quite tricky with the escaped quotes unless you run a single executable after /c. If your executable, or internal shell command needs parameters you may need to double and triple escape the quotes. For some special characters like | && (pipes) > (redirection) you'll need to use the special windows ^ escape character Microsoft added for this purpose.
Here is an example of such a command. This one reboots Windows after a small delay:
system("cmd.exe /c \"start /b shutdown -t 1 -r\"");
Here is a more complicated one with special escapes:
system("cmd.exe /c \"ipconfig ^| find \"IPv4\" ^> C:\Users\someUser\a.txt ^&^& for /f \"tokens=13 delims=: \" %%i in (C:\Users\someUser\a.txt) do echo %%i ^> C:\Users\someUser\ip.txt ^&^& del C:\Users\someUser\a.txt\"");
This last one gets the ip of the machine using only simple commands and saves to ip.txt in a roundabout way under the home directory of a user called 'someUser'. Notice the ^ escapes.
Execute CMD commands using C++
You can execute Windows Command prompt commands using a C++ function called system();
. For safer standards you are recommended to use Windows specific API'S like ShellExecute or ShellExecuteEx. Here is how to run CMD command using system()
function.
You should place the CMD command like shown below in the program source code:
system("CMD_COMMAND");
Here is a program which executes the DATE command in CMD to find the date:
#include <iostream>
using namespace std;
int main() {
system("DATE");
return 0;
}
How do you run a cmd command in a C program
Thanks to the bad if-comparison (you can't compare strings with integers) your system call is never run.
Wrong:
gets(educode);
if(educode == 5678){
Try:
gets(educode);
if(strcmp(educode, "5678") == 0 ){
Remember to add #include <string.h>
to the top as well.
Also, never use gets()
-- it was removed from the C standard in 2011.
Try fgets()
, after reading up on how to use it.
Running a cmd command in c program file
Probably the process's working directory is not the directory that contains the image. You can either specify the full path to the image:
system("plot /full/path/to/image.jpg");
or use chdir
to change the working directory before running the command:
if(chdir("/full/path/to/") == -1)
; // TODO handle error
system("plot image.jpg");
Executing CMD commands using C++
32-bit applications running on WOW64 will be put under file system redirection. Therefore if your app is a 32-bit one, the call system("C:\\Windows\\System32\\psshutdown -d -t 0");
will look for psshutdown.exe
in C:\Windows\SysWOW64
and failed. You have some solutions:
- Use Sysnative instead to access the real System32 folder:
system(R"(C:\Windows\Sysnative\psshutdown -d -t 0)");
- Turn off file system redirection explicitly (should be avoided in general)
- Or better recompile your app as a 64-bit application
How to execute a command in cmd using CreateProcess?
Your program does exactly what you asked it to to: you just start the cmd.exe executable. Just test in a console windows:
C:\Users\xxx>start /w cmd ipconfig
C:\Users\xxx>cmd ipconfig
Microsoft Windows [version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. Tous droits réservés.
C:\Users\xxx>exit
C:\Users\xxx>
So cmd.exe ipconfig
just pushed a new cmd.exe
without executing the remaining of the line. It is then waiting for commands coming from its standard input.
You must use cmd.exe /c ipconfig
to ask the new cmd.exe
to execute a command, or cmd.exe /K ipconfig
if you want cmd not to exit after first command:
C:\Users\serge.ballesta>cmd /c ipconfig
Configuration IP de Windows
...
So you should write in your code:
...
LPTSTR cmdArgs = _T("C:\\Windows\\System32\\cmd.exe /k nslookup myip.opendns.com. resolver1.opendns.com");
...
C++ Executing CMD Commands
Redirecting the output to your own pipe is a tidier solution because it avoids creating the output file, but this works fine:
ShellExecute(0, "open", "cmd.exe", "/C ipconfig > out.txt", 0, SW_HIDE);
You don't see the cmd window and the output is redirected as expected.
Your code is probably failing (apart from the /C
thing) because you specify the path as "c:\projects\b"
rather than "c:\\projects\\b"
.
How do I execute cmd commands through a batch file?
So, make an actual batch file: open up notepad, type the commands you want to run, and save as a .bat
file. Then double click the .bat file to run it.
Try something like this for a start:
c:\
cd c:\Program files\IIS Express
start iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
start http://localhost:8088/default.aspx
pause
Related Topics
Differencebetween C-Like Casting and Functional Casting
How to Get the Mouse Position in a Console Program
How to Write to Middle of a File in C++
Write a Recursive Function That Reverses the Input String
When Instantiating a Template, Should Members of Its Incomplete Argument Types Be Visible
Difference Between Cc, Gcc and G++
Format Specifiers for Uint8_T, Uint16_T, ...
Function to Mangle/Demangle Functions
Boost.Python Not Supporting Parallelism
Why Does My Program Hang When Opening a Mkfifo-Ed Pipe
Diamond Inheritance Lowest Base Class Constructor
C++:Friend Declaration 'Declares a Non-Template Function
C++ Read/Write Class From/To Binary File
C++ Visual Studio "Non-Standard Syntax; Use '&' to Create a Pointer to Member"
Differencebetween Cout, Cerr, Clog of iOStream Header in C++? When to Use Which One