开发者

WaitNamedPipe just hangs

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 )
    Slee开发者_运维问答p (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");


You may have others, but two problems are right here:

"\\.\pipe\CC-"+po.dwProcessId

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:

"\\\\.\\pipe\\CC-"

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 of dwProcessId 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:

char pipe_name[32];
sprintf(pipe_name, "\\\\.\\pipe\\CC-%d", po.dwProcessId);

HANDLE pipe = CreateNamedPipe(pipe_name, /* ... */);

WaitNamedPipe(pipe, NMPWAIT_WAIT_FOREVER);

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