Mailslots on Windows 95 and Windows 98 have three limitations that you should be aware of: 8.3-character name limits, inability to cancel blocking I/O requests, and timeout memory leaks.
Windows 95 and Windows 98 silently limit mailslot names to an 8.3-character name format. This causes interoperability problems between Windows 95 or Windows 98 and Windows NT or Windows 2000. For example, if you create or open a mailslot with the name \\.\Mailslot\Mymailslot, Windows 95 will actually create and reference the mailslot as \\.\Mailslot\Mymailsl. The CreateMailslot and CreateFile functions succeed even though name truncation occurs. If a message is sent from Windows 2000 to Windows 95 or vice versa, the message will not be received because the mailslot names do not match. If both the client and the server are running on Windows 95 machines, there isn't a problem—the name is truncated on both the client and the server. An easy way to prevent interoperability problems is to limit mailslot names to eight characters or less.
Windows 95 and Windows 98 also have a problem with canceling blocking I/O requests. Mailslot servers use the ReadFile function to receive data. If a mailslot is created with the MAILSLOT_WAIT_FOREVER flag, read requests block indefinitely until data is available. If a server application is terminated when there is an outstanding ReadFile request, the application hangs forever. The only way to cancel the application is to reboot Windows. A possible solution is to have the server open a handle to its own mailslot in a separate thread and send data to break the blocking read request. Figure 3-3 demonstrates this solution in detail.
Figure 3-3. Revised mailslot server
// Server2.cpp #include <windows.h> #include <stdio.h> #include <conio.h> BOOL StopProcessing; DWORD WINAPI ServeMailslot(LPVOID lpParameter); void SendMessageToMailslot(void); void main(void) { DWORD ThreadId; HANDLE MailslotThread; StopProcessing = FALSE; MailslotThread = CreateThread(NULL, 0, ServeMailslot, NULL, 0, &ThreadId); printf("Press a key to stop the server\n"); _getch(); // Mark the StopProcessing flag to TRUE so that when ReadFile // breaks, our server thread will end StopProcessing = TRUE; // Send a message to our mailslot to break the ReadFile call // in our server SendMessageToMailslot(); // Wait for our server thread to complete if (WaitForSingleObject(MailslotThread, INFINITE) == WAIT_FAILED) { printf("WaitForSingleObject failed with error %d\n", GetLastError()); return; } } // // Function: ServeMailslot // // Description: // This function is the mailslot server worker function to // process all incoming mailslot I/O // DWORD WINAPI ServeMailslot(LPVOID lpParameter) { char buffer[2048]; DWORD NumberOfBytesRead; DWORD Ret; HANDLE Mailslot; if ((Mailslot = CreateMailslot("\\\\.\\mailslot\\myslot", 2048, MAILSLOT_WAIT_FOREVER, NULL)) == INVALID_HANDLE_VALUE) { printf("Failed to create a MailSlot %d\n", GetLastError()); return 0; } while((Ret = ReadFile(Mailslot, buffer, 2048, &NumberOfBytesRead, NULL)) != 0) { if (StopProcessing) break; printf("Received %d bytes\n", NumberOfBytesRead); } CloseHandle(Mailslot); return 0; } // // Function: SendMessageToMailslot // // Description: // The SendMessageToMailslot function is designed to send a // simple message to our server so we can break the blocking // ReadFile API call // void SendMessageToMailslot(void) { HANDLE Mailslot; DWORD BytesWritten; if ((Mailslot = CreateFile("\\\\.\\mailslot\\myslot", GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { printf("CreateFile failed with error %d\n", GetLastError()); return; } if (WriteFile(Mailslot, "STOP", 4, &BytesWritten, NULL) == 0) { printf("WriteFile failed with error %d\n", GetLastError()); return; } CloseHandle(Mailslot); } |
The final problem with Windows 95 and 98 worth mentioning is memory leaks, which can occur when you're using timeout values on mailslots. When you create a mailslot using the CreateMailslot function with a timeout value greater than 0, the ReadFile function leaks memory when the timeout expires and the function returns FALSE. After many calls to the ReadFile function, the system becomes unstable and subsequent ReadFile calls whose timers expire start returning TRUE. As a result, the system is no longer able to execute other MS-DOS applications. To work around this, create the mailslot with a timeout value of either 0 or MAILSLOT_WAIT_FOREVER. This will prevent an application from using the timeout mechanism, which causes the actual memory leak.
The Microsoft knowledge base documents the following problems and limitations. You can access the knowledge base at http://support.microsoft.com/support/search. We briefly describe each issue here.