Discussion:
how does user communicate with process created with createprocess api
(too old to reply)
chano
2014-10-21 06:24:55 UTC
Permalink
how does user communicate with process created with createprocess api?
Like user input or output to user if process is console program?
is there seperate api for communication with process?
or is there even a way to communicate user with console program?
like user input goes to string array and output from console program
stored to string array? so I can parser the data then do something about
it?
can someone post an example?
Deanna Earley
2014-10-21 08:54:28 UTC
Permalink
Post by chano
how does user communicate with process created with createprocess api?
Like user input or output to user if process is console program?
is there seperate api for communication with process?
or is there even a way to communicate user with console program?
like user input goes to string array and output from console program
stored to string array? so I can parser the data then do something about
it?
It depends on the program and what you want to do.
If it's a command line app, you can pass alternative input, output and
error pipes that allow you to send and receive its output.
If it's a GUI app, then you'll need to look at the automation API, or
agree with the author on a form of IPC, be it pipes, messages, events,
DDE, etc.
--
Deanna Earley (***@icode.co.uk)
iCatcher Development Team
http://www.icode.co.uk/icatcher/

iCode Systems

(Replies direct to my email address will be printed, shredded then fed
to the rats. Please reply to the group.)
chano
2014-10-21 11:03:34 UTC
Permalink
yes I mean the program is console based program.
and that's exactly what I want. redirect stdin, stdout, stderr.
and user communicating with process called with createprocess.
and MSDN example is somewhat inadequate.
I need more comments on the code. and more detailed examples.
several of them.
Deanna Earley
2014-10-21 12:03:41 UTC
Permalink
Post by chano
yes I mean the program is console based program.
and that's exactly what I want. redirect stdin, stdout, stderr.
and user communicating with process called with createprocess.
and MSDN example is somewhat inadequate.
I need more comments on the code. and more detailed examples.
several of them.
Which bit is inadaquate? This looks fairly extensive to me.
http://msdn.microsoft.com/en-gb/library/windows/desktop/ms682499(v=vs.85).aspx

What bit are you stuck on?
--
Deanna Earley (***@icode.co.uk)
iCatcher Development Team
http://www.icode.co.uk/icatcher/

iCode Systems

(Replies direct to my email address will be printed, shredded then fed
to the rats. Please reply to the group.)
chano
2014-10-21 20:17:24 UTC
Permalink
Post by Deanna Earley
Which bit is inadaquate? This looks fairly extensive to me.
http://msdn.microsoft.com/en-gb/library/windows/desktop/ms682499
(v=vs.85).aspx
First it's c++. some header is not available in c. second I copied and
pasted example code. edited little bit. as I edit I understand what the
statement does. and it compiles, then program crashes when I run.
investigated, it crashes somewhere after creating file. and no clue why
it crashes. and or I'm missing something that I didn't exactly get what
the example does like it redirects stdin from a file by opening it then
redirect stderr and stdout to screen? no clear explanations
chano
2014-10-21 20:44:02 UTC
Permalink
Ok I was missing a statement. that's why the program didn't run.
but now when I run the program I don't get expected result.

Do you want to see the code?
R.Wieser
2014-10-22 08:38:17 UTC
Permalink
Chano,
Post by chano
First it's c++.
So ? I'm not using any C language variant, but still used the example as a
base/guideline for my own code. That didn't work in the first try either.
:-|
Post by chano
it crashes somewhere after creating file. and no clue why
it crashes.
That means you need to take a closer look at the result values from the
different functions. And by the way: You're not *creating* that file, but
just opening it for reading (its contents are send to the started programs
STDIN by way of the created pipes).

You could even remove that file opening and the subsequent calling of
WriteToPipe (transferring the files contents to the child processes STDIN).
Post by chano
like it redirects stdin from a file by opening it then redirect stderr
and stdout to screen?
No. This example sends the read output from the child process to the
standard output of the parent process. It probably aimed at development
environments, where the parent process is started from within one, and its
output displayed in the "debug output" window (mostly at) the bottom of it.
(fun fact: a development environment which probably uses the same method as
in the example to connect to the parents-processes in- and output :-) )

Take a look at the ReadFromPipe function: First it reads from the pipe
connected to the child ("ReadFile( g_hChildStd_OUT_Rd,..."), than it writes
what it read to the STDOUT of the current process, the parent
("WriteFile(hParentStdOut, ....").

You can remove the latter writefile and do with the contents of the "chBuf"
whatever you like.

