Delete named pipe.NET

I list my named pipes using this code:

private IEnumerable<string> GetNamedPipesList() { string[] listOfAllPipes = Directory.GetFiles(@"\\.\pipe\"); return listOfAllPipes.Where(pipe => pipe.Contains("FST")); } 

It works great. I can display them:

  private void Scan_Click(object sender, EventArgs e) { IEnumerable<string> fsbPipes = GetNamedPipesList(); tbxOutput.Text = string.Empty; foreach (string fsbPipe in fsbPipes) { var pipe = fsbPipe.Replace(@"\\.\pipe\", ""); tbxOutput.AppendText(pipe + Environment.NewLine); } } 

Examples:

 FST-MT4_Miroslav-130 FST-MT4_Miroslav-150 FST-MT4_Miroslav-120 

After that, I want to close them by deleting the files as:

  private void CloseAll_Click(object sender, EventArgs e) { IEnumerable<string> myPipes = GetNamedPipesList(); foreach (string pipe in myPipes) { try { File.Delete(pipe); } catch (Exception exception) { tbxOutput.AppendText(exception.Message + Environment.NewLine); } } } 

But it returns the parameter is invalid. I'm not sure I can do this, but I had to try, since I downloaded them as files.

How can I close these named pipes?

+4
source share
2 answers

According to MSDN , there is a set of classes designed to work with named pipes, but they only apply when you actually own them in your application.

What you are doing here is enumerating named pipes through a lower API file system. However, namedpipes are really not like files: they are shared by many processes, and there is a mechanism similar to garbagecollection that automatically deletes them when it is no longer used (see http://social.msdn.microsoft.com/ Forums / vstudio / en-US / 66f747a0-22c9-4e7d-bb5a-e4e63197318a / deleting-named-pipe ). Strictly speaking: when all interested processes close their handles to the pipe, they will be destroyed.

Look at the lower-level API for pipe processing: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365150(v=vs.85).aspx There is no mention of destruction. Create Connect Disconnect only.

If I remember correctly, even on * nix file systems, "rm" on a named pipe does not destroy it. It separates the inodes files from the directory and effectively β€œhides” the special file, but the one who has this file open holds it and pops it out, and the special file still exists and is used until the last file descriptor is closed, only then the actual file is really freed.

So, maybe you should try to rephrase your problem and find who is holding the phone, and then "talk" with them to close the pen in the usual way? That is, connect to the pipe and send a polite message "please exit" in the correct application protocol?

Well, having written all this, now until the actual solution.

Since you know the handset, if you have access rights high enough, you can do anything. You can list all processes, you can check them on your Handles, you can check each descriptor and check what it points to. If it points to the handset, you can extract the name of this channel. If the name matches your pipe, you can insert a stream into this process, and from this stream you can close the descriptor and immediately SUSPECT this process if it detects a broken descriptor and starts reconnecting to this channel. Loop around all the processes and you can be sure that your pipe will be closed. You can then do your things with your now defunct channel and resume all these processes.

I think that all this is quite possible, since some tools, such as ProcessHacker2, can display many details of the handles of other processes, and also allow you to close this handle on demand. So, just duplicate, filter and automate this task and its implementation. But, indeed, this is a dangerous and hardly stable / reliable solution.

+3
source

Thanks to @CodeCaster and @quetzalcoatl, I found out that we cannot delete named pipes, but we have to kill their holders.

The easiest way to do this if we know the name of the owner is to use the task manager.

I made a small tool that lists named pipes. It supports filtering.

Download link: ListNamedPipes.exe

No filter

Filter Chrome pipes

The code is here:

  private void Scan_Click(object sender, EventArgs e) { IEnumerable<string> fsbPipes = GetNamedPipesList(); tbxOutput.Text = string.Empty; foreach (string fsbPipe in fsbPipes) { string pipe = fsbPipe.Replace(@"\\.\pipe\", ""); tbxOutput.AppendText(pipe + Environment.NewLine); } } private IEnumerable<string> GetNamedPipesList() { string[] listOfAllPipes = Directory.GetFiles(@"\\.\pipe\"); return listOfAllPipes.Where(pipe => pipe.Contains(tbxFilter.Text)); } 
+3
source

Source: https://habr.com/ru/post/1489256/


All Articles