gpt4 book ai didi

php - C++ 和 PHP 中的命名管道

转载 作者:行者123 更新时间:2023-11-30 04:29:31 25 4
gpt4 key购买 nike

我正在用 C++ 创建一个管道,当我尝试使用 PHP 代码写入该管道时,当我尝试获取该管道的句柄时,出现拒绝访问错误。基本上,php 代码调用另一个写入管道的 c++ exe,但它失败了。我认为它死于用户没有的某些安全或访问权限(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);

最佳答案

由于服务器和客户端进程在两个不同的用户帐户下运行,并且服务器端管道是使用默认安全描述符创建的,因此建议设置一个允许 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

关于php - C++ 和 PHP 中的命名管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9300665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com