- Execute CMD commands using C++
- 3 Answers 3
- Not the answer you’re looking for? Browse other questions tagged c++ windows cmd command or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Batch command/script to do commands from .txt file?
- 3 Answers 3
- I’m trying to find a command or a script that can run commands from a .txt file
- Further Reading
- Run a Text File as a Batch Script
- Batch Script Logic
- Text File
- Execute this by using either. . .
- 1.Drag and Drop
- 2. Command Line
- @ECHO OFF Results
- Further Resources
- How do I execute a *.dll file [closed]
- 9 Answers 9
- Creating batch file to start cygwin and execute specific command
- 2 Answers 2
- Optional reading: interacting with a mintty.exe window:
- How to execute a batch file from C#?
- 7 Answers 7
- B:\foo.cmd:
- Program.cs:
Execute CMD commands using C++
In my project I want to execute some CMD commands. What is the syntax for doing that using C++.
3 Answers 3
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:
Here is a program which executes the DATE command in CMD to find the date:
Use Windows specific APIs:
I suppose you could always do:
This however, automatically sets the path to the folder your file is in. Just type cd\ to return to base file.
Not the answer you’re looking for? Browse other questions tagged c++ windows cmd command or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Batch command/script to do commands from .txt file?
Im trying to find a command or a script that can do commands from a specified .txt file. e.g it would treat a .txt file as a batch file but not necessarily rename it to .bat.
Can anyone help?
That would run filename.txt as a .bat file and do any commands in it.
3 Answers 3
I’m trying to find a command or a script that can run commands from a .txt file
The simplest solutions are always the best.
Use the following command:
cmd goto , setlocal and others. However, multi-line nested if and for commands do work as long as for replaceable parameters use just one percent (like in the command-line).
See StackOverflow answer How to run batch script without using *.bat extension, also by by Aacini, for more observations on this approach. This answer also includes a link to code that can get goto and call working as well!
Further Reading
- An A-Z Index of the Windows CMD command line
- A categorized list of Windows CMD commands
- redirection — Redirection operators.
- type — Display the contents of one or more text files.
Run a Text File as a Batch Script
Yes this is possible by making the text file which contains the commands you need executed to be referenced as the first batch argument that is passed to the batch script.
You can set the argument value as a variable and using type you can create a temporary batch file with the content of the text file and then run that file through cmd to process accordingly.
Batch Script Logic
Note: You can use cmd /c to have the command window(s) close after execution.
Text File
Note: This is the text file without a .bat or .cmd which each line will contain a valid batch command which will be executed.
Execute this by using either. . .
1.Drag and Drop
2. Command Line
@ECHO OFF Results
Further Resources
%* in a batch script refers to all the arguments (e.g. %1 %2 %3 %4 %5 . %255) only arguments %1 to %9 can be referenced by number.
In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:
How do I execute a *.dll file [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago .
I have a DLL file and I want to execute it on Windows. I obtained this DLL file from a challenge site which alleges the DLL should be executed independently.
9 Answers 9
To run the functions in a DLL, first find out what those functions are using any PE (Portable Executable) analysis program (e.g. Dependency Walker). Then use RUNDLL32.EXE with this syntax:
dllname is the path and name of your dll file, entrypoint is the function name, and optional arguments are the function arguments
You can execute a function defined in a DLL file by using the rundll command. You can explore the functions available by using Dependency Walker.
While many people have pointed out that you can’t execute dlls directly and should use rundll32.exe to execute exported functions instead, here is a screenshot of an actual dll file running just like an executable:
While you cannot run dll files directly, I suspect it is possible to run them from another process using a WinAPI function CreateProcess:
DLLs are shared libraries which are used by other windows programs while EXEs are the files which are actually executed and are linked to DLL files so that they can use DLLs.
Both are of same format, PE(portable executable or format of machine code in windows in simple words).
In other words EXEs contain the entry point(main) and the DLLs contain the library functions.. You cannot execute a file which just contains library functions you can just use them via other programs.
But still there are programs like rundll32.exe which provides that entry point and some minimal framework required by DLL functions to be called.
The point that I want to make is, you can never execute a DLL file you can just use it’s code by providing an entry point through an EXE or some other program.
Creating batch file to start cygwin and execute specific command
I want to create a batch file thats start cygwin and executes a specific command (The command is to read a bash file and executes some command inside it).
This is the batch file that I have developed, it works to some extent. the cygwin terminal opens and tries to read the bash file, but cannot execute the commands inside:
How can I make this work?
2 Answers 2
From your batch file, launch Cygwin’s bash shell and use the login flag. This provides a foundation for setting path and environment variables through your .bash_profile or .bashrc files. I believe this may be the source of your difficulties.
If you provide more details about the nature of your bash file, I can be more specific. Good luck.
Patrick Kelly’s helpful answer points out that not running bash as a login shell in Cygwin may be the source of the problem.
Indeed: Always run Cygwin bash as a login shell, because crucial initializations will otherwise not take place; notably, the $PATH variable won’t contain /usr/local/bin:/usr/bin , and the locale won’t be set correctly.
In order to make bash start a login shell, the bash executable must be passed the -l option (or its longer alias, —login ).
To execute your script:
in the current console window, see Patrick’s answer.
Note that unless the target script relies on a particular working directory, use of -c with separate cd and execution commands is not strictly necessary; something like the following will do:
in mintty.exe , the terminal application that Cygwin comes with (as you’ve tried):
- Note that executing mintty from a Command Prompt or batch file invariably opens a new console window, and does so asynchronously.
Note how a Windows-style path can (alternatively) be used to specify the target script (which means you could have used c:\cygwin64\bin\bash instead of /bin/bash too).
More importantly, note how an explicit reference to the bash executable is required in both cases, which contrasts with what you tried:
Options you pass to mintty.exe are specific to it — they’re not passed through to bash . Specifically, mintty.exe options you’re using are (see all of them by running mintty —help ):
- -l, —log FILE|- . Log output to file or stdout
- -i, —icon FILE[,IX] . Load window icon from file, optionally with index
Therefore, -li creates a log file named i in the current directory, because i is interpreted as the l ‘s option-argument, not as separate option i .
The specified script ( /cygdrive/c/. ) still executes, however — but, crucially, not in a login shell.
What you meant to do requires an explicit call to the bash executable, as demonstrated above, so that the options are interpreted by Bash:
Also, as noted, executing mintty.exe from a batch file invariably creates a new console window, and does so asynchronously — that is, your batch file immediately proceeds to the PAUSE command, irrespective of whether the mintty.exe process has terminated yet or not.
Obviously, whatever is output in that new window will not be visible in the original window.
Optional reading: interacting with a mintty.exe window:
Caveat: mintty.exe runs asynchronously even when using the usual start /wait approach from a batch file; that is, the following attempt to block a batch file / command prompt until mintty.exe terminates does not work:
When passed a script / command, the Bash session created by mintty.exe will invariably exit when the script terminates, because bash — even when it’s passed -i to indicate an interactive session — will automatically exit in that case.
If all you need is to inspect the output after the script terminates, run:
Note that no shell is running in that window once the script exits, so all you can do is inspect the script’s output — nothing more.
To keep a shell open, you need a workaround:
Note that this creates a new shell instance after the script exits.
How to execute a batch file from C#?
(See end for solution)
I didn’t think this was going to be hard. I have a commmand file, d:\a.cmd which contains:
But these lines of C# won’t execute it:
Throws Win32Exception: «%1 is not a valid Win32 application.»
Process.Start opens .pdf files. why not execute command files?
This works if I type it in a cmd window:
Windows XP, MS Visual Studio 2008.
Thanks in advance, Jim
SOLUTION I’m only SLIGHTLY embarrassed 🙁 There was a file named cmd.exe, size zero in my app’s dir. I have no idea how it got there but it is now toast and both of the above C# statements now work. I’m off to find a Harry Potter book so I can get some self-punishment ideas from Dobby.
7 Answers 7
I’ve got four things for you that you can try out:
(1) Try providing the full path for cmd.exe (e.g. on my machine: C:\WINDOWS\SYSTEM32\CMD.EXE ).
(2) Try adding call to the command to be executed:
(3) Besides that, I can only guess where the %1 in the Win32Exception is coming from. Maybe your file associations are set-up incorrectly.
If you type the following on the command-line:
You will likely get a mention of cmdfile . If you then look up this token with:
You might get an answer along the lines of:
Those settings are stored in the registry, and this is how the command-line interpreter knows how to execute files with custom extensions. (You can find out how a PDF document is started by executing the above two statements for the .pdf extension.)
(4) If you start to suspect that your machine might be mis-configured, start regedit (the registry editor) and locate the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor .
On my Windows XP machine (and your Process.Start example works on my machine, with different filenames though), I’ve got the following values stored in there:
Of those, the AutoRun value might be of some interest. I think it corresponds to the /d command-line switch of cmd.exe , which controls whether cmd.exe attempts to start files with custom extensions. Usually, this is enabled. Maybe, on your machine, it isn’t?
Or you can do a .bat file, then call this file through System.Diagnostics.Process.Start() . It won’t redirect output to Console Application, but it would certainly execute the commands inside.
You need to specify the process full name (cmd.exe).
You should try
So you can be sure to execute the right file even if a cmd.exe is in your applications directory.
It looks like something wrong with your computer. Try running this on another machine. This should work. Process.Start(string) uses ShellExecuteEx to launch the file, so it’s pretty much the same thing as double-clicking the file in Explorer, as you supposed.
A simple test worked for me.
B:\foo.cmd:
Program.cs:
This works as expected.
Your error message is suspicious, «%1 is not a valid Win32 application.» The value in my registry at HKCR\cmdfile\shell\open\command is
The %1 gets replaced by the file name, and the %* can be ignored here (it indicates that any further command-line arguments should be passed along, but we’re not concerned with that right now).
The fact that the file itself is launched to handle this type of file indicates that Windows itself knows how to launch this type of file. On a normal installation of Windows, the following extensions should be set up similarly:
- .exe Windows and DOS executable files
- .com DOS «command» files
- .bat Windows and DOS batch files
- .cmd Windows NT batch files
- .pif Windows shortcuts to DOS executable files
If you go to HKCR\.xxx (where xxx is any of the above), the «(Default)» value should be xxxfile . If you then go to HKCR\xxxfile\shell\open\command , the «(Default)» value should be «%1» %* . Also the «(Default)» value of HKCR\xxxfile\shell should be either not set or open .
If you have anything else in any of these values, then some program has attempted to insert itself into the execution process. Viruses sometimes do this (Sircam, for example).