Named Pipes with Command Prompt - c++

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

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.

Execute CMD commands using 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.

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

How can I launch Mozilla Firefox using C++?

Compiled through compiler like Code::Blocks, I have tried the following, bit it does not work:
/*Running Firefox.exe*/
#include <stdio.h>
/*using c++*/
#include <iostream>
#include <stdlib.h>
using namespace std;
int main ()
{
int x;
cout << "Checking if processor is available..." << endl;
/*System used here*/
if (system(NULL)) puts ("Proceed");
else exit (1);
cout<< "Executing Firefox..." << endl;
/*Having some error here saying not recognized as internal or external command*/
x = system ("C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
/*cout here*/
cout <<"The value returned was:" << x << endl;
return 0;
}
Is it because Firefox is not recognized as a system of windows? If so how can I run Firefox, or even Internet Explorer from code?
Run cmd.exe (Windows command shell) and enter the string C:/Program Files (x86)/Mozilla Firefox/firefox.exe at the command line, and you will see the same problem - i.e. the problem is with your command string rather than your C++ code.
The space in the path requires the command string to be quoted:
system ("\"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe\"") ;
or
system ("\"C:/Program Files (x86)/Mozilla Firefox/firefox.exe\"") ;
I believe you need to convert path to DOS format
Launch a command prompt cd into Mozilla Firefox folder
run this: for %I in (.) do echo %~sI
Copy the output to system command with \ replaced with \\ append firefox.exe at end
Is it because Firefox is not recognized as a system of windows?
If you open up cmd.exe and type in C:/Program Files... it won't work because spaces are used as a delimiter. Quote your path:
system("\"C:/Program Files (x86)/Mozilla Firefox/firefox.exe\"");
Although if you're targeting Windows you should consider using CreateProcess which saves you this trouble.
If so how can I run Firefox, or even Internet Explorer from code?
If you want to show a web page, use ShellExecute* and let the shell do the work. It will take care of launching Firefox, Internet Explorer, Chrome, or whatever browser the user has configured to view web pages.
*Read the Remarks section about initializing COM, first.
Try using windows API CreateProcess API

QtCreator returns error "Cannot change to working directory"

I'm trying to running a simple HelloWorld program with Qt Creator
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
When running occurs the error
Cannot change to working directory '/home/myusername/build-myproject-Desktop-Debug': file or directory not found
Many tutorials in the internet tell me to disable "Run in terminal", but I need this for my project.
My working directory is /home/myusername/build-myproject-Desktop-Debug
Any help?
Which OS are you running?
I'm on Linux and had the same problem. My solution was to use xterm and set the default Terminal in Qtcreator to xterm (In the menu: Tools, then Options, then Environment, then Terminal, there "/usr/bin/xterm -e")
You can install Terminator terminal (sudo apt-get install terminator) and use it as Qt Creator terminal:
Tools->Options->Enviroment->System->Terminal->"/usr/bin/terminator -x"