跨设备部署Named Pipe时出现1326 STATUS_LOGON_FAILURE错误求助
跨设备Named Pipe通信1326 STATUS_LOGON_FAILURE错误解决方案
问题描述
我是C++新手,尝试在台式机和笔记本之间实现Named Pipe通信,但运行程序时始终触发1326 STATUS_LOGON_FAILURE错误。同一设备内修改客户端IP为本地地址后代码可正常运行,但跨设备连接时就会报错。已确认两台设备均开启文件共享。
服务端代码
#include <iostream> #include <windows.h> #include <wchar.h> #include <wctype.h> #include <lmuse.h> #include <lm.h> using namespace std; int main(int argc, const char **arv){ wcout << "Creating an instance of a named pipe..." << endl; PSECURITY_DESCRIPTOR psd; SECURITY_ATTRIBUTES sa; psd = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH); InitializeSecurityDescriptor(psd, SECURITY_DESCRIPTOR_REVISION); SetSecurityDescriptorDacl(psd, true, NULL, false); sa.nLength = sizeof(sa); sa.bInheritHandle = true; sa.lpSecurityDescriptor = psd; HANDLE pipe = CreateNamedPipe( TEXT("\\\\.\\pipe\\my_pipe"), PIPE_ACCESS_OUTBOUND, PIPE_TYPE_BYTE, 1, 0, 0, 0, NULL ); if(pipe == NULL || pipe == INVALID_HANDLE_VALUE){ wcout << "Failed to create outbound pipe instance."; system("pause"); return 1; } wcout << "Waiting for a client to connect to the pipe..." << endl; BOOL result = ConnectNamedPipe(pipe, NULL); if(!result){ wcout << "Failed to make connection on named pipe." << endl; CloseHandle(pipe); system("pause"); return 1; } wcout << "Sending data to pipe..." << endl; wchar_t data[255]; printf("What data do you want to pass?\n"); wscanf(L"%l[^\n]%*l[^\n]", data); while((getchar()) != 'n'); DWORD numBytesWritten = 0; result = WriteFile( pipe, data, wcslen(data) * sizeof(wchar_t), &numBytesWritten, NULL ); if(result){ wcout << "Number of bytes sent : " << numBytesWritten << endl; } else { wcout << "Failed to send data." << endl; } CloseHandle(pipe); wcout << "Done" << endl; system("pause"); return 0; }
客户端代码
#include <iostream> #include <windows.h> using namespace std; int main(int argc, const char ** argv){ wcout << "Connecting to pipe..." << endl; HANDLE pipe = CreateFile( TEXT("\\\\x.x.x.x\\pipe\\my_pipe"), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if(pipe == INVALID_HANDLE_VALUE){ wcout << "Failed to connect to pipe." << endl; printf("\nError occured while writing to the server %d: ", GetLastError()); system("pause"); return 1; } wcout << "Reading data from pipe..." << endl; wchar_t buffer[128]; DWORD numBytesRead = 0; BOOL result = ReadFile( pipe, buffer, 127 * sizeof(wchar_t), &numBytesRead, NULL ); if(result){ buffer[numBytesRead / sizeof(wchar_t)] = '\n'; wcout << "Number of bytes read: " << numBytesRead << endl; wcout << "Message: " << buffer << endl; } else { wcout << "Failed to read data from the pipe" << endl; } CloseHandle(pipe); wcout << "Done." << endl; system("pause"); return 0; }
解决步骤
1. 修复服务端安全描述符传递问题
你创建了允许匿名访问的安全描述符,但未将其传入CreateNamedPipe,导致管道使用默认安全策略拒绝跨设备连接。修改CreateNamedPipe的最后一个参数为&sa:
HANDLE pipe = CreateNamedPipe( TEXT("\\\\.\\pipe\\my_pipe"), PIPE_ACCESS_OUTBOUND, PIPE_TYPE_BYTE, 1, 0, 0, 0, &sa // 传入配置好的安全属性 );
2. 处理跨设备身份验证
跨设备Named Pipe通信需要信任凭证:
- 确保两台设备在同一工作组或域中
- 客户端使用服务端存在的用户账户(用户名和密码一致),可通过
WNetAddConnection2提前建立信任连接,或在CreateFile时指定包含凭证的安全属性 - 临时测试可关闭两台设备防火墙(注意安全风险),或在防火墙中添加允许Named Pipe通信的规则
3. 代码细节修复
- 服务端
wscanf格式错误,应改为wscanf(L"%[^\n]", data); - 服务端换行符判断错误,
while((getchar()) != 'n');应改为while((getchar()) != '\n');
内容的提问来源于stack exchange,提问作者Scott_b404
相关产品推荐
相关产品推荐

