CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Nov 2011
    Posts
    2

    How to Write a program to communicate between two processes using the pipe.

    I am learning operating system, this is a really big trouble for me, i do not how to write that kind of program above in C++, if anyone know the answer please post complete "code" that write in C++, this is urgent, thank you so much, this is question:
    Write a program to communicate between two processes using the pipe as follows:
    A process read from the file consists of multiple consecutive sequences, each sequence of the operations +, -, *, / and 2 mathematics
    out. For example, the file will save the string like this:
    2 + 3
    1 to 2
    4 * 6
    15 / 3
    Then the first process of the first sequence send(transmit) data for the second process . the second process
    perform calculations and return the resulting string back to the first process to record the
    file as follows:
    2 + 3 = 5
    1-2 = -1
    4 * 6 = 24
    15 / 3 = 5

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: How to Write a program to communicate between two processes using the pipe.

    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: How to Write a program to communicate between two processes using the pipe.

    if anyone know the answer please post complete "code" that write in C++, this is urgent, thank you so much
    FYI. If this is really urgent, please follow the Victor's advice.
    Last edited by Igor Vartanov; November 10th, 2011 at 05:34 AM.
    Best regards,
    Igor

  4. #4
    Join Date
    Nov 2011
    Posts
    2

    Re: How to Write a program to communicate between two processes using the pipe.

    Thank you anyway.

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: How to Write a program to communicate between two processes using the pipe.

    However, please don't quit the thread. Try to start some real coding, identify your problems, post your code (snippets, or better compilable project), and people here will be glad to answer your specific questions.
    Best regards,
    Igor

  6. #6
    Join Date
    May 2002
    Posts
    1,798

    Re: How to Write a program to communicate between two processes using the pipe.

    Interesting links, Victor. Is there any advantage to using pipes as opposed to using WM_COPYDATA ?
    mpliam

  7. #7
    Join Date
    Jun 2006
    Location
    Chile
    Posts
    13

    Re: How to Write a program to communicate between two processes using the pipe.

    Quote Originally Posted by Mike Pliam View Post
    Interesting links, Victor. Is there any advantage to using pipes as opposed to using WM_COPYDATA ?
    You reread the links posted and extract information yourself.
    Pumpobee is prolounced as mumbolee

  8. #8
    Join Date
    May 2002
    Posts
    1,798

    Re: How to Write a program to communicate between two processes using the pipe.

    I have compiled and run the examples given by M$. The coding is pretty advanced and, although I found it works OK, I am concerned about using this approach without soliciting the opinions of those more experienced than I am.

    I have been having trouble processing errors from a DLL. I have considered using MessageBox(...), files, PostMessage(WM_COPYDATA, ...), sockete, and DLL callbacks, but I have never tried using pipes which I have come to think of as somewhat arcane.

    My question is: Have any gurus had experience using pipes to send error messages from a DLL to an application? What is the best way to handle DLL errors? One thing is certain, throwing exceptions across DLL boundaries is a big NO NO!

    This is an old topic that has come up many times before. The web is replete with comments regarding error processing in DLLs. But I have not come across any good code examples dealing with the problem.

    FWIW, I had started an old thread that posed a similar question, but my question was somewhat misinterpreted as how to debug a DLL. I am NOT asking how to debug a DLL, rather, how to build an interface with an MFC App and DLL that can feed back messages to the App user during runtime.
    http://www.codeguru.com/forum/showth...r+messages+dll

    Your thoughts greatly appreciated.
    Last edited by Mike Pliam; November 15th, 2011 at 10:40 PM.
    mpliam

  9. #9
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: How to Write a program to communicate between two processes using the pipe.

    Quote Originally Posted by Mike Pliam View Post
    Interesting links, Victor. Is there any advantage to using pipes as opposed to using WM_COPYDATA ?
    WM_COPYDATA:

    Pros:
    1. Faster/very easy to implement for a developer.

    Cons:
    1. Slower to run (implemented on a user level, goes through a message pump).
    2. Requires a window (HWND) to accept messages (i.e. won't work for a console process, or a service.)
    3. Doesn't work across sessions or desktops.
    4. Less reliable (SendMessage may fail for no apparent reason.)
    5. Easy to run into a deadlock between threads (by using SendMessage) or a race condition (with PostMessage.)

    Pipes:

    Pros:
    1. Works across sessions and/or various desktops.
    2. Runs faster (implemented on a kernel level via memory mapping).
    3. Can send larger amounts of data.

    Cons:
    1. Quite challenging to implement.
    2. Difficult to debug.
    3. Lack of reliable documentation. MSDN samples are often buggy.

  10. #10
    Join Date
    May 2002
    Posts
    1,798

    Re: How to Write a program to communicate between two processes using the pipe.

    Thanks ahmd. I have been trying implement named pipes to pass error messages from a DLL using modifications of the code provided by M$:

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    I opted to rewrite the pipe client and pipe server codes as two separate classes, and to convert both files from unicode to multibyte. When these classes are independently implemented in separate Win32 console apps, they work just fine. The only problem that I encountered was that of trying to use the DWORD WINAPI InstanceThread(LPVOID); callback function in my CNamedPipeServer class. I finally just took it out of the class and set it as a free-standing C-language function.

    Encouraged by my initial success, I tried using these classes int a DLL and it's calling app. But using the pipe client in a DLL class and the pipe server in a Win32 app calling class, doesn't work and all I keep getting is the message:
    Could not open pipe. GLE=2
    Now, if I try to run the pipe client without booting the pipe server app in the non-dll setting, I get the same message. As soon as I boot the pipe server app, the message disappears and everything is working. So it appears that my problem in the dll-app setting is my inability to properly initialize the server in the main app. Another problem is how to capture the messages received by the server, but that should not be too hard to do.

    I have attached a Win32 demo of my named pipe classes which works. I have also attached a demo app to show my problem sendiing messages through a named pipe from a dll to a calling app.

    Your thoughts greatly appreciated.
    Attached Files Attached Files
    Last edited by Mike Pliam; November 17th, 2011 at 07:03 PM.
    mpliam

  11. #11
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: How to Write a program to communicate between two processes using the pipe.

    Listen, just curious, why would you want to rewrite it from using unicode to multibyte? That seems pretty masochistic. Every system since Windows 2000 uses Unicode internally.

    As those MS examples go, I saw those too and I'd stay away from copying them one-to-one. As you can see by the number of comments below, they are far from perfect. What they fail to show is that one needs to properly configure the DACL for each pipe server (or lpSecurityAttributes parameter in CreateNamedPipe), since otherwise you may have a conflict of a less privileged user process (or a pipe client) trying to connect to a pipe server that is most certainly running with higher privileges (as a local service).

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: How to Write a program to communicate between two processes using the pipe.

    Every system since Windows 2000 uses Unicode internally.
    You had to say "since Windows NT 3.1" actually. Windows NT OS family made Unicode-based since the very first version released in 1993.
    Best regards,
    Igor

  13. #13
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: How to Write a program to communicate between two processes using the pipe.

    otherwise you may have a conflict of a less privileged user process (or a pipe client) trying to connect to a pipe server that is most certainly running with higher privileges (as a local service).
    A service may expose the pipe with NULL DACL but impersonate client to avoid this privilege elevation. It depends on design points.
    Best regards,
    Igor

  14. #14
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: How to Write a program to communicate between two processes using the pipe.

    Quote Originally Posted by Igor Vartanov View Post
    A service may expose the pipe with NULL DACL...
    Maybe I'm mistaking, but isn't it actually "frowned upon" to set a NULL DACL? It sure is easy to program but it may become a security breach.

  15. #15
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: How to Write a program to communicate between two processes using the pipe.

    Let me quote myself:
    Quote Originally Posted by Igor Vartanov
    It depends on design points.
    Best regards,
    Igor

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured