views:

56

answers:

2

I have tried creating a named pipe but getting GLE 5 (access denied Error)

#include <windows.h> 
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include "iostream.h"

//#define PIPE_ACCESS_DUPLEX 0x00000003
//#define PIPE_ACCESS_INBOUND 0x00000001
//#define PIPE_ACCESS_OUTBOUND 0x00000002
#define BUFSIZE 512

int main()
{
    HANDLE hPipe;
    LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\mynamedpipe"); 
    hPipe=CreateNamedPipe(lpszPipename,PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,PIPE_UNLIMITED_INSTANCES,BUFSIZE,BUFSIZE,0,NULL);

    if (hPipe != INVALID_HANDLE_VALUE) 
        cout<<"Valid";


      if (GetLastError() != ERROR_PIPE_BUSY) 
      {
         printf( TEXT("Could not open pipe. GLE=%d\n"), GetLastError() ); 
         return -1;
      }

    return 0;
}
+1  A: 

lPipeName is invalid, you need to escape the '\' characters correctly as in the msdn example (see here for details of the various error codes).

I'd also use defines rather than hex numbers, a pipe I use is declared with:

hPipe = CreateNamedPipe( lpszPipename, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,  
                         PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 
                        PIPE_UNLIMITED_INSTANCES, BUFSIZE, BUFSIZE, 0, NULL);

Which imo is more readable.

The MSDN code snippet is for the client side. The server side creates the pipe using CreateNamedPipe, the client side connects to the already created pipe using CreateFile.

EDIT: The first two paragraphs of the remarks section of the CreatenamedPipe man page describe why you may be getting access denied. Assuming this is the only instance of this named pipe you are creating the problem may be your permissions. Are you on Vista or Windows 7? If so make sure you are running as administrator. Otherwise you're going to have to play with your settings till you get it right.

PS : Are you calling DisconnectNamedPipe and CloseHandle when you're finished with the pipe? I'd call them even if the pipe hasn't created correctly.

Patrick
@Patrick, Now getting GLE 5 error, updated the code snipet
Subhen
I am working Win XP machine
Subhen
Tried both DisconnectNamedPipe(lpszPipename); CloseHandle(hPipe); at the end of the code before return 0; Same error
Subhen
A: 

Well I tried around a lot of things with my programme but was not able to find out why the creation is failed.

I was working on VC++ 6.0 . Then I started my Visual Studio 2008 and created a C++ project. Pasted the Code . Compiled. Got the error :

Error   1   fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory

commented #include "iostream.h".

Rebuild and that worked . Not very sure why this happened , but worked for me. Please update if you got to know why this is happening or there is a solution.

Subhen