With the code below I am able to work with MailSlots in different customers with several computers on their private lan.
But on one of them I am not able to discover what is happening, although for sure it will be a permissions problem.
The call to CreateMailslot returns INVALID_HANDLE_VALUE and GetLastError retuns 203 "The system could not find the environment option that was entered."
Do I need to specify more options in sa1 / SECURITY_ATTRIBUTES.
The code looks fine to me (provided that 'msSlotName' is in the correct format). Are you SURE it is 'CreateMailSlot()' that is reporting error 203? That is an unusual error for it to report. AFAIK, mailslots don't use environment variables at all. The code you showed is not calling 'GetLastError()', so is it possible that you might be calling another API (directly or indirectly) after calling 'CreateMailslot()' and before calling 'GetLastError()' that might be changing the error code? When an API fails, you need to call 'GetLastError()' immediately, before calling anything else that might change the error code.
lHSlot := CreateMailslot(PChar(msSlotName), 0, lReadTimeout, @sa1);
if lHSlot = INVALID_HANDLE_VALUE then
dwErr := GetLastError; // <-- is it really 203 here?
As I suspected, you are NOT calling GetLastError() IMMEDIATELY after CreateMailslot() fails. You are performing other operations first, which are likely resetting the error code when they try to interact with the user's environment.
You need to get the error code first, THEN perform your other operations, eg:
You were true, after correcting the code, now the error is "183: Operation completed correctly"
I suspect that this is due to two of my programs in the same computer trying to create the same mailslot, (using both the same function and the same mailslot name in each program).
So, now, the new question is: How can I get the handle to the mailslot that is created in another program in the same computer.. Can I share this same mailslot.
pss wrote: You were true, after correcting the code, now the error is "183: Operation completed correctly"
There is no Win32 error code that has a text message of "Operation completed correctly". The closest would be "The operation completed successfully", which is the text message for error code 0 (ERROR_SUCCESS).
Error code 183 is ERROR_ALREADY_EXISTS ("Cannot create a file when that file already exists"). Which makes sense, if you are trying to create the same mailslot multiple times.
Per MSDN documentation
, only 1 process can have ownership of a named mailslot at a time. Subsequent attempts to create the same mailslot will fail.
pss wrote: I suspect that this is due to two of my programs in the same computer trying to create the same mailslot, (using both the same function and the same mailslot name in each program).
Yes, that would cause a conflict.
pss wrote: So, now, the new question is: How can I get the handle to the mailslot that is created in another program in the same computer.. Can I share this same mailslot.
It probably CAN be done (such as by having the creating process use DuplicateHandle() to duplicate the mailslot handle and then pass it to the sharing process via IPC). But I would not recommend it, as it introduces a race condition when both processes then try to read inbound messages from the same mailslot. And besides, further
also suggests this wouldn't work, anyway.
The real question is, why do you have multiple processes creating the same mailslot to being with? Are you running multiple instances of the same executable? You might need to consider
moving the mailslot to a separate dedicated process
and then delegate received messages to other processes as needed.