命名管道服务器没有侦听(Named Pipes Server Not Listening)

2019-09-28 17:45发布

我试图做一个Windows服务,简单地坐在并侦听命名管道的消息。 但它不工作。 尝试连接到命名管道进行简单的时间,当我查看进程资源管理器服务的进程的进程句柄,我看不到命名管道。

这是我到目前为止有:

namespace Service
{
    class Service : ServiceBase
    {
        Thread workerThread;
        static bool serviceStarted = false;
        static PipeSecurity pipeSecurity = new PipeSecurity();        

        public Service()
        {
            this.ServiceName = "Service";
            this.EventLog.Log = "Application";
            this.EventLog.Source = "ServiceName";
            this.CanHandlePowerEvent = false;
            this.CanHandleSessionChangeEvent = false;
            this.CanPauseAndContinue = false;
            this.CanShutdown = true;
            this.CanStop = true;
        }

        static void Main()
        {
            ServiceBase.Run(new Service());
        }

        protected override void OnStart(string[] args)
        {                
            base.OnStart(args);

            pipeSecurity.AddAccessRule(new PipeAccessRule("Authenticated Users", PipeAccessRights.ReadWrite, AccessControlType.Allow));
            pipeSecurity.AddAccessRule(new PipeAccessRule(WindowsIdentity.GetCurrent().User, PipeAccessRights.FullControl, AccessControlType.Allow));

            ThreadStart threadStart = new ThreadStart(WorkerThread);
            workerThread = new Thread(threadStart);
            workerThread.Start();
            serviceStarted = true;    
        }

        protected override void OnStop()
        {                
            base.OnStop();
            serviceStarted = false;
            workerThread.Join(new TimeSpan(0, 0, 30)); 
        }

        protected override void OnShutdown()
        {
            base.OnShutdown();
            serviceStarted = false;
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        private void WorkerThread()
        {
            while (serviceStarted)
            {
                try
                {
                    using (NamedPipeServerStream pipeServerStream = new NamedPipeServerStream("PipeName", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.None, 128, 128, pipeSecurity))
                    {
                        pipeServerStream.WaitForConnection();
                        byte[] buffer = new byte[128];
                        StringBuilder message = new StringBuilder();
                        do
                        {
                            int len = pipeServerStream.Read(buffer, 0, buffer.Length);
                            message.Append(Encoding.UTF8.GetString(buffer, 0, len));
                        }
                        while (!pipeServerStream.IsMessageComplete);
                        EventLog.WriteEntry(message.ToString());
                    }
                }
                catch(Exception ex)
                {

                }
                finally
                {
                    if (serviceStarted)
                        Thread.Sleep(100);
                }
            }
            Thread.CurrentThread.Abort();
        }
    }
}

好像只是没有得到创建命名管道。 有任何想法吗? 当我尝试通过PowerShell连接:

PS C:\> $Client = New-Object System.IO.Pipes.NamedPipeClientStream(".", "PipeName", [System.IO.Pipes.PipeDirection]::Out)
PS C:\> $Client.Connect(1000)

Exception calling "Connect" with "1" argument(s): "The operation has timed out."
At line:1 char:1
+ $Client.Connect(1000)

Answer 1:

   new NamedPipeServerStream("PipeName", ...)

这管道是只对在同一会话中运行的进程进行访问。 这是服务会话0。 您Powershell的程序在桌面会话中运行。 为了使其可见所有会话,则必须使用前缀名"Global\\"

顺便说一句,不要犹豫,加入一些记录,以便你有确凿的证据证明管服务器要调用为waitForConnection()。 这消除了对你(和我们)结束相当的疑问,管道实际上在听。 使用空的catch块产生太多FUD。



文章来源: Named Pipes Server Not Listening