无法连接到命名管道

时间:2013-09-06 18:25:15

标签: c++ windows

我正在尝试在本机应用程序和托管应用程序之间进行通信,我正在使用NamedPipe。我首先尝试使用c ++来创建和打开namedpipe我怎么会遇到安全错误

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

1 个答案:

答案 0 :(得分:0)

问题在于我使用读写配置客户端而不是仅写入