A remark: The WriteToPipe function will not finish untill all of the file is
send to the child, and the the ReadFromPipe function will not exit until the
child process closes.

Another remark: Did you notice that the example opens the input file, but
does not close it ? :-o

Regards,
Rudy Wieser

P.s.
It would be a good idea to mention which language (and version thereof) you
are using ...
Post by chano
Post by Deanna Earley
Which bit is inadaquate? This looks fairly extensive to me.
http://msdn.microsoft.com/en-gb/library/windows/desktop/ms682499
(v=vs.85).aspx
First it's c++. some header is not available in c. second I copied and
pasted example code. edited little bit. as I edit I understand what the
statement does. and it compiles, then program crashes when I run.
investigated, it crashes somewhere after creating file. and no clue why
it crashes. and or I'm missing something that I didn't exactly get what
the example does like it redirects stdin from a file by opening it then
redirect stderr and stdout to screen? no clear explanations
chano
2014-10-22 17:42:50 UTC
Permalink
I'm using c as language with native win32 api

I created file "test.txt" it's contents are some file list in the
directory.

and used it as opening source for stdin.

then I changed cmdline to "dir"

does the example supposed to read file for stdin and send output to
parent's stdout?

when I run the program it holds output until i press ctrl-c
is the example program supposed to do that?
dir program supposedly finishes after listing files in the directory.
why is it hold until i press ctrl-c and force finish the dir program(child
process)?

here is modified example code i used.

#include <windows.h>
#include <stdio.h>

#define BUFSIZE 4096

HANDLE hStd_IN_Rd = NULL;
HANDLE hStd_IN_Wr = NULL;
HANDLE hStd_OUT_Rd = NULL;
HANDLE hStd_OUT_Wr = NULL;
HANDLE hStd_ERR_Rd = NULL;
HANDLE hStd_ERR_Wr = NULL;

HANDLE hInputFile = NULL;

int main(int argc, char *argv[])
{
SECURITY_ATTRIBUTES saAttr;

saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;

CreatePipe(&hStd_OUT_Rd, &hStd_OUT_Wr, &saAttr, 0);
CreatePipe(&hStd_ERR_Rd, &hStd_ERR_Wr, &saAttr, 0);
CreatePipe(&hStd_IN_Rd, &hStd_IN_Wr, &saAttr, 0);


char szCmdLine[] = TEXT("dir");
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;

ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION));
ZeroMemory( &siStartInfo, sizeof(STARTUPINFO));
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = hStd_OUT_Wr;
siStartInfo.hStdOutput = hStd_OUT_Wr;
siStartInfo.hStdInput = hStd_IN_Rd;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;


CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, 0, NULL, NULL,
&siStartInfo, &piProcInfo);

CloseHandle(piProcInfo.hProcess);
CloseHandle(piProcInfo.hThread);

hInputFile = CreateFile( "test.txt", GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);

DWORD dwRead, dwWritten;
CHAR chBuf[BUFSIZE];
BOOL bSuccess = FALSE;

for (;;) {
bSuccess = ReadFile(hInputFile, chBuf, BUFSIZE, &dwRead,
NULL);
if ( ! bSuccess || dwRead == 0 ) break;

bSuccess = WriteFile(hStd_IN_Wr, chBuf, dwRead, &dwWritten, NULL);
if ( ! bSuccess ) break;

}

CloseHandle(hStd_IN_Wr);


bSuccess = FALSE;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

for (;;) {
bSuccess = ReadFile (hStd_OUT_Rd, chBuf, BUFSIZE, &dwRead,
NULL);
if ( ! bSuccess || dwRead == 0 ) break;

bSuccess = WriteFile(hStdOut, chBuf, dwRead, &dwWritten,
NULL);
if ( ! bSuccess ) break;
}

