本文介绍了PowerShell命名管道:没有连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个命名管道来读写.

I need a named pipe to read and write.

在一个程序中,我使用 kernel32.dll 创建管道服务器:

In a program I create the pipe server using from kernel32.dll:

string PipeName = "\\\\.\\pipe\\myMT4";
int PipeMode = PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE|PIPE_WAIT; # tried too: PIPE_NOWAIT
int hPipe = CreateNamedPipeW(
            PipeName,
            PIPE_ACCESS_DUPLEX,
            PipeMode,
            PIPE_UNLIMITED_INSTANCES,1024,1024,
            NMPWAIT_USE_DEFAULT_WAIT,NULL);

hPipe句柄有效-这里的一切似乎都正常!

The handle hPipe is valid - every things seems to be ok here!

但是在PowerShell脚本中,我想打开一个客户端,连接并打开编写器-
并且无法连接=>超时

But in a PowerShell-script I want to open a client, connect and open the writer -
and cannot connect => timed out

function connect{
    Param ([PSObject] $h)
    ...
    $h.Pipe = New-Object -TypeName System.IO.Pipes.NamedPipeClientStream "\\.\pipe\PipeTest"
    $h.Pipe.Connect( 5000 )
    $h.Writer = New-Object -TypeName System.IO.StreamWriter $h.Pipe, $h.Encode

我真的希望这种方式在读写时具有相似的访问权限
从/到管道和承插口,例如:

I really would prefer this way to have a similar access when reading and writen
either from/to the pipe and sockets e.g.:

function write{
    Param ([PSObject] $h, [string] $line )
  try {
        $h.Writer.Write($line)
    }

怎么了?提前致谢,太好了.

What is wrong?Thanks in advance,Gooly.

PS:似乎该程序无法处理管道服务器-我必须打开管道客户端,并且该管道可以工作,但是会导致其他问题:

PS:It seems that the program cannot deal with pipe-servers - I have to open a pipe-client, and that works but that causes other problems:

我为PowerShell-pipe-server定义了

I define for the PowerShell-pipe-server :

 $pipeName = "testpipe"
 $pipeDir  = [System.IO.Pipes.PipeDirection]::InOut
 $pipeMsg  = [System.IO.Pipes.PipeTransmissionMode]::Message
 $pipeOpti = [System.IO.Pipes.PipeOptions]::Asynchronous
 $pipe = New-Object system.IO.Pipes.NamedPipeServerStream(
                  $pipeName, $pipeDir, 1, $pipeMsg, $pipeOpti )
 $pipe.WaitForConnection() #
 $sw = new-object System.IO.StreamWriter $pipe
 $sw.AutoFlush = $true
 $sw.WriteLine("Server pid is $pid")
 $sw.Dispose()
 $pipe.Dispose()

1)我的第一个问题是powerShell-pipe-server被

1) My first problem is now that the powerShell-pipe-server is blocked by

 $pipe.WaitForConnection()

直到客户端连接,但同时它必须独立处理2个不同的套接字,并且

until a client connects but it must handle 2 different sockets independently meanwhile and

2)如果客户端关闭连接,我无法告诉客户端再次打开同一管道,并且客户端收到Windows错误:ERROR_PIPE_BUSY 231

2) if the client closes the connection I was unable to tell the client to open the same pipe again and the client gets the Windows-error: ERROR_PIPE_BUSY 231

使用kernel32.dll-function构成我连接到服务器的程序:

Form the program I connect to the server with the kernel32.dll-function:

 int CallNamedPipeW(string PipeName,
           string outBuffer, int outBufferSz,
           uint& inBuffer[], int inBufferSz,
           int& bytesRead[], int timeOut
 );

有什么想法吗?

推荐答案

嗯,我可以让命名管道在两个不同的PowerShell会话之间工作,所以我不认为这是PowerShell固有的局限性:

Hmm, I can get named pipes to work between two different PowerShell sessions so I don't think it is an inherent PowerShell limitation:

这是服务器脚本:

$pipe = new-object System.IO.Pipes.NamedPipeServerStream 'testpipe','Out'
$pipe.WaitForConnection()
$sw = new-object System.IO.StreamWriter $pipe
$sw.AutoFlush = $true
$sw.WriteLine("Server pid is $pid")
$sw.Dispose()
$pipe.Dispose()

这是客户端脚本:

$pipe = new-object System.IO.Pipes.NamedPipeClientStream '.','testpipe','In'
$pipe.Connect()
$sr = new-object System.IO.StreamReader $pipe
while (($data = $sr.ReadLine()) -ne $null) { "Received: $data" }
$sr.Dispose()
$pipe.Dispose()

客户输出:

Received: Server pid is 22836

这篇关于PowerShell命名管道:没有连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:40