Writing to Console from DLL - c++

I have a DLL for a program, and want to be able to run that program in a command line, and then pipe the output of my DLL to another program. How do I do this?
I can currently opening a new debug console to print to, but I want to be able to pipe the output so that I can run a shortcut like:
C:\Windows\System32\cmd.exe /K "C:\Program_Using_the_DLL.exe | C:\Program_to_Pipe_To.exe"
This is different from some similar questions (IE: this one) in that I need to be able to pipe the output, so I need it to come to the current console.
Perhaps I can use GetStdHandle() somehow, or is there someway of using AttachConsole(ATTACH_PARENT_PROCESS)? Any help would be appreciated! I'm new to this nitty-gritty part.
(DLL is written in C++)

Try using the function GetStdHandle(STD_OUTPUT_HANDLE) to get a HANDLE to the current output file.

Related

How do i make a C++ program open multiple CMD Prompts

This is kind of an odd question but does anyone know how to make a c++ program open multiple separate CMD prompts? they don't even have to be proper command prompts they can spit out text in a separate CMD like window that's fine. they don't need to talk to each other or do anything more than display text. the only reason I'm not doing this in Batch is because it needs to be a compiled EXE. I know I can run windows commands in c++ with "system(...)" however I'm not sure how to force these to be separate windows. if anyone knows a way to do that I would greatly appreciate it.
Thank you for your time
You can use CreateProcess to rum cmd,and communicate through pipe.
Your question is not clear to me, but let me try:
A single process can have no or exactly one console window. E.g. in Visual Studio, in the linker settings, you can set the subsystem to console, then the console window is opened automatically or you can use AllocConsole to get one. If you need more windows to display text, you have to create these windows by yourself (these windows aren't normal console windows). Another possibility is to start more backgroud processes, each having its own console window to display the text. To start these background processes, use CreateProcess and use for dwCreationFlags CREATE_NEW_CONSOLE.
If you have a batch file and want to display multiple console windows, use the start command to start your console program to create a new console and disply its output there, e.g.
start cmd.exe /k dir

Display matlab's program's output calling for another c++ program

I'm not sure if my question is clear:
I have a C++ project that I run from Qtcreator, this program call another c++ script outside the project. This last one runs a shell script calling a bunch of matlab scripts.
I want to display output from the matlab scripts, disp doesn't work.
I tried to write the values I want to read in a .txt file. The file is created but stay empty.
I tried these lines to write in a file:
fileId= fopen('imagename.txt','a');
fprintf(fileId, 'test : %s',imageName);
fclose(fileId);
I also tried assignin with the values I want to show but they're not kept within matlab's workspace.
I can't change the architecture of the whole program because it's a big project made by someone else I have to continue.
Do you have another way to watch what is going on the matlab scripts?
It's difficult to launch them directly from Matlab as I don't have access to their inputs values. I can hardly change the c++ script calling them to display theses values because I have a 'reference to ofstream is ambiguous' issue when I try to build it, so I will have to debug something made by someone else and non commented.
The fact that the file you write to stays empty is weird and I would try to find the reason regardless of your question.
Anyway, since you call Matlab from a shell script I guess you use 'matlab -r' option with your script name, or something similar. In that case you can use the -f option which tells Matlab to write the command line output to a log file:
matlab -logfile output.log

How do i output text to a printer in c++

I am new to programming and seem to be having trouble figuring out how to direct output to a printer.
cout directs to monitor, is there a command to direct to the print queue?
I have a simple console program and am using code blocks for my ide.
A problem like this is dependent on your operating system. Assuming you use windows, you may need to look at the 'Windows API' which may or may not have a method of doing this. Alternatively, you can output to a text file, then use a script to open the file, run through the print menu, and close the file which is less eloquent but will work.

How can I use and launch an exe file inside my c++ program?

I want to use an .exe file inside my c++ program. I reviewed and checked these functions (system - ShellExecute - CreateProcess) to achieve this goal, but I found them useless because I need the output of that .exe file inside my program for further processes.
CreateProcess can be used to pipe the output from the created program back to the creator. MSDN even has sample code to do exactly this: Creating a Child Process with Redirected Input and Output
I think you do want CreatProcess. You can get the STDIN / STDOUT:
http://msdn.microsoft.com/en-us/library/ms682499%28v=vs.85%29.aspx
you should use the "Process.standardOutput" to read the result of an .exe.
Here is the link on MSDN, there is plenty of explainations:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.71).aspx
You could use one of those functions to execute your file and have it write the output to another file. Then you just have to read that into your program.

Is it possible to get the output of a program while it's running?

If I have a windows console program written with c++, is it possible to retrieve that program's standard output, while the program is running? And if not, what would be the best way to rewrite the program? I know I could output to files and continuously check those files for updates. Is there another way? Is there a better way?
There are some interesting articles in Code Project:
CommandLineHelper (C#)
Redirecting an arbitrary Console's Input/Output (MFC/C++)
Universal Console Redirector (MFC/C++)
Yes, if you start the program yourself:
in CreateProcess, you pass a STARTUPINFO where you can specify handles for SDIN, STDOUT and STDERR. Note that oyu need to supply all three once you specify the STARTF_USESTDHANDLES flag.
Also, the handles need to be inheritable (otherwise, the child process can't access them), so the SECURITY_ATTRIBUTES basically need to look at least like this:
SECURITY_ATTRIBUTES secattr = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
You could open handles to disk files that contain input and receive output. Alternatively, this can be Pipes that can be read / written incrementally while the console app is running.
If it is a ready console executable
you can allways redirect it output in a file like this:
c:> echo Some text > file
or
c:> program > file
If you mean this?
As your question is not exactly clear.
\\ into another program
Oh, Ok
But my first answer is also used to it.
As there is also another possibility like:
c:> program1 | program2
its make a "pipe" between console programs
program2 receive on it stdin what program1 throws to stdout
Its common old-aged Unix-way practice in console programs.
And in such way NO need to rewrite programs to specifically support it.
If you are only interested in the program's stdout, popen() makes this pretty simple:
FILE* program_output = popen("command line to start the other program");
//read from program_output as you would read a "normal" file
//...
pclose(program_output);
You'd most likely need to use pipes to achieve this, and since you're using Windows, here's a link to MSDN article with an example that seems to do exactly what you wanted.