Jump to content

Using the MailSlot UDF Over Network


maloysius
 Share

Recommended Posts

Hey everyone!

I was hoping someone might be able to help me with this.  I am pretty sure this great UDF I found by Trancexx is going to be the solution to my current script problems.  Basically, I need to be able to have a central computer send a message to every computer in a workgroup.  I am using the given script examples, which are for locally testing on the same computer ("\\.\mailslot\Test") and it works great.  After reading up on it a bit, I saw that in order to broadcast to everything in a workgroup, you would simply need to replace "." with the name of the workgroup ("\\Workgroup\mailslot\Test").  However, when I do this, the Sender comes up fine, but the Receiver will not start, citing that it's unable to create the mailslot because it likely already exists.  I am using the verbatim scripts from the example, with just the mailslot name changed.

Sender:

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#include "MailSlot.au3"

Global Const $sMailSlotName = "\\WORKGROUP\mailslot\Test"


Global $hGUI = GUICreate("MailSlot Demo Sender", 450, 400, @DesktopWidth / 4 - 225, -1, -1, 8) ; $WS_EX_TOPMOST

GUICtrlCreateLabel("Message text:", 10, 22, 100, 25)
GUICtrlSetColor(-1, 0x0000CC)
GUICtrlSetFont(-1, 11)

Global $hEdit = GUICtrlCreateEdit("", 15, 50, 300, 340)

Global $hButtonSend = GUICtrlCreateButton("&Send Mail", 330, 100, 100, 25)

Global $hButtonCloseApp = GUICtrlCreateButton("&Exit", 330, 350, 100, 25)

GUISetState()


While 1

    Switch GUIGetMsg()
        Case - 3, $hButtonCloseApp
            Exit
        Case $hButtonSend
            _SendMail($sMailSlotName)
    EndSwitch

WEnd


; Wrapper function:

Func _SendMail($sMailSlotName)

    Local $sDataToSend = GUICtrlRead($hEdit)

    If $sDataToSend Then
        _MailSlotWrite($sMailSlotName, $sDataToSend);, 1)
        Switch @error
            Case 1
                MsgBox(48, "MailSlot demo error", "Account that you try to send to likely doesn't exist!", 0, $hGUI)
            Case 2
                MsgBox(48, "MailSlot demo error", "Message is blocked!", 0, $hGUI)
            Case 3
                MsgBox(48, "MailSlot demo error", "Message is send but there is an open handle left." & @CRLF & "That could lead to possible errors in future", 0, $hGUI)
            Case 4
                MsgBox(48, "MailSlot demo error", "All is fucked up!" & @CRLF & "Try debugging MailSlot.au3 functions. Thanks.", 0, $hGUI)
            Case Else
                MsgBox(64, "MailSlot demo", "Sucessfully sent!", 0, $hGUI)
        EndSwitch
        GUICtrlSetData($hEdit, "")
    Else
        MsgBox(64, "MailSlot demo", "Nothing to send.", 0, $hGUI)
    EndIf

EndFunc   ;==>_SendMail

Receiver:

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#include "MailSlot.au3"

Global Const $sMailSlotName = "\\WORKGROUP\mailslot\Test"

Global $hMailSlot = _MailSlotCreate($sMailSlotName)
MsgBox(48, "MailSlot demo", $hMailSlot, 0)
;Global $hMailSlot = $sMailSlotName
If @error Then
    MsgBox(48 + 262144, "MailSlot", "Failed to create new account!" & @CRLF & "Probably one using that 'address' already exists.")
    Exit
EndIf

Global $iNumberOfMessagesOverall

Global $hGUI = GUICreate("MailSlot Demo Receiver", 450, 400, 3 * @DesktopWidth / 4 - 225, -1, -1, 8) ; $WS_EX_TOPMOST

GUICtrlCreateLabel("Message text:", 10, 22, 100, 25)
GUICtrlSetColor(-1, 0x0000CC)
GUICtrlSetFont(-1, 11)

Global $hEdit = GUICtrlCreateEdit("", 15, 50, 300, 340)

Global $hButtonRead = GUICtrlCreateButton("Read &Mail", 330, 50, 100, 25)

Global $hButtonCheckCount = GUICtrlCreateButton("&Check Mail Count", 330, 100, 100, 25)

Global $hButtonCloseAccount = GUICtrlCreateButton("Close Mail &Account", 330, 150, 100, 25)

