[英]Having trouble connecting to named pipe from a C++ win32 program
I have two programs that run on the same computer and I need them to talk. 我有两个在同一台计算机上运行的程序,我需要他们谈谈。 One is an existing program written in C++ using the older windows32 framework.
一个是使用较旧的Windows32框架以C ++编写的现有程序。 The other is a newly written C# application using the .NET framework.
另一个是使用.NET框架的新编写的C#应用程序。 My newer .NET application will act as the server and the older existing application the client.
我较新的.NET应用程序将充当服务器,而较旧的现有应用程序将充当客户端。
I have the named pipe created on both the client and server. 我在客户端和服务器上都创建了命名管道。 I am just not able to get them to connect.
我只是无法让他们连接。 I suspect the problem is simply I am not matching the file name correctly (named pipes use files in the background).
我怀疑问题只是因为我没有正确匹配文件名(命名管道在后台使用文件)。
Here is the .NET server code: 这是.NET服务器代码:
private volatile NamedPipeServerStream pipeServer;
pipeServer = new NamedPipeServerStream("MyPipe", PipeDirection.InOut);
pipeServer.WaitForConnection();
Here is the older existing win32 client code: 这是较旧的现有win32客户端代码:
HANDLE pipe = CreateFile(
"\\Device\\NamedPipe\\MyPipe",
GENERIC_READ | GENERIC_WRITE, // only need read access
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
I run the server code first and it sits there waiting for a connection. 我先运行服务器代码,然后坐在那里等待连接。 Every single time my handle is equal to
INVALID_HANDLE_VALUE
. 每当我的句柄等于
INVALID_HANDLE_VALUE
。
I used ProcessExplorer to see if I can find the true name of the pipe. 我使用ProcessExplorer来查看是否可以找到管道的真实名称。 According to ProcessExplorer the name is \\Device\\NamedPipe\\MyPipe .
根据ProcessExplorer的名称为\\ Device \\ NamedPipe \\ MyPipe 。
I think I am matching this on my client side. 我认为我在客户方面与此匹配。 If anyone can lend a suggestion I am appreciative.
如果有人可以提出建议,我将不胜感激。 Thanks.
谢谢。
To open a named pipe with CreateFile
, you need to specify the file name as \\\\.\\pipe\\name
, so in your case it would look like: 要使用
CreateFile
打开命名管道,您需要将文件名指定为\\\\.\\pipe\\name
,因此在您的情况下,它看起来像:
HANDLE pipe = CreateFile(
"\\\\.\\Pipe\\MyPipe",
GENERIC_READ | GENERIC_WRITE, // only need read access
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
Setting PipeSecurity
solved this issue for me. 设置
PipeSecurity
为我解决了这个问题。 Making it accessible for Everyone
looks less secure but it worked. 使
Everyone
可以访问它看起来不太安全,但是可以正常工作。
PipeSecurity ps = new PipeSecurity();
PipeAccessRule psRule = new PipeAccessRule(@"Everyone", PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow);
ps.AddAccessRule(psRule);
NamedPipeServerStream pipeServer = new NamedPipeServerStream("datapipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous, 1, 1, ps);
Source: Named Pipe Error - System.UnauthorizedAccessException: Access to the path is denied. 源: 命名管道错误-System.UnauthorizedAccessException:拒绝访问路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.