tags:

views:

41

answers:

2

Quoted here:

hPipe = CreateFile( 
         lpszPipename,   // pipe name 
         GENERIC_READ |  // read and write access 
         GENERIC_WRITE, 
         0,              // no sharing 
         NULL,           // default security attributes
         OPEN_EXISTING,  // opens existing pipe 
         0,              // default attributes 
         NULL);  

How can the above code make sure that it actually opens a pipe and not an existing hard disk file?

BTW, how can I open a persistent pipe so that can be used multiple times?

+2  A: 

A pipe name must start with \\.\pipe\ (or more generally, \\servername\pipe\). A file on a hard drive never will have that prefix, so you just need to ensure that the name has that prefix. Alternatively, you can use CallNamedPipe, which (I'm pretty sure) will fail if passed a name of something other than a named pipe.

I'm not sure what your second question is intended to ask -- you can send as many messages/as much data over a pipe as you wish. If you mean opening a single named pipe on the server that can be used by multiple clients, the last parameter when you call CreateNamedPipe specifies the maximum number of concurrent instances (clients, in essence) allowed.

Jerry Coffin
+2  A: 

The way it opens a pipe is the prefix in the filename. It must be \\\\.\\pipe\\pipename, which isn't a legal filename (for an actual file, you'd usually start with a relative path or a drive letter, colon and slash, except in the rare case of opening using a device ID or some such). Seeing as it can't be opening a file, it then must open a pipe.

To open a persistent pipe, I'm not sure whether you want to use it from multiple apps at once (if so, you can open it from each, but be careful not to block it up) or make it persist between sessions. If the latter, I'm not entirely sure, but I've never heard of a way to keep the pipe open when no program has it open (similar in some ways to keeping a file open, I'd presume). It's possible that as long as you don't CloseHandle it, it will stay open until the next reboot. Worth testing, at least.

If you need persistent data transfer between applications or sessions, you may want to find a more reliable (and more flexible) method, however. Pipes can easily get blocked up if something unexpected happens (which should always be expected when multiple threads/processes are working together), which can freeze one or both connected applications (often to the point where even killing them in a debugger is difficult).

peachykeen