|
Hi all,
i want to execute a programm (f.e. ipconfig.exe) with CreateProcess and redirect the output to a char* if possible. The question is: How could this be done? As i am new to CPP, i haven't got a clue...
Can anybody give me a hint please?
Thanks in advance!
-- modified at 10:08 Wednesday 14th June, 2006
|
|
|
|
|
nmx_de wrote: i want to execute a programm (f.e. ipconfig.exe) with CreateProcess
ok
nmx_de wrote: redirect the output to a char* if possible
Do you want to redirect the settings done in ipconfig.exe ???
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
A_LaxmanDo you want to redirect the settings done in ipconfig.exe ???
The problem has nothing to do with ipconfig.exe itself. It was only an example.
I want to be able to call any program (like ipconfig.exe or anything else that returns text) from my program and receive the result.
Thanks for the fast reply
|
|
|
|
|
|
I think it's not what is looking for. He start a console program (like ipconfig.exe) and would like to retrieve the text outputted in the console (so it has nothing to do with returning data).
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
Cedric Moonen wrote: He start a console program (like ipconfig.exe) and would like to retrieve the text outputted in the console
Jep! This is exactly what i want...
|
|
|
|
|
nmx_de wrote: He start a console program (like ipconfig.exe) and would like to retrieve the text outputted in the console
Try myexe.exe > filename.txt in the ShellExecute which gives the output generated by the myexe.exe into filename.txt .
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
nmx_de wrote: Can anybody give me a hint please?
Hint 1
Hint 2
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
|
In the first line i want to thank you all for your help.
I managed to get it done like this, but i have some problems using it in another application.
When i put this code to my class and try to run a command from another function, the whole application freezes. Ok, i know that the code looks horrible, but it works a a single application.
Anybody out there with some tipps & tricks regarding this problem?
How could it be done as a class member without freezing the whole app?
Thanks, n.
<br />
#include "stdafx.h"<br />
#include <windows.h><br />
<br />
char* cmd(char *cmd);<br />
<br />
int main()<br />
{<br />
char* output = cmd("ipconfig.exe");<br />
MessageBox(0,output,"Test",0);<br />
return 0;<br />
}<br />
<br />
char* cmd(char *cmd)<br />
{<br />
HANDLE readHandle;<br />
HANDLE writeHandle;<br />
HANDLE stdHandle;<br />
DWORD bytesRead;<br />
char maxbuffer[1024];<br />
<br />
static char output[50000];<br />
SECURITY_ATTRIBUTES sa;<br />
PROCESS_INFORMATION pi;<br />
STARTUPINFO si;<br />
<br />
ZeroMemory(&sa,sizeof(SECURITY_ATTRIBUTES));<br />
ZeroMemory(&pi,sizeof(PROCESS_INFORMATION));<br />
ZeroMemory(&si,sizeof(STARTUPINFO));<br />
<br />
ZeroMemory(maxbuffer, 1024);<br />
<br />
sa.bInheritHandle = true;<br />
sa.lpSecurityDescriptor = NULL;<br />
sa.nLength = sizeof(SECURITY_ATTRIBUTES);<br />
si.cb = sizeof(STARTUPINFO);<br />
si.dwFlags = STARTF_USESHOWWINDOW;<br />
si.wShowWindow=SW_HIDE;<br />
<br />
if (!CreatePipe(&readHandle,&writeHandle,&sa,NULL))<br />
return "Error";<br />
<br />
stdHandle=GetStdHandle(STD_OUTPUT_HANDLE);<br />
<br />
if (!SetStdHandle(STD_OUTPUT_HANDLE,writeHandle))<br />
return "Error";<br />
if (!CreateProcess(NULL,cmd,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi))<br />
return "Error";<br />
if (!ReadFile(readHandle,output,1023,&bytesRead,NULL))<br />
return "Error";<br />
<br />
output[bytesRead]='\0';<br />
<br />
if (!SetStdHandle(STD_OUTPUT_HANDLE,stdHandle))<br />
return "Error: SetStdHandle(stdHandle) failed";<br />
if (!CloseHandle(readHandle))<br />
return "Error: CloseHandle(stdHandle) failed";<br />
if (!CloseHandle(writeHandle))<br />
return "Error: CloseHandle(stdHandle) failed";<br />
<br />
return output;<br />
}<br />
|
|
|
|
|
I am trying to write on a label.
In the OnEraseBackground function I have
pDC->TextOut(50,42,"aaaa",30);
At this location I get a black rectangle.
What do I need to do to see the text?
thanks,
sb
|
|
|
|
|
I do not know what foreground color, background color, drawing mode you are using, or if you are drawing at the right time because the code sample is missing detail.
But the last parameter you have specified in your example is incorrect - it is supposed to be the character count of the specified string (note that the documentation may be incorrect when it says "byte count"). As such, you should be specifying 4 .
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
wrote:
I am trying to write on a label.
Why you are trying TextOut on label ??
Simply use SetWindowText for that label if using MFC.
For win32 use SendMessage(handle,WM_SETTEXT,...);
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
As you are working with CDC you can simply set the text color using
SetTextColor() API which takes COLORREF as parameter.
The secret of life is not enjoyment
but education through experience.
- Swami Vivekananda.
|
|
|
|
|
Any particular reason why you are writing in the OnEraseBackground() method? Why not do it in the OnPaint() method?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Thats what I saw in a sample program, so I used OnEraseBackground.
I did get TextOut to work , thanks everybody.
I'm drawing on the label because I drew a histogram on it and want to put values for the tick marks on the axes. Maybe there is a smarter control to use instead ofa label?
thanks
sb
|
|
|
|
|
you can use WM_PAINT instead WM_ERASEBKGND
<br />
void CAnswer::OnPaint()<br />
{<br />
CPaintDC dc(this);<br />
<br />
dc.SetBkMode(0);<br />
dc.TextOut(50,42,"aaaa",30);<br />
}<br />
whitesky
|
|
|
|
|
Hi,
I tried several things to launch an external program :
<br />
CString command("\"D:\\USER\\PSPad editor\\PSPad.exe\" \"log.txt\"");<br />
system(command);<br />
The above code launches a command prompt that closes instantly and doesn't launch PSPad.
<br />
CString command("cmd /C \"D:\\USER\\PSPad editor\\PSPad.exe\" \"log.txt\"");<br />
system(command);<br />
The above code launches a command prompt that launches PSPad but the calling dialog looses focus until I close PSPad.
Actually I'd like to launch my program just like double clicking on "log.txt" would do (without a command prompt would be great). Is it possible ?
Thanks !
|
|
|
|
|
Tnarol wrote: I'd like to launch my program just like double clicking on "log.txt" would do (without a command prompt would be great). Is it possible ?
Use ShellExecute(...) or CreateProcess(...) API's for launching external application's
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
Tnarol wrote: Is it possible ?
yes
use ShellExecute
nave
|
|
|
|
|
|
|
COOL ! It works great !
Thanks everyone.
|
|
|
|
|
And you suggested this the other day!!!
Your previous post[^]
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|