Code that check for the existence of the server’s named pipe in C#

nsmb-piranha-plant

From this MSDN link

Code:

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool WaitNamedPipe(string name, int timeout);
 
public static bool namedPipeExist(string pipeName)
{
    try
    {
        int timeout = 0;
        string normalizedPath = System.IO.Path.GetFullPath(
            string.Format(@"\\.\pipe\{0}", pipeName));
        bool exists = WaitNamedPipe(normalizedPath, timeout);
        if (!exists)
        {
            int error = Marshal.GetLastWin32Error();
            if (error == 0) // pipe does not exist
                return false;
            else if (error == 2) // win32 error code for file not found
                return false;
            // all other errors indicate other issues
        }
        return true;
    }
    catch (Exception)
    {
        return false; // assume it doesn't exist
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s