• Page:
  • 1
  • 2

TOPIC: Trouble creating Mailslots

Trouble creating Mailslots 2 months 3 weeks ago #14581

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.
var
  sd: SECURITY_DESCRIPTOR;
  sa1: SECURITY_ATTRIBUTES;
  lHSlot: THandle;

begin
  InitializeSecurityDescriptor(@sd, SECURITY_DESCRIPTOR_REVISION);
  SetSecurityDescriptorDacl(@sd, True, nil, False);
  sa1.lpSecurityDescriptor := @sd;
  sa1.bInheritHandle := False;
  lHSlot := CreateMailslot(PChar(msSlotName), 0, lReadTimeout, @sa1);
end;

Please Log in or Create an account to join the conversation.

Trouble creating Mailslots 2 months 3 weeks ago #14600

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
begin
  dwErr := GetLastError; // <-- is it really 203 here?
  ...
end;

Please Log in or Create an account to join the conversation.

Trouble creating Mailslots 2 months 3 weeks ago #14604

Sorry for the inconcretion, I intended to be as concise as possible.
The slotnane is '\\.\mailslot\amagenda'
The code checking the error is
  lHSlot := CreateMailslot(PChar(msSlotName), 0, lReadTimeout, @sa1);
  If lHSlot = INVALID_HANDLE_VALUE Then
  begin
    sTx := '    ' + GNComputerName + '  (' + DateTimeToStr(Now) + ')  MSlotCrear: Error creando mailslot: ';
    sTx := sTx + IntToStr(GetLastError) + ': ';
    sTx := sTx + SysErrorMessage(GetLastError);
    WriteDbgLine(sTx);
  end

Variable sTx is (in spanish):
PC-SECRE (28/06/2018 8:00:16) MSlotCrear: Error creando mailslot: 203: El sistema no encontró la opción de entorno especificada

Please Log in or Create an account to join the conversation.

Trouble creating Mailslots 2 months 3 weeks ago #14613

pss wrote: The code checking the error is


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:
lHSlot := CreateMailslot(PChar(msSlotName), 0, lReadTimeout, @sa1);
If lHSlot = INVALID_HANDLE_VALUE Then
begin
  dwErr := GetLastError; // <-- add this variable!
  sTx := '    ' + GNComputerName + '  (' + DateTimeToStr(Now) + ')  MSlotCrear: Error creando mailslot: ';
  sTx := sTx + IntToStr(dwErr) + ': ';
  sTx := sTx + SysErrorMessage(dwErr);
  WriteDbgLine(sTx);
end

Alternatively, you might consider using SysUtils.Format() instead:
lHSlot := CreateMailslot(PChar(msSlotName), 0, lReadTimeout, @sa1);
If lHSlot = INVALID_HANDLE_VALUE Then
begin
  dwErr := GetLastError; // <-- add this variable!
  sTx := Format('    %s  (%s)  MSlotCrear: Error creando mailslot: %d: %s',
    [GNComputerName, DateTimeToStr(Now), dwErr, SysErrorMessage(dwErr)]);
  WriteDbgLine(sTx);
end

Please Log in or Create an account to join the conversation.

Last Edit: by Remy.

Trouble creating Mailslots 2 months 1 week ago #14824

Remy,
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.

Please Log in or Create an account to join the conversation.

Trouble creating Mailslots 2 months 1 week ago #14853

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 MSDN documentation 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.

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
  • 2
Moderators: April Anne