Jump to content
trancexx

MailSlot

Recommended Posts

Hey everyone!

I was hoping someone might be able to help me with this.  I am pretty sure this great UDF 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.

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!

Share this post


Link to post
Share on other sites

To be more specific, here is there error that I am encountering on the Receiver:

 

Failed to create new account!

Probably one using that 'address' already exists.

Share this post


Link to post
Share on other sites

Unfortunately same message is coming up no matter how I try :( I'm attempting to look up how to use it over a network, but everything I'm finding basically gets summed up as "yeah it's super easy" and never actually elaborates on it.  Is anyone out there using this over their local network, and if so, how??

Thank you for your help!

Share this post


Link to post
Share on other sites

Sure thing @dmob:

#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

 

Share this post


Link to post
Share on other sites
On 9/16/2019 at 11:09 AM, maloysius said:

Global Const $sMailSlotName = "\\WORKGROUP\mailslot\Test" Global $hMailSlot = _MailSlotCreate($sMailSlotName)

why, oh, why. What's wrong with declaring on your PC a mailslot ?, why the workgroup ? :mad2:

On your PC you'd use "\\.\" and if you wanna broadcast to the workgroup, use the same everything but start with "\\*\", that is all ;) 

Share this post


Link to post
Share on other sites
Posted (edited)

Quick note to anyone who knows nothing about MailSlots (like me)... make sure to name the MailSlot like this "\\.\mailslot\" then whatever you want... Otherwise it will fail. Repeatedly. :mad2:

On the other hand, thank you very much for this UDF, it's very helpful! (Once I understand it)

Edit: Hmm... maybe I should read the function documentation better 😐

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...