【问题标题】:Error on attempt to create named pipe using VBScript尝试使用 VBScript 创建命名管道时出错
【发布时间】:2014-09-04 23:02:34
【问题描述】:

我正在尝试在 win7 上使用 VBScript 创建命名管道。 这是我的代码(取自there):

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("\\.\pipe\PipeName", True)
a.WriteLine("This is a test.")
a.Close

但我遇到了一个错误(手动翻译,所以可能不准确):

test.vbs(2, 1) Microsoft VBScript runtime error: File not found

与普通文本文件相同的代码可以正常工作:

Set a = fs.CreateTextFile(".\\PipeName", True)

但是,当我试图逃避反斜杠时:

Set a = fs.CreateTextFile("\\\\.\\pipe\\PipeName", True)

我明白了:

test.vbs(2, 1) Microsoft VBScript runtime error: Path not found

UPD:我以管理员身份运行脚本。

UPD2:我找到了另一种不使用管道的问题的解决方案,所以我的问题有点过时了,但我不知道该怎么做。

【问题讨论】:

  • 我认为您所引用问题的答案假设管道已经存在
  • 我尝试了一些随机名称,但这没有帮助。

标签: windows-7 vbscript named-pipes


【解决方案1】:

您是否创建了名为“PipeName”的命名管道服务器?这段代码对我有用(我将管道命名为“HelloWorld”):

C# 服务器:

static void Main(string[] args)
{
    using (var pipe = new NamedPipeServerStream("HelloWorld"))
    {
        pipe.WaitForConnection();
        StreamReader reader = new StreamReader(pipe);
        var line = reader.ReadLine();
        Console.WriteLine(line);
    }
    Console.ReadLine();
}

VBScript 客户端:

Dim fs, pipe
Set fs = CreateObject("Scripting.FileSystemObject")
Set pipe = fs.OpenTextFile("\\.\pipe\HelloWorld", 8, False, 0)
pipe.WriteLine("This is my message")
pipe.Close

【讨论】:

  • 是否有必要从 C/C#/pascal/etc 创建管道?我想使用管道来重定向控制台应用程序的输入、输出和错误流,并假设我可以从 VBS 创建一个管道。
  • 是的,除非您能找到从 VBScript 创建管道的方法。不幸的是,我不能。
【解决方案2】:

我尝试使用来自 VBScript 的命名管道。我未能通过fso.CreateTextFile("\\.\pipe\MyPipe") 创建命名管道。

但我通过经典应用程序创建的管道成功地从 VBScript 连接:

管道是由这样的代码(pascal)创建的:

procedure OpenTestPipe;

var
 i,hOut: Integer;

begin

  hOut:=CreateNamedPipe('\\.\pipe\Test.htm',PIPE_ACCESS_OUTBOUND,PIPE_TYPE_BYTE,PIPE_UNLIMITED_INSTANCES,1024,1024,NMPWAIT_USE_DEFAULT_WAIT,nil);

  i:=FileWrite(hOut,'Hello'#13#10,7);

MessageBox(0,'Pipe is opened','Pipe sample',0);

  FileClose(hOut);

end;

当显示 MessageBox 时,我打开了 VBScript

Set fso = CreateObject("Scripting.FileSystemObject")

MsgBox fso.OpenTextFile("\\.\pipe\test.htm",1).readLine

收到一条消息你好

【讨论】:

  • 感谢您的想法,但我不想为 wix 编写二进制模块来执行此操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
  • 2021-10-18
  • 1970-01-01
  • 2022-01-24
相关资源
最近更新 更多