WaitNamedPipe 只是挂起
我在 WaitNamedPipe 函数挂起时遇到问题。 ...这是我的代码中与问题相关的部分。我创建了一个进程,然后创建了一个管道,并且函数 WaitNamedPipe 似乎卡在 FALSE 上,因此挂起。函数 waitnamedpipe 等待 CC 进程启动。
PROCESS_INFORMATION po;
STARTUPINFO s;
GetStartupInfo (&s);
if(CreateProcess ("c:\\s2.exe", NULL, NULL, NULL, false, 0, NULL, NULL, &s, &po) == FALSE)
{
printf("Error %d starting CC\n", GetLastError());
exit(-1);
}
HANDLE pipe=CreateNamedPipe ("\\.\pipe\CC-"+po.dwProcessId, 0x00000003, 0x40000000,
0x00080000L, 0x00000004, 128, 0, NULL);
while (WaitNamedPipe ("\\.\pipe\CC-"+po.dwProcessId, INFINITE) == FALSE )
Sleep (300);
*编辑我改变了一些..但是它仍然挂起
PROCESS_INFORMATION po; STARTUPINFO s;
GetStartupInfo (&s);
if(CreateProcess ("c:\\s2.exe", NULL,
NULL, NULL, false, 0, NULL, NULL, &s,
&po) == FALSE) {
printf("Error %d starting CC\n", GetLastError());
exit(-1);
}
HANDLE pipe=CreateNamedPipe(pipe_name, 0x00000003,
FILE_FLAG_FIRST_PIPE_INSTANCE,
PIPE_UNLIMITED_INSTANCES,128, 128, 0,
NULL);
while(WaitNamedPipe(pipe_name, INFINITE)==FALSE)
Sleep(300);
HANDLE CC = CreateFile (pipe_name,
GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL);
bool fConnected = ConnectNamedPipe(pipe, NULL) ? TRUE :
(GetLastError() == ERROR_PIPE_CONNECTED);
if(fConnected) printf("true"); else printf("false");
I'm having trouble with the WaitNamedPipe function hanging. ...this the portion of my code relevant to the problem. I created a process, then a pipe, and the function WaitNamedPipe seems to be stuck on FALSE and therefore hangs. The function waitnamedpipe waits for the CC process to initiate.
PROCESS_INFORMATION po;
STARTUPINFO s;
GetStartupInfo (&s);
if(CreateProcess ("c:\\s2.exe", NULL, NULL, NULL, false, 0, NULL, NULL, &s, &po) == FALSE)
{
printf("Error %d starting CC\n", GetLastError());
exit(-1);
}
HANDLE pipe=CreateNamedPipe ("\\.\pipe\CC-"+po.dwProcessId, 0x00000003, 0x40000000,
0x00080000L, 0x00000004, 128, 0, NULL);
while (WaitNamedPipe ("\\.\pipe\CC-"+po.dwProcessId, INFINITE) == FALSE )
Sleep (300);
*edit I changed it up some..However it still hangs
PROCESS_INFORMATION po; STARTUPINFO s;
GetStartupInfo (&s);
if(CreateProcess ("c:\\s2.exe", NULL,
NULL, NULL, false, 0, NULL, NULL, &s,
&po) == FALSE) {
printf("Error %d starting CC\n", GetLastError());
exit(-1);
}
HANDLE pipe=CreateNamedPipe(pipe_name, 0x00000003,
FILE_FLAG_FIRST_PIPE_INSTANCE,
PIPE_UNLIMITED_INSTANCES,128, 128, 0,
NULL);
while(WaitNamedPipe(pipe_name, INFINITE)==FALSE)
Sleep(300);
HANDLE CC = CreateFile (pipe_name,
GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL);
bool fConnected = ConnectNamedPipe(pipe, NULL) ? TRUE :
(GetLastError() == ERROR_PIPE_CONNECTED);
if(fConnected) printf("true"); else printf("false");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能还有其他问题,但这里有两个问题:
第一,反斜杠字符在 C 和 C++ 字符串中充当转义字符,因此您需要在源代码中使用两个反斜杠才能在字符串本身中提供一个反斜杠。因此,首先,您的字符串需要是:
第二个问题是
+po.dwProcessId
部分。"\\.\pipe\CC-"
部分给出了指向静态字符串开头的指针。然后,您将 dwProcessId 的值添加到该指针。如果你的进程 ID 恰好是一个小于字符串的数字,那么它将给出一个指向字符串后面的指针(例如,如果它恰好是 2,它将生成一个指向".\\pipe 的指针\\CC-"
,跳过两个前导反斜杠。如果(更常见的情况)进程 ID 恰好是一个大于字符串长度的数字,您将得到一个指向一些您根本不拥有的内存,并且不知道它可能包含什么,这意味着您为
CreateNamedPipe
提供的名称基本上肯定是无效的,因为它无法创建命名管道。 ,WaitNamedPipe
将立即返回 false - 这正是您所观察到的行为:
我还没有测试过它,但这应该至少有一点机会成为一个。我还不确定您的命名管道逻辑是否正确,但至少这应该让您能够开始以有意义的方式进行工作。
You may have others, but two problems are right here:
First, the back-slash character acts as an escape character in C and C++ strings, so you need two backslashes in your source code to give one backslash in the string itself. So, for the start, your string needs to be:
The second problem is with the
+po.dwProcessId
part. The"\\.\pipe\CC-"
part gives a pointer to the beginning of a static string. You're then adding the value ofdwProcessId
to that pointer. If your process ID happens to be a number that's smaller than the string, that'll give a pointer to later in the string (e.g., if it happened to be 2, it would produce a pointer to".\\pipe\\CC-"
, skipping over the two leading backslashes. If (as will more often be the case) the process ID happens to be a number larger that the length of the string, you'll get a pointer to some memory you don't own at all and have no idea what it might contain.That means the name you're giving to
CreateNamedPipe
is essentially certain to be invalid. Since it fails to create the named pipe,WaitNamedPipe
will immediately return false -- exactly the behavior you've observed.You could try something more like this:
I haven't tested it, but that should stand at least a little chance of being a bit closer to working. I'm not at all sure your named pipe logic is right yet, but at least this should get you to the point that you can start to work on it in a meaningful way.
您混淆了命名管道的客户端和服务器端。服务器(管道供应商)将调用 CreateNamedPipe 创建一个管道。然后,为了等待客户端,它可以调用 ConnectNamedPipe (但要注意,如果客户端在此调用之前已经连接到它,它将立即返回 GetLastError() == ERROR_PIPE_CONNECTED。)
客户端可以调用 CreateFile(不是 CreateNamedPipe)打开实例。如果失败,则意味着服务器实例不可用,因此客户端可以调用 WaitNamedPipe 以在可用时收到通知。
MSDN 上的一个很好的参考是命名管道客户端< /a> 示例,以及多线程管道服务器< /a> 示例。
编辑:杰瑞指出的所有问题都是真实的。我指出他所指的“管道逻辑”。
You're confusing client and server sides of the named pipe. The server (vendor of the pipe) will call CreateNamedPipe to create a pipe. Then, to wait for clients, it can call ConnectNamedPipe (Though beware, if a client connected to it already before this call, it will just return immediately with GetLastError() == ERROR_PIPE_CONNECTED.)
The client side can call CreateFile (Not CreateNamedPipe) to open an instance. If that fails, it means the server instance isn't available, so the client can call WaitNamedPipe to get notified of when one is available.
A good reference on MSDN is the Named Pipe Client example, and the Multithreaded Pipe Server example.
Edit: All the problems Jerry points out are true. I am pointing out the 'pipe logic' that he's referring to.