PowerShell 命名管道 : no connection?

标签 powershell named-pipes

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

在一个程序中,我使用 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 是有效的 - 这里的一切似乎都正常!

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

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 

我真的希望这种方式在读写时具有类似的访问权限n
来自/到管道和套接字,例如:

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

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

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

我为 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 被

阻止了
 $pipe.WaitForConnection()

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

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

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

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

有什么想法吗?

最佳答案

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

这是服务器脚本:

$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 命名管道 : no connection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24096969/

相关文章:

powershell - 将 IIS Web 应用程序转换为文件夹并返回

c++ - 命名管道服务器无法获取管道句柄状态

c++ - 创建命名管道时出现 GLE=5(拒绝访问)错误

python - 如何使用 Python 中的线程模块写入命名管道?

c - 使用 select() 和 O_NONBLOCK 从 FIFO 读取两个连续写入

sql-server - 在 SQL Server 中创建和修改的序列号

Powershell 不允许我对蓝牙设备使用 disable-pnpdevice

PowerShell 新命令包装器 : Supply values for the following parameters

powershell - 在 Get-ChildItem 值之前打印字符串

powershell - 在 PowerShell 模块中忽略 Write-Verbose