Anonymous pipe - Communication between process question.

Anonymous pipe - Communication between process question.

Post by Rocky Le » Fri, 07 Jun 2002 11:07:47



Hi all,
I use anonymous pipe to set up a communication channel between 2 processses
in the same machine. MSDN suggests that parent process can pass the pipe
handle to its child process on its command line or through its standard I/O
handles. So I use command line to make a try, but it doesn't work! The child
process cannot write to the pipe! It say the handle received is invalid!
I enclose my snippet here, could please take a look at it, and give me some
hint.
Thanks a lot,
Rocky Lee

///////////////////////
//    For Parent
///////////////////////
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <iostream>
using namespace std;
BOOL run_command_line_tool(/*IN*/LPCSTR lpCmdLine,
         /*IN*/UINT uCmdShow = SW_SHOW);
BOOL run_command_line_tool(/*IN*/LPCSTR lpCmdLine,
         /*IN*/UINT uCmdShow/* = SW_SHOW*/)
{
 STARTUPINFO si;
 ZeroMemory(&si, sizeof(si));
 si.cb = sizeof(si);
 si.dwFlags = STARTF_USESHOWWINDOW;
 si.wShowWindow = uCmdShow;

 PROCESS_INFORMATION pi;

 BOOL br = CreateProcess(NULL,
       const_cast<LPTSTR> (lpCmdLine),
       NULL,
       NULL,
       FALSE,
       NORMAL_PRIORITY_CLASS,
       NULL,
       NULL,
       &si,
       &pi);
 WaitForSingleObject(pi.hProcess, INFINITE);

 //DWORD dwExitCode = -1;

 if (br)
 {
  //GetExitCodeProcess(pi.hProcess, &dwExitCode);
  CloseHandle(pi.hThread);
  CloseHandle(pi.hProcess);
 }

 //return dwExitCode;
 // I don't use exit code, as winnt require some
 // security token to get it.
 return br;

Quote:}

void main()
{
 SECURITY_ATTRIBUTES sa;
 // Set the bInheritHandle flag so pipe handles are inherited.
 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
 sa.bInheritHandle = TRUE;
 sa.lpSecurityDescriptor = NULL;

 // These handle can be inherited.
 HANDLE hReadFromChild;
 HANDLE hWriteToChild;
 BOOL fRet = ::CreatePipe(&hReadFromChild,
       &hWriteToChild,
       &sa,
       1024);

 // We can pass the handles to a child process on its command line or
 // through its standard I/O handles. An unrelated process would need
 // to receive the handle by other means, such as through
 // a DDE conversation or a shared file.

 HANDLE hDupReadFromChild(NULL);
 fRet = ::DuplicateHandle(GetCurrentProcess(), // dupplicate from this
process.
        hReadFromChild, // Handle to be dupplicated.
        GetCurrentProcess(), // Process that will received the dupplicated
handle.
        &hDupReadFromChild, // Dupplicated handle.
        0, // Ignored.
        TRUE, // Dupplicated handle can be inherited.
        DUPLICATE_SAME_ACCESS);

 TCHAR szCommandLine[MAX_PATH];
 _stprintf(szCommandLine,
   _T("\\Vcpp\\Pipe\\PipeClient\\Debug\\PipeClient.exe %ul"),
   //hDupReadFromChild
   hReadFromChild);

 run_command_line_tool(szCommandLine);

 // Read from child.
 TCHAR cBuff[MAX_PATH];
 DWORD dwBytesRead;

 fRet = ReadFile(hDupReadFromChild,
     cBuff,
     sizeof(cBuff),
     &dwBytesRead,
     NULL);

 CloseHandle(hDupReadFromChild);
 CloseHandle(hReadFromChild);
 CloseHandle(hWriteToChild);

 cBuff[dwBytesRead] = _T('\0');
 cout << "Received from client: " << cBuff << endl;

Quote:}

/////////////////////////////////
//    For Child Process
/////////////////////////////////
#include <windows.h>
#include <tchar.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
 if (argc < 2)
  return 1;
 HANDLE hWriteToParent = 0;
 unsigned long ul = _tcstoul(argv[1], argv + _tcsclen(argv[1]), 16);
 hWriteToParent = reinterpret_cast<void*>(ul);
 cout << "Pipe Handle to write to Parent's process: " << hWriteToParent <<
