C++ 无法连接到命名管道

C++ 无法连接到命名管道,c++,windows,C++,Windows,我试图在本机应用程序和托管应用程序之间进行通信,我使用的是NamedPipe。我已经尝试了用C++来创建和打开命名管道,我到底是如何获得安全性错误的? SECURITY_ATTRIBUTES sa; sa.lpSecurityDescriptor = (PSECURITY_DESCRIPTOR)malloc(SECURITY_DESCRIPTOR_MIN_LENGTH); if (!InitializeSecurityDescriptor(sa.lpSecurityDescriptor, SEC

我试图在本机应用程序和托管应用程序之间进行通信,我使用的是NamedPipe。我已经尝试了用C++来创建和打开命名管道,我到底是如何获得安全性错误的?
SECURITY_ATTRIBUTES sa;
sa.lpSecurityDescriptor = (PSECURITY_DESCRIPTOR)malloc(SECURITY_DESCRIPTOR_MIN_LENGTH);
if (!InitializeSecurityDescriptor(sa.lpSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION))
{
    DWORD er = ::GetLastError();
}
if (!SetSecurityDescriptorDacl(sa.lpSecurityDescriptor, TRUE, (PACL)0, FALSE))
{
    DWORD er = ::GetLastError();
}
sa.nLength = sizeof sa;
sa.bInheritHandle = TRUE;
// To know the maximal size of the received data for reading from the      // pipe buffer

union maxSize
{
    UINT   _1;
};

_pipe = ::CreateNamedPipe(pipeName.c_str(), PIPE_ACCESS_INBOUND, 
                            PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 
                            PIPE_UNLIMITED_INSTANCES, 
                            sizeof maxSize, 
                            sizeof maxSize,
                            NMPWAIT_USE_DEFAULT_WAIT,
                            &sa);

if (_pipe == INVALID_HANDLE_VALUE)
{
    DWORD dwError = ::GetLastError();
}

cout<<"Connected to Pipe"<<endl;

auto pipe = CreateFile( 
     pipeName.c_str(),   // pipe name 
     GENERIC_READ | GENERIC_WRITE ,  // read and write access 
     0,              // no sharing 
     &sa,           // default security attributes
     OPEN_EXISTING,  // opens existing pipe 
     0,              // default attributes 
     NULL);          // no template file 


if (pipe == INVALID_HANDLE_VALUE)
{
    DWORD dwError = ::GetLastError();
    cout<<"Failed to open Pipe "<<dwError<<endl;
}
SECURITY\u属性sa;
sa.lpSecurityDescriptor=(PSECURITY\u DESCRIPTOR)malloc(SECURITY\u DESCRIPTOR\u MIN\u LENGTH);
if(!InitializeSecurityDescriptor(sa.lpSecurityDescriptor,安全描述符\u修订版))
{
DWORD er=::GetLastError();
}
if(!SetSecurityDescriptorDacl(sa.lpSecurityDescriptor,TRUE,(PACL)0,FALSE))
{
DWORD er=::GetLastError();
}
sa.nLength=sa的尺寸;
sa.bInheritHandle=TRUE;
//了解从//管道缓冲区读取的接收数据的最大大小
联合最大尺寸
{
UINT_1;
};
_管道=::CreateNamedPipe(pipeName.c_str(),管道访问\u入站,
管道类型信息|管道读取模式信息|管道等待,
管道\u无限\u实例,
maxSize的大小,
maxSize的大小,
NMP等待使用默认等待,
&sa);
if(_pipe==无效的_句柄_值)
{
DWORD dwError=::GetLastError();
}

cout问题是我将客户端配置为读写而不是只写

什么失败了?创建管道、连接到管道或两者都连接?是否存在无法使用默认安全属性(即
lpSecurityAttributes
参数的pass NULL)的原因?它无法连接。管道服务器应该是windows服务,因此即使使用nullptr而不是sa,我也不能使用常规安全性-我无法连接,错误为5