CreateNamedPipe ERROR_INVALID_NAME

对着背影说爱祢 提交于 2021-01-28 03:10:23

问题


Code snippet:

void RunThread(void* unused_args)
{
    PSECURITY_DESCRIPTOR sdsc;
    ULONG size;
    ConvertStringSecurityDescriptorToSecurityDescriptor("S:(ML;;NW;;;LW)", SDDL_REVISION_1, &sdsc, &size);
    SECURITY_ATTRIBUTES sa;
    sa.nLength = sizeof(sa);
    sa.bInheritHandle = false;
    sa.lpSecurityDescriptor = sdsc;
    HANDLE pipe = CreateNamedPipe("\\.\pipe\mmaivpc_test_pipe", PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE, 255, 1024, 1024, 0, &sa);
    DWORD error = GetLastError();
}

If you haven't figured it out from the function name, this function is getting called by _beginthread. GetLastError() is returning ERROR_INVALID_NAME and I can not figure out why.


回答1:


You need to escape the backslashes in the string literal being used for the pipe name:

HANDLE pipe = CreateNamedPipe("\\\\.\\pipe\\mmaivpc_test_pipe",
                              PIPE_ACCESS_DUPLEX, 
                              PIPE_TYPE_BYTE, 255, 1024, 1024, 0, &sa);



回答2:


You should escape your back slashes :)




回答3:


If you used forward slashes / in your paths your code would be more readable, and you would avoid errors like this.

Not many people seem to know that windows accepts both /and \ as the directory separator.




回答4:


Yes, the same way for network shares also , like \\server1 , we have to represent in c++ as \\\\server1 to escape '\' we have to use one more '\'




回答5:


Wrap an argument lpName into TEXT(), like this:

HANDLE pipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\mmaivpc_test_pipe"), PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE, 255, 1024, 1024, 0, &sa);

and the name must be in format \\\\.\\pipe\\your_pipe_name. Name of a pipe is not case sensitive.

Here is good example https://msdn.microsoft.com/en-us/library/windows/desktop/aa365588(v=vs.85).aspx



来源:https://stackoverflow.com/questions/15454578/createnamedpipe-error-invalid-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!