endl;

 TCHAR szMsg[] = _T("A message from client program!");

 DWORD dwByteWritten(0);
 BOOL fRet = WriteFile(hWriteToParent,
       szMsg,
       sizeof(szMsg) + 1,
       &dwByteWritten,
       NULL);
 if (!fRet)
 {
  cout << "Cannot write to parent! Error: " << GetLastError() << endl;
 }
 else
 {
  cout << "Write successfully!" << endl;
 }

 return 0;

Quote:}

///////////
//END
///////////
 
 
 

Anonymous pipe - Communication between process question.

Post by Norman Bulle » Fri, 07 Jun 2002 11:38:02



> Hi all,
> I use anonymous pipe to set up a communication channel between 2 processses
> in the same machine. MSDN suggests that parent process can pass the pipe
> handle to its child process on its command line or through its standard I/O
> handles. So I use command line to make a try, but it doesn't work! The child
> process cannot write to the pipe! It say the handle received is invalid!
> I enclose my snippet here, could please take a look at it, and give me some
> hint.
> Thanks a lot,
> Rocky Lee

            < snipped >

Quote:>  _stprintf(szCommandLine,
>    _T("\\Vcpp\\Pipe\\PipeClient\\Debug\\PipeClient.exe %ul"),
>    //hDupReadFromChild
>    hReadFromChild);

            < snipped >

Quote:>  unsigned long ul = _tcstoul(argv[1], argv + _tcsclen(argv[1]), 16);

            < snipped >

It looks like you're formatting the HANDLE value as a decimal number
when you put it in the command line, but the child process appears to
interpret the command line argument as a hexadecimal number.

Norm

 
 
 

Anonymous pipe - Communication between process question.

Post by Rocky Le » Sat, 08 Jun 2002 19:29:09


Hi Norman,
Thank you for your reply. I've fixed that, but it still doesn't work. The
child process fails to write to the inherited handle. It seems like a access
denial.
Rocky Lee


> > Hi all,
> > I use anonymous pipe to set up a communication channel between 2
processses
> > in the same machine. MSDN suggests that parent process can pass the pipe
> > handle to its child process on its command line or through its standard
I/O
> > handles. So I use command line to make a try, but it doesn't work! The
child
> > process cannot write to the pipe! It say the handle received is invalid!
> > I enclose my snippet here, could please take a look at it, and give me
some
> > hint.
> > Thanks a lot,
> > Rocky Lee

>             < snipped >

> >  _stprintf(szCommandLine,
> >    _T("\\Vcpp\\Pipe\\PipeClient\\Debug\\PipeClient.exe %ul"),
> >    //hDupReadFromChild
> >    hReadFromChild);

>             < snipped >

> >  unsigned long ul = _tcstoul(argv[1], argv + _tcsclen(argv[1]), 16);

>             < snipped >

> It looks like you're formatting the HANDLE value as a decimal number
> when you put it in the command line, but the child process appears to
> interpret the command line argument as a hexadecimal number.

> Norm

 
 
 

1. Using anonymous pipe between service and child process

Hi.

I have an application which communicates with a child process through an
anonymous pipe. It works fine, but when I modify the application and use it
as a Windows Service that part does not work. It seems like the handles does
not get inherited by the child process.

I use the SetStdHandle and GetStdHandle to redirect standard in and out for
the child process to the pipe and it works fine as long as the parent
process is a normal application... The Service use the System Account. Isn't
this possible to do???

Thanks for any help!!!

/ Simon

2. I can't believe this: Dfhsm Audit Y2K critical problem

3. Anonymous pipe between unrelated processes?

4. Good place for PBLs??

5. anonymous pipes question

6. creating an EPS with fonts on QuarkXpress -> Windows PageMaker

7. Newbie Question: Usage of Windows Anonymous Pipe and WSOCK select call

8. anonymous pipes question

9. Q: Usage of anon. pipes for communication w/ child process

10. Q: Using anon. pipes for communication w/ child process

11. Parent / child process communication with pipes

12. Win2000 vs. Win98: Problems with anonymous pipes.