用c ++和php命名管道

时间:2012-02-15 20:17:02

标签: php c++ ipc named-pipes

我在c ++中创建一个管道,当我尝试使用php代码写入该管道时,当我尝试获取该管道的句柄时,它会给我访问被拒绝错误。 基本上php代码调用另一个c ++ exe,它写入管道但它失败了。我认为它死于用户没有的一些安全性或访问权限(php用户)。 任何想法??

Code c ++

     hPipe = CreateNamedPipe( 
        wcPipeName,             // pipe name 
        PIPE_ACCESS_DUPLEX,       // read/write access 
         PIPE_TYPE_MESSAGE |       // message type pipe 
         PIPE_READMODE_MESSAGE |   // message-read mode 
            PIPE_WAIT,                // blocking mode 
         PIPE_UNLIMITED_INSTANCES, // max. instances  
        1024,                  // output buffer size 
        1024,                  // input buffer size 
         NMPWAIT_USE_DEFAULT_WAIT, // client time-out 
         NULL); 

用于编写的代码客户端从php

调用
hPipe = CreateFile( 
        lpszPipename,   // pipe name 
        GENERIC_READ,   // read and write access 
        0,              // no sharing 
        NULL,           // default security attributes
        OPEN_EXISTING,  // opens existing pipe 
        0,              // default attributes 
        NULL);          // no template file 

    if (hPipe == INVALID_HANDLE_VALUE) 
    {
        printf("\nCreatePipe failed\n %d \n",GetLastError()); 

        return FALSE;
    }

    //Sleep(1000);

    // Write the reply to the pipe. 
    fSuccess = WriteFile( 
        hPipe,        // handle to pipe 
        Buffer,      // buffer to write from 
        BufferSize-1,       // number of bytes to write 
        &cbWritten,   // number of bytes written 
        NULL);        // not overlapped I/O 

    FlushFileBuffers(hPipe); 
    CloseHandle(hPipe);

1 个答案:

答案 0 :(得分:2)

由于服务器和客户端进程在两个不同的用户帐户下运行,并且使用默认安全描述符创建服务器端管道建议设置允许Everyone访问的安全描述符:

// Create a security descriptor that has an an empty DACL to
// grant access to 'Everyone'
//
SECURITY_DESCRIPTOR sd;
if (0 == InitializeSecurityDescriptor(&sd,
                                      SECURITY_DESCRIPTOR_REVISION) ||
    0 == SetSecurityDescriptorDacl(&sd,
                                   TRUE,
                                   static_cast<PACL>(0),
                                   FALSE))
{
    std::cerr << "Failed: " << GetLastError() << "\n";
}
else
{

    SECURITY_ATTRIBUTES sa;
    sa.nLength              = sizeof(sa);
    sa.lpSecurityDescriptor = &sd;
    sa.bInheritHandle       = FALSE;

    hPipe = CreateNamedPipe( 
        wcPipeName,             // pipe name 
        PIPE_ACCESS_DUPLEX,       // read/write access 
         PIPE_TYPE_MESSAGE |       // message type pipe 
         PIPE_READMODE_MESSAGE |   // message-read mode 
         PIPE_WAIT,                // blocking mode 
        PIPE_UNLIMITED_INSTANCES, // max. instances  
        1024,                  // output buffer size 
        1024,                  // input buffer size 
        NMPWAIT_USE_DEFAULT_WAIT, // client time-out 
        &sa); 
}

此外,客户端使用GENERIC_READ打开管道,然后尝试WriteFile()到句柄:这需要是GENERIC_WRITEGENERIC_READ | GENERIC_WRITE