简体   繁体   English

创建命名管道时出现GLE = 5(访问被拒绝)错误

[英]Getting GLE=5 (Access Denied ) Error While creating Named Pipe

I have tried creating a named pipe but getting GLE 5 (access denied Error) 我尝试创建命名管道,但得到GLE 5(访问被拒绝错误)

#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;
}

lPipeName is invalid, you need to escape the '\\' characters correctly as in the msdn example (see here for details of the various error codes). lPipeName无效,您需要像msdn示例中一样正确转义'\\'字符(有关各种错误代码的详细信息,请参见此处 )。

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. 哪个imo更易读。

The MSDN code snippet is for the client side. MSDN代码段是针对客户端的。 The server side creates the pipe using CreateNamedPipe, the client side connects to the already created pipe using CreateFile. 服务器端使用CreateNamedPipe创建管道,客户端使用CreateFile连接到已创建的管道。

EDIT: The first two paragraphs of the remarks section of the CreatenamedPipe man page describe why you may be getting access denied. 编辑: CreatenamedPipe手册页的备注部分的前两段描述了为什么您可能会被拒绝访问。 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? 您使用的是Vista还是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? PS:完成管道后,是否要调用DisconnectNamedPipe和CloseHandle? I'd call them even if the pipe hasn't created correctly. 即使管道创建不正确,我也会打电话给他们。

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 . 我正在VC ++ 6.0上工作。 Then I started my Visual Studio 2008 and created a C++ project. 然后,我启动了Visual Studio 2008,并创建了一个C ++项目。 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" . 评论了#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. 如果您知道为什么会发生这种情况或有解决方案,请进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM