goo*_*oly 3 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);
Run Code Online (Sandbox Code Playgroud)
句柄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
Run Code Online (Sandbox Code Playgroud)
当
从/向管道和套接字读取和写入时,我真的更喜欢这种方式具有类似的访问权限,例如:
function write{
Param ([PSObject] $h, [string] $line )
try {
$h.Writer.Write($line)
}
Run Code Online (Sandbox Code Playgroud)
怎么了?先谢谢,Gooly.
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()
Run Code Online (Sandbox Code Playgroud)
1)我的第一个问题是powerShell-pipe-server被阻止了
$pipe.WaitForConnection()
Run Code Online (Sandbox Code Playgroud)
直到客户端连接,但它必须独立处理2个不同的套接字
2)如果客户端关闭连接,我无法告诉客户端再次打开相同的管道,客户端收到Windows错误:ERROR_PIPE_BUSY 231
形成我使用kernel32.dll函数连接到服务器的程序:
int CallNamedPipeW(string PipeName,
string outBuffer, int outBufferSz,
uint& inBuffer[], int inBufferSz,
int& bytesRead[], int timeOut
);
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
嗯,我可以让命名管道在两个不同的PowerShell会话之间工作,所以我不认为这是一个固有的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()
Run Code Online (Sandbox Code Playgroud)
这是客户端脚本:
$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()
Run Code Online (Sandbox Code Playgroud)
客户输出:
Received: Server pid is 22836
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5120 次 |
| 最近记录: |