2012-02-15 81 views
1

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

码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); 
+0

我有一个想法:发布一些代码。 :) – netcoder 2012-02-15 20:20:31

+0

你可以发布客户端和服务器的C++代码,并在什么平台上运行?这两个进程是否以相同的用户身份运行? – hmjd 2012-02-15 20:22:38

+0

没有php代码正在运行,因为iis用户 – kunal 2012-02-15 20:26:14

回答

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

+0

感谢很多队友像魔术一样工作。 – kunal 2012-02-15 20:57:07