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

发布于 2024-09-16 09:01:35 字数 847 浏览 7 评论 0原文

我尝试创建命名管道但收到 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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肩上的翅膀 2024-09-23 09:01:35

lPipeName 无效,您需要按照 msdn 示例正确转义“\”字符(请参阅 此处了解各种错误代码的详细信息)。

我还使用定义而不是十六进制数字,我使用的管道声明为:

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 更具可读性。

MSDN 代码片段适用于客户端。服务器端使用 CreateNamedPipe 创建管道,客户端使用 CreateFile 连接到已创建的管道。

编辑:
CreatenamedPipe 手册页描述了访问被拒绝的原因。假设这是您正在创建的命名管道的唯一实例,问题可能是您的权限。您使用的是 Vista 还是 Windows 7?如果是这样,请确保您以管理员身份运行。否则你将不得不调整你的设置直到你得到正确的结果。

PS:完成管道后,您是否正在调用 DisconnectNamedPipe 和 CloseHandle ?即使管道没有正确创建,我也会调用它们。

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.

美羊羊 2024-09-23 09:01:35

好吧,我用我的程序尝试了很多东西,但无法找出创建失败的原因。

我正在研究 VC++ 6.0 。然后我启动了 Visual Studio 2008 并创建了一个 C++ 项目。粘贴代码。已编译。收到错误:

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

评论了#include“iostream.h”

重建并成功。不太清楚为什么会发生这种情况,但对我有用。如果您知道为什么会发生这种情况或有解决方案,请更新。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文