问题
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