Global $hButtonRestoreAccount = GUICtrlCreateButton("&Restore Account", 330, 200, 100, 25)

Global $hButtonCloseApp = GUICtrlCreateButton("&Exit", 330, 350, 100, 25)

GUISetState()


While 1

    Switch GUIGetMsg()
        Case - 3, $hButtonCloseApp
            Exit
        Case $hButtonRead
            If $hMailSlot Then
                _ReadMessage($hMailSlot)
            Else
                MsgBox(48, "MailSlot demo", "No account is available!", 0, $hGUI)
            EndIf
        Case $hButtonCheckCount
            If $hMailSlot Then
                _CheckCount($hMailSlot)
            Else
                MsgBox(48, "MailSlot demo", "No account is available!", 0, $hGUI)
            EndIf
        Case $hButtonCloseAccount
            If $hMailSlot Then
                _CloseMailAccount($hMailSlot)
            Else
                MsgBox(64, "MailSlot demo", "No account is available!", 0, $hGUI)
            EndIf
        Case $hButtonRestoreAccount
            If $hMailSlot Then
                MsgBox(64, "MailSlot demo", "This account already exists!", 0, $hGUI)
            Else
                _RestoreAccount($sMailSlotName)
            EndIf
    EndSwitch

WEnd


; Wrapper functions:

Func _ReadMessage($hHandle)

    Local $iSize = _MailSlotCheckForNextMessage($hHandle)

    If $iSize Then
        Local $sData = _MailSlotRead($hMailSlot, $iSize, 1)
        $iNumberOfMessagesOverall += 1
        GUICtrlSetData($hEdit, "Message No" & $iNumberOfMessagesOverall & " , Size = " & $iSize & " :" & @CRLF & @CRLF & $sData)
    Else
        MsgBox(64, "Nothing read", "MailSlot is empty", 0, $hGUI)
    EndIf

EndFunc   ;==>_ReadMessage


Func _CheckCount($hHandle)

    Local $iCount = _MailSlotGetMessageCount($hHandle)
    Switch $iCount
        Case 0
            MsgBox(64, "Messages", "No new messages", 0, $hGUI)
        Case 1
            MsgBox(64, "Messages", "There is 1 message waiting to be read.", 0, $hGUI)
        Case Else
            MsgBox(64, "Messages", "There are " & $iCount & " messages waiting to be read.", 0, $hGUI)
    EndSwitch

EndFunc   ;==>_CheckCount


Func _CloseMailAccount(ByRef $hHandle)

    If _MailSlotClose($hHandle) Then
        $hHandle = 0
        MsgBox(64, "MailSlot demo", "Account succesfully closed.", 0, $hGUI)
    Else
        MsgBox(48, "MailSlot demo error", "Account could not be closed!", 0, $hGUI)
    EndIf

EndFunc   ;==>_CloseMailAccount


Func _RestoreAccount($sMailSlotName)

    Local $hMailSlotHandle = _MailSlotCreate($sMailSlotName)

    If @error Then
        MsgBox(48, "MailSlot demo error", "Account could not be created!", 0, $hGUI)
    Else
        MsgBox(64, "MailSlot demo", "New account with the same address successfully created!", 2, $hGUI)
        $hMailSlot = $hMailSlotHandle ; global var
    EndIf

EndFunc   ;==>_RestoreAccount

 

Does anyone have any ideas on why this might be?  I am still pretty new to this whole thing, so apologies if it's something extremely simple that I'm overlooking.  Is there more than one change here that I need to make to the script for it to function over a network?

Thanks in advance for any advice!

Link to comment
Share on other sites

Hello maloysius,

After testing this on my two local computers (luckily I actually set them up on the same WORKGROUP), I was able to get this working.

Here's the issue, the 'Receiver' needs to keep the name as: "\\.\mailslot\Test", and the 'Sender' is: "\\WORKGROUP\mailslot\Test". That allowed everything to work correctly for me, and I could receive messages from the sender to the receiver computer.

image.png.c14802211c8139036d3e8e0e44e2084c.png

 

This site gave me the information that I needed: 

Quote

When a server program creates a mailslot, the mailslot name must have the following form:

\\.\mailslot\[path]name

.......

To write a message to a mailslot, a client program "opens" that mailslot by its name. The client uses the following form for the mailslot name:

\\ComputerName\mailslot\[path]name

 

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...