return 0;
}
Geoff
2014-10-23 02:30:24 UTC
Permalink
Post by chano
for (;;) {
Your loops will fail to terminate when ReadFile and WriteFile each
fail to report success.

The example is flawed.

While it's fine for demonstrating how to use certain functions, the
use of an infinite loop and the failure to properly handle the return
value from ReadFile and WriteFile makes it a bad example of how to use
those functions. In real-world code you should never let a FALSE
return code go un-analyzed nor should you depend on that exclusively
to break out of an infinite loop. A realistic program would call
GetLastError and report the nature of the error.

The second example on the link cited by Deanna Early is a C example
even though the block says "C++". It also fails to terminate and must
be terminated with a ctrl-C. Your program has the same defect.

Run your code in a debugger and breakpoint at those if (!bSuccess...
lines and examine why those function calls are failing or replace
those if(!bSuccess lines with GetLastError calls and output the
failure code, then terminate the program with return EXIT_FAILURE;
chano
2014-10-23 06:49:00 UTC
Permalink
I tried with debugger. when out.txt is there, the example program exits
normally. when I delete out.txt the program has to be terminated with
ctrl-c. output of dir program was in out.txt. then I deleted contents of
out.txt and ran the program. it exited normally, but output of dir
program is not stored in out.txt.
why do I have to press ctrl-c to terminate the program when out.txt is
deleted? does anyone know?
Richard Heathfield
2014-10-23 07:10:24 UTC
Permalink
Post by chano
I tried with debugger. when out.txt is there, the example program exits
normally. when I delete out.txt the program has to be terminated with
ctrl-c. output of dir program was in out.txt. then I deleted contents of
out.txt and ran the program. it exited normally, but output of dir
program is not stored in out.txt.
why do I have to press ctrl-c to terminate the program when out.txt is
deleted? does anyone know?
You mean you don't?

Check out those infinite loops. Two of them, if I recall correctly. Under
what conditions do they halt?

Unless those conditions are met, the program will continue until it is
interrupted by a signal, a system-wide crash, or a power failure.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
chano
2014-10-23 21:40:15 UTC
Permalink
Post by Richard Heathfield
You mean you don't?
I really don't
Post by Richard Heathfield
Check out those infinite loops. Two of them, if I recall correctly.
Under what conditions do they halt?
If bSuccess is false, dwRead is 0
Post by Richard Heathfield
Unless those conditions are met, the program will continue until it is
interrupted by a signal, a system-wide crash, or a power failure.
I ran with debugger(gdb) the first loop went on expected. at 2nd loop
after first iteration, when readfile for 2nd time it's just holding there.

dwRead should be 0 and I think bSuccess should be 1 but it's just holding
there. I think it's not loop issue, I think it's the readfile function.
chano
2014-10-31 08:12:37 UTC
Permalink
Now I get it. On the comments at the bottom of the page, it says the
example is flawed. and solution is posted on the same comment section.
R.Wieser
2014-10-21 09:01:04 UTC
Permalink
Chano,
Post by chano
how does user communicate with process created with createprocess api?
That would be by commandline when the process is a console-based one, or by
the provided GUI when the proces is a windows based one. :-)

Ofcourse, both are proveded as-is and work needs to be done to actually
retrieve the entered text (console) or pressed buttons, etc. (GUI)
Post by chano
like user input goes to string array and output from console
program stored to string array?
Here you sound like you do not want the *user* (a person) but *another
process* to communicate with the newly started console-process.

In that case you need to connect (named) pipes to the newly-created
processes STDIN and STDOUT (preferrably STDERR too). What you than do with
the received STDOUT text is than fully upto you. More info:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).as
px

Ofcourse, if both processes are written by you you can also choose to
communicate between the processes by means of shared memory, TCP/IP (over
localhost) and others. You can find more about it here:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365574(v=vs.85).as
px

Regards,
Rudy Wieser
Post by chano
how does user communicate with process created with createprocess api?
Like user input or output to user if process is console program?
is there seperate api for communication with process?
or is there even a way to communicate user with console program?
like user input goes to string array and output from console program
stored to string array? so I can parser the data then do something about
it?
can someone post an example?
David Lowndes
2014-10-21 09:02:41 UTC
Permalink
Post by chano
how does user communicate with process created with createprocess api?
Like user input or output to user if process is console program?
is there seperate api for communication with process?
or is there even a way to communicate user with console program?
like user input goes to string array and output from console program
stored to string array? so I can parser the data then do something about
it?
I think you're asking to redirect the standard input/output - try
this:
http://msdn.microsoft.com/en-gb/library/windows/desktop/ms682499(v=vs.85).aspx

Dave
Rosario193
2014-10-21 09:16:20 UTC
Permalink
Post by chano
how does user communicate with process created with createprocess api?
Like user input or output to user if process is console program?
is there seperate api for communication with process?
or is there even a way to communicate user with console program?
like user input goes to string array and output from console program
stored to string array? so I can parser the data then do something about
it?
can someone post an example?
if process are in the same programs comunicate trhu memory
if process are in different programs if i remember well i used tcp/ip
Loading...