CreateNamedPipe导致ERROR_ACCESS_DENIED?

时间:2013-01-14 03:34:51

标签: c++ winapi named-pipes

我需要创建一个命名管道,用于客户端和服务器之间的通信(在同一主机中),这里是代码:

WCHAR wszPipeName[MAX_FILE_LENGTH];
swprintf_s(wszPipeName, MAX_FILE_LENGTH, L"\\\\.\\pipe\\TEST%d", uniqueID);
pipe = CreateNamedPipe(
           wszPipeName, // name of the pipe
           PIPE_ACCESS_DUPLEX,
       PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_NOWAIT,
       1,
           MAX_MSG_SIZE, 
           MAX_MSG_SIZE , //inbound buffer
           MAX_READ_DATA_TIMEOUT,
           NULL // use default security attributes
       );

处理程序返回总是INVALID_HANDLE_VAULE,错误是ERROR_ACCESS_DENIED。

这里有什么不对吗?它在Windows 7/8上运行。

由于

2 个答案:

答案 0 :(得分:0)

找到原因,这是因为安全限制。提供合适的安全描述符后,它就可以了!

答案 1 :(得分:0)

这是Python代码,但这将为本地用户设置安全描述符,并拒绝远程用户:

dacl = ACL()

# Deny NT AUTHORITY\NETWORK SID
sid = CreateWellKnownSid(WinNetworkSid)
dacl.AddAccessDeniedAce(ACL_REVISION, GENERIC_ALL, sid)

# Allow current user SID
token = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY)
sid = GetTokenInformation(token, TokenUser)[0]
dacl.AddAccessAllowedAce(ACL_REVISION, GENERIC_READ | GENERIC_WRITE, sid)

security_descriptor = SECURITY_DESCRIPTOR()
security_descriptor.SetSecurityDescriptorDacl(True, dacl, False)

security_attributes = SECURITY_ATTRIBUTES()
security_attributes.SECURITY_DESCRIPTOR = security_descriptor

pipe = CreateNamedPipe(
    <your other params here>
    security_attributes
)
相关问题