none
Deleting Named Pipe? RRS feed

  • Question

  • Hi! I need to write a DeleteNamedPipe(string namedPipeName) method.

    I understand that I need to obtain a handle (SafeFileHandle) to the file.

    How can I get the handle based on the namedPipeName?

    Then I would do:

                    FlushFileBuffers(hHandle);
                    DisconnectNamedPipe(hHandle);
                    CloseHandle(hHandle);

            [DllImport("kernel32.dll", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            static extern bool FlushFileBuffers(SafeFileHandle hFile);

            [DllImport("kernel32.dll", SetLastError = true)]
            private static extern bool DisconnectNamedPipe(SafeFileHandle hHandle);

            [DllImport("kernel32.dll", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            static extern bool CloseHandle(SafeFileHandle hObject);

    Will it be sufficient enough? What else can I do ?

    Many thanks in advance,

    --Michael

    Tuesday, May 8, 2012 12:54 AM

Answers

  • The purpose of the named pipe is to have multiple processes communicating with each other. So even though the process that created the pipe is closed, that doesn't mean that the other processes using the pipe have closed their handle on the pipe. Once those processes close their handle of the pipe will the pipe get cleaned up.
    Thursday, May 10, 2012 4:04 AM
  • Thank you for taking a look.

    The pipe is already created. Do I still use CreateNamedPipe function to create one to obtain a handle? Will it create multiples?

    What do I pass in terms of parameters? How can I find out what the attributes of the pipe I want to delete are?I just know the name of it.

    Many thanks in advance,

    --Michael

    Hi Michael,

    I agree with Jader. A named pipe can be used in many other process by making many instances. The documentation tells us: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365150(v=vs.85).aspx 

    A named pipe server process uses this function either to create the first instance of a specific named pipe and establish its basic attributes or tocreate a new instance of an existing named pipe.

    You can see, you cannot find a handle of a named pipe by its name. Because you may close the others instance of this named pipe.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thursday, May 17, 2012 10:44 AM
    Moderator

All replies

  • Hi Michael,

    Welcome to the MSDN Forum.

    Based on this documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365166(v=vs.85).aspx 

    Parameters

    hNamedPipe [in]

    A handle to an instance of a named pipe. This handle must be created by the CreateNamedPipe function.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, May 8, 2012 2:56 PM
    Moderator
  • Thank you for taking a look.

    The pipe is already created. Do I still use CreateNamedPipe function to create one to obtain a handle? Will it create multiples?

    What do I pass in terms of parameters? How can I find out what the attributes of the pipe I want to delete are?I just know the name of it.

    Many thanks in advance,

    --Michael

    Tuesday, May 8, 2012 6:05 PM
  • First look at the Task Manager.  The Task manager has a process name and a PID.  The PID is the handle.  The term handle is usually reserved for for Window Processes, but can be used for any Process.  The webpage Mike feng referenced uses the Kernel32.dll.  Many of the kernel32.dll mthods are implimented in the Net Library Process Class (see webpage below). The Process class uses the System.Dll which is a Net Library dll, but most of these methods are just wrappers which call the kernel32.dll.  So yo have a choice of looking up the PIP names using either the kernel32.dll function of the net library equivalent function which in bothe cases are called GetProcessbyName.  Once you get the process ID you can kill the Pipe using Mike feng kernel32 method.

    http://msdn.microsoft.com/en-us/library/ccf1tfx0.aspx

    Since you are using a kernel32.dll function there is very little resources that are used inside C# associated with the killing the Pipe.  the windows operating system contains most of the resources and will do all the cleanup.  You don't se it in the Task Manager, but every process besides having a PIP als has a PPID (Parent Process ID).  When you kill any process windows automatically checks for any process that has the Process you are killing as a parent and also kills any children (and children of the children) of the process.  The processes are all in numerical order in the orer that they were started so to minize any harmfull effects of shutting down a process the processes are killed in the reverse order from when they were started (highest child process first).


    jdweng

    Wednesday, May 9, 2012 3:09 AM
  • Thank you for taking a look.

    The pipe is already created. Do I still use CreateNamedPipe function to create one to obtain a handle? Will it create multiples?

    What do I pass in terms of parameters? How can I find out what the attributes of the pipe I want to delete are?I just know the name of it.

    Many thanks in advance,

    --Michael

    Hi Michael,

    How do you know the pipe is already? I don't doubt about the existence of the pipe. I ask this because when you know it exists, you should know something about it. For example, may be you have a reference of the pipe, or something else.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 9, 2012 3:54 AM
    Moderator
  • Hi Michael,

    Please checkout this:

    http://www.eggheadcafe.com/articles/20060404.asp


    Regards, http://shwetamannjain.blogspot.com

    Wednesday, May 9, 2012 1:12 PM
  • Hi! Thank you all for helping. I should've provided more background about why I want to delete a pipe.

    We bought some unique 3rd party closed source app that creates several pipes. When the app’s components close they do not always clean up the pipes.  I can see the pipes using my Named Pipe Enumerator I wrote and whose code I hope to post in this thread if I figure out how to delete a named pipe. When the components launch again, they fail because the pipes are already there. I have to use that particular app because it’s the only one on the market and our business people insist on using it.

    So if it was I who created the pipe I would know the handle. Unfortunately in my case, I do not create the pipe I need to delete. I rather enumerate all the pipes on a PC and pick the ones I need to delete.

    Many thanks in advance,

    --Michael Z...

     

    Wednesday, May 9, 2012 6:18 PM
  • Windows will clean up the named pipes if no processes are using them.
    Wednesday, May 9, 2012 7:42 PM
  • Joel Engineer,      

    Neither Process.GetProcessByName() nor GetProcesses()  return the handles of Named Pipes.

                // Get all processes running on the local computer.
                Process[] localAll = Process.GetProcesses();
                foreach(Process p in localAll)
                {
                    Debug.WriteLine(p.ProcessName);  // Named Pipes do not show up
                }

    My question remains open.

    Many thanks in advance,

    Wednesday, May 9, 2012 7:48 PM
  • Waiting does not work for me  - people open and close the apps so I need to clean up named pipes explicitly.

    Wednesday, May 9, 2012 7:49 PM
  • "When the app’s components close they do not always clean up the pipes.  I can see the pipes using my Named Pipe Enumerator I wrote and whose code I hope to post in this thread if I figure out how to delete a named pipe."

    There's no way to "delete" a named pipe. Like all kernel objects a pipe is automatically deleted when the last handle to it is closed. If some 3rd party code creates a pipe and then fails to close its handle then there's not much you can do, you need to somehow obtain the handle that code is using and close it. But since it's 3rd party code I doubt that you can access that handle easily.

    Wednesday, May 9, 2012 9:46 PM
    Moderator
  • Can't you find the parent of each pipe and kill() the parents?

    jdweng

    Wednesday, May 9, 2012 9:55 PM
  • I am afraid I do not know how to find a parent. The app is closed but the pipes are still there sometimes.
    Thursday, May 10, 2012 12:08 AM
  • The purpose of the named pipe is to have multiple processes communicating with each other. So even though the process that created the pipe is closed, that doesn't mean that the other processes using the pipe have closed their handle on the pipe. Once those processes close their handle of the pipe will the pipe get cleaned up.
    Thursday, May 10, 2012 4:04 AM
  • My first question is if you can kill the process using the task manager.  any task that yo ucan kill in the task manager you should be able to kill in software.  Second, when you do kill the process in the task manager does any other process die?

    The Parent PID (PPID) is part of the process structure.  You never said what the stream was connected to.  If you are connected to a com port you can look at the interfaces and bind to the active port and then close the port.  Yo can check manually that port status by using the DOS command : netstat -a.


    jdweng

    Thursday, May 10, 2012 9:00 AM
  • Thank you for taking a look.

    The pipe is already created. Do I still use CreateNamedPipe function to create one to obtain a handle? Will it create multiples?

    What do I pass in terms of parameters? How can I find out what the attributes of the pipe I want to delete are?I just know the name of it.

    Many thanks in advance,

    --Michael

    Hi Michael,

    I agree with Jader. A named pipe can be used in many other process by making many instances. The documentation tells us: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365150(v=vs.85).aspx 

    A named pipe server process uses this function either to create the first instance of a specific named pipe and establish its basic attributes or tocreate a new instance of an existing named pipe.

    You can see, you cannot find a handle of a named pipe by its name. Because you may close the others instance of this named pipe.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thursday, May 17, 2012 10:44 AM
    Moderator