Windows10 1803更新后CreateFile创建Mailslot报ERROR_BAD_NETPATH(53)
Problem Overview
Starting with the Windows 10 May 2018 Update (version 1803), calling CreateFile("\\<computername>\mailslot\<slotname>", ...) consistently returns error code 53 (ERROR_BAD_NETPATH). This happens regardless of whether the computer name is valid (even the local machine) or invalid. The only working syntax is explicitly using the local path: \\.\mailslot\<slotname>, which functions correctly.
Prior to Windows 10 1803, this remote/network mailslot path syntax worked across all Windows versions. Initial testing pointed to KB4103721, but further confirmation showed the root cause is the 1803 feature update itself.
Test Code Reproduction
Client Code (Fails with Remote/Computer Name Paths)
This client works when run without arguments (uses . as the computer name) but fails when specifying any explicit computer name:
#include <windows.h> #include <stdio.h> #include <tchar.h> LPTSTR SlotName = TEXT("\\\\%hs\\mailslot\\sample_mailslot"); BOOL WriteSlot(HANDLE hSlot, LPTSTR lpszMessage) { BOOL fResult; DWORD cbWritten; fResult = WriteFile(hSlot, lpszMessage, (DWORD) (lstrlen(lpszMessage)+1)*sizeof(TCHAR), &cbWritten, (LPOVERLAPPED) NULL); if (!fResult) { // this failure is valid if computername is not valid printf("WriteFile failed with %d.\n", GetLastError()); return FALSE; } printf("Slot written to successfully.\n"); return TRUE; } int main(int nArgs,char * arg[]) { HANDLE hFile; TCHAR szSlot[256]; _stprintf (szSlot,SlotName,nArgs > 1 ? arg[1] : "."); _tprintf(TEXT("Writing to slot %s\n"),szSlot); hFile = CreateFile(szSlot, GENERIC_WRITE, FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL); if (hFile == INVALID_HANDLE_VALUE) { // this is the failure I'm trying to debug printf("CreateFile failed with %d.\n", GetLastError()); return FALSE; } WriteSlot(hFile, TEXT("Message one for mailslot.")); WriteSlot(hFile, TEXT("Message two for mailslot.")); Sleep(5000); WriteSlot(hFile, TEXT("Message three for mailslot.")); CloseHandle(hFile); return TRUE; }
Server Code (Works with Local Path)
This server reads and displays messages sent to the local mailslot:
#include <windows.h> #include <tchar.h> #include <stdio.h> #include <strsafe.h> HANDLE hSlot; LPTSTR SlotName = TEXT("\\\\.\\mailslot\\sample_mailslot"); BOOL ReadSlot() { DWORD cbMessage, cMessage, cbRead; BOOL fResult; LPTSTR lpszBuffer; TCHAR achID[80]; DWORD cAllMessages; HANDLE hEvent; OVERLAPPED ov; cbMessage = cMessage = cbRead = 0; hEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("ExampleSlot")); if( NULL == hEvent ) return FALSE; ov.Offset = 0; ov.OffsetHigh = 0; ov.hEvent = hEvent; fResult = GetMailslotInfo( hSlot, // mailslot handle (LPDWORD) NULL, // no maximum message size &cbMessage, // size of next message &cMessage, // number of messages (LPDWORD) NULL); // no read time-out if (!fResult) { printf("GetMailslotInfo failed with %d.\n", GetLastError()); return FALSE; } if (cbMessage == MAILSLOT_NO_MESSAGE) { printf("Waiting for a message...\n"); return TRUE; } cAllMessages = cMessage; while (cMessage != 0) // retrieve all messages { // Create a message-number string. StringCchPrintf((LPTSTR) achID, 80, TEXT("\nMessage #%d of %d\n"), cAllMessages - cMessage + 1, cAllMessages); // Allocate memory for the message. lpszBuffer = (LPTSTR) GlobalAlloc(GPTR, lstrlen((LPTSTR) achID)*sizeof(TCHAR) + cbMessage); if( NULL == lpszBuffer ) return FALSE; lpszBuffer[0] = '\0'; fResult = ReadFile(hSlot, lpszBuffer, cbMessage, &cbRead, &ov); if (!fResult) { printf("ReadFile failed with %d.\n", GetLastError()); GlobalFree((HGLOBAL) lpszBuffer); return FALSE; } // Concatenate the message and the message-number string. StringCbCat(lpszBuffer, lstrlen((LPTSTR) achID)*sizeof(TCHAR)+cbMessage, (LPTSTR) achID); // Display the message. _tprintf(TEXT("Contents of the mailslot: %s\n"), lpszBuffer); GlobalFree((HGLOBAL) lpszBuffer); fResult = GetMailslotInfo(hSlot, // mailslot handle (LPDWORD) NULL, // no maximum message size &cbMessage, // size of next message &cMessage, // number of messages (LPDWORD) NULL); // no read time-out if (!fResult) { printf("GetMailslotInfo failed (%d)\n", GetLastError()); return FALSE; } } CloseHandle(hEvent); return TRUE; } BOOL WINAPI MakeSlot(LPTSTR lpszSlotName) { hSlot = CreateMailslot(lpszSlotName, 0, // no maximum message size MAILSLOT_WAIT_FOREVER, // no time-out for operations (LPSECURITY_ATTRIBUTES) NULL); // default security if (hSlot == INVALID_HANDLE_VALUE) { printf("CreateMailslot failed with %d.\n", GetLastError()); return FALSE; } return TRUE; } int main() { if (!MakeSlot(SlotName)) return FALSE; while(TRUE) { ReadSlot(); Sleep(1000); } CloseHandle(hSlot); return TRUE; }
Key Observations
- Local path works: Using
\\.\mailslot\<slotname>always succeeds in opening the mailslot for writing. - Remote/computer name paths fail: Any path using
\\<computername>\mailslot\<slotname>(even with the local machine's name) returns ERROR_BAD_NETPATH 53 on Windows 10 1803+. - Regression from prior versions: This behavior is a breaking change introduced in the 1803 feature update.
内容的提问来源于stack exchange,提问作者truefish

