我正在尝试让一个C++应用程序在特定操作发生时通知一个C#应用程序。我尝试使用命名管道来实现这一点。
我已经在C++应用程序上设置了一个命名管道服务器,它似乎正在工作(命名管道被创建 - 它出现在PipeList检索到的列表中),并在C#应用程序上设置了一个命名管道客户端,但失败了:C#客户端代码的第一行给出了“管道句柄未设置。你的PipeStream实现是否调用了InitializeHandle?”错误,第二行抛出了“拒绝访问路径”的异常。
我错在哪里? C++服务器代码
我已经在C++应用程序上设置了一个命名管道服务器,它似乎正在工作(命名管道被创建 - 它出现在PipeList检索到的列表中),并在C#应用程序上设置了一个命名管道客户端,但失败了:C#客户端代码的第一行给出了“管道句柄未设置。你的PipeStream实现是否调用了InitializeHandle?”错误,第二行抛出了“拒绝访问路径”的异常。
我错在哪里? C++服务器代码
CString namedPipeName = "\\\\.\\pipe\\TitleChangePipe";
HANDLE pipe = CreateNamedPipe(namedPipeName, PIPE_ACCESS_INBOUND , PIPE_WAIT, 1, 1024, 1024, 120 * 1000, NULL);
if (pipe == INVALID_HANDLE_VALUE) {
MessageBox(NULL, "Pipe Could Not be Established.", "Error: TCM", MB_ICONERROR);
return -1;
}
char line[512]; DWORD numRead;
while (true)//just keep doing this
{
numRead = 1;
while ((numRead < 10 || numRead > 511) && numRead > 0)
{
if (!ReadFile(pipe, line, 512, &numRead, NULL) || numRead < 1) {//Blocking call
CloseHandle(pipe); //If something went wrong, reset pipe
pipe = CreateNamedPipe(namedPipeName, PIPE_ACCESS_INBOUND , PIPE_WAIT, 1, 1024, 1024, 120 * 1000, NULL);
ConnectNamedPipe(pipe, NULL);
if (pipe == INVALID_HANDLE_VALUE) {
MessageBox(NULL, "Pipe Could Not be Established.", "Error: TCM", MB_ICONERROR);
return -1; }
numRead = 1;
}
}
line[numRead] = '\0'; //Terminate String
}
CloseHandle(pipe);
C#客户端代码
var client = new NamedPipeClientStream(".", "TitleChangePipe", PipeDirection.InOut);
client.Connect();
var reader = new StreamReader(client);
var writer = new StreamWriter(client);
while (true)
{
var input = Console.ReadLine();
if (String.IsNullOrEmpty(input))
break;
writer.WriteLine(input);
writer.Flush();
Console.WriteLine(reader.ReadLine());
}
CreateNamedPipe
的实例,以便按照建议的方式使用变量。就在使用管道之前等待连接方面,据我所知,这就是ConnectNamedPipe
所做的。话虽如此,我不再遇到“拒绝访问路径”的异常,但仍会在客户端代码的第一行收到“未设置管道句柄。您的PipeStream实现是否调用InitializeHandle” 的错误提示。 - Claudiu