-1

我正在尝试在本机应用程序和托管应用程序之间进行通信,我正在使用 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;
}
4

1 回答 1

0

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

于 2013-09-06T19:27:58.113 回答