Execute CMD commands using C++ - c++

In my project I want to execute some CMD commands. What is the syntax for doing that 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;
}

Use Windows specific APIs:
ShellExecute or ShellExecuteEx
CreateProcess
See this also.

I suppose you could always do:
#include <iostream>
#include <windows.h>
using namespace
int main()
{
WinExec("cmd", 1);
return 0;
}
This however, automatically sets the path to the folder your file is in. Just type cd\ to return to base file.

Related

Getting Environment Variable $PATH in Linux using C++ in Eclipse

I am trying to get the value of environment variable $PATH in Linux using a simple C++program as bellow:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main ()
{
// get PATH using pipe
FILE* pip = popen("exec bash -c 'echo $PATH'", "r");
if (!pip)
{
printf("can not open pipe!");
return 1;
}
char lineversion[600];
memset (lineversion, 0, sizeof(lineversion));
if (!fgets(lineversion, sizeof(lineversion), pip))
{
printf("fgets error!");
return 1;
}
std::cout << lineversion << std::endl;
// get PATH using getenv
char* pPath = getenv ("PATH");
std::cout << pPath << std::endl;
}
I used two different methods: using pipe and using getenv method. They both output this:
/opt/texbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/netbeans-7.3.1/bin
Interestingly the actual value of PATH is something different!
Why my C++ program is showing a different value for PATH?
Edit 1: I am running my C++ program in Eclipse IDE.
Edit 2: Compiling the program directly (without Eclipse IDE) shows the correct PATH value!
Edit 3: I found the answer in here too.
A process inherits the environment from the process that created it.
This is how Linux works, along with many other operating systems.
If you launch a program from Eclipse, that program inherits Eclipse's environment.
If you launch a program from the shell, that program inherits the shell's environment, including the modifications to PATH you have in your init files.
Since Eclipse has inherited its environment from whichever process launched it, you should see the expected output if you launch Eclipse from the shell instead of through your desktop GUI.

How can I get console output in code block IDE

My code has everything ok! But I don't get any output on the console.
This is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
return 0;
}
Click on build->run or hit Ctrl+F10 and a new CMD Window should pop up, showing you your "Hello world!".
I think Anti-virus might be causing a problem for you. Try excluding the folder that contains your file. There was another question posted with same problem and for that excluding the folder worked. Code::Blocks console app won't show output
I experienced this same problem on linux; installing xterm solved the problem for me.
See Settings > Environment > General Settings > Terminal to launch console programs
Please include getchar() in the function before return statement. This happens because the computer is executing your program and doesn't wait for you to see the output. Including getchar(), at the end mandates it to wait for an input & in the meanwhile you can observe your output
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
getchar();
return 0;
}
You can use the following command to use linux terminal, and it is tested by me.
gnome-terminal --geometry=80x20+300+240 -x

Named Pipes with Command Prompt

Is it possible to pipe to a named pipe using the command prompt
dir >"\\.\pipe\my_named_pipe"
my_named_pipe being a pipe created by a win32 application
#include <windows.h>
#include <iostream>
int main()
{
HANDLE pipe= CreateNamedPipe("\\\\.\\pipe\\my_named_pipe",PIPE_ACCESS_INBOUND,PIPE_TYPE_BYTE,1,500,500,NMPWAIT_USE_DEFAULT_WAIT,NULL);
char* buf = new char[501];
ReadFile(pipe,buf,500,NULL);
std::cout << buf << std::endl;
}
I tried on windows XP
dir >\\.\pipe\my_named_pipe
and it worked properly.
According to Wikipedia http://en.wikipedia.org/wiki/Named_pipe the answer is no for Windows and yes for Unix. But Windows can run Unix so it depends if you want to run Unix Services. See for more info http://en.wikipedia.org/wiki/Windows_Services_for_UNIX

C++ system() not giving output of dsget command

This is weird... Am on Windows 7 in a domain environment, doing a quick program to process a bunch of data [so no lecturing me on the evil of using system() =P ], and I've got this code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
system("dsget group \"CN=Accounting,OU=Groups,OU=Exchange Users,DC=MyDomain,DC=com\" -members");
}
The output is nothing at all. Stuff I've tried:
Ran that command verbatim on a command prompt, successfully got data back.
Did a "cout" on that command string to make sure the \" part was being processed right.
Did system("ipconfig -all") and other system commands to make sure that was working.
Did system("echo [dsget_command] > runThis.cmd") then system("runThis.cmd")... the only output was seeing it try to execute the dsget statement but still no results.
It's so weird that this is just happening to me on the "dsget" command, and also so weird that the command runs fine on a command prompt [not through the C++ program]. Any suggestions?

Batch file launched in an exe doesn't work

Ok so I have an executable which launches my batch file and the batch file then should create an ODBC data source to my SQL server. If I run the batch file by itself it runs perfectly and creates the data source, but when I run it through my exe it runs, returns no errors but doesn't create the data source.
my entire exe code is
#include <windows.h>
#include <iostream>
#include <dos.h>
#include <conio.h>
using namespace std;
int main ( int argc, char *argv[] )
{
cout << system("find2.bat");
system("PAUSE");
return 0;
}
and my batch command is
ODBCCONF.exe CONFIGSYSDSN "SQL Server" "DSN=test_DSN | Description=test Data Source | SERVER=(local) | Trusted_Connection=Yes"
I also tried to output any errors from the batch file but it just created a blank text file so I guess no errors.
So does anybody have any ideas what could be causing this?
Are you sure the batch file is running? Since you're not building an absolute path, I would guess it's not being found.
You need to modify the call according to this:
system("cmd /c C:\\path\\to\\find2.bat");