It is quite easy to use mailslots by means of calls to functions in kernel32.dll. For interprocess com's they have the avantage over ad-hoc methods, like using a conventional file, that messages are first-in, first-out, so there is no need for contortions to prevent overwriting unread messages, etc. Note the caveat in the MSDN docs for mailslots that messages are sent as datagrams and hence can be lost without notification. Two-way communication requires that each end act a both a server and a client (each end with a separate mailslot).
I've tried using AHK fileappend to send to a mailslot, but I can't get it to work. Interestingly I have been able to use command line
copy (name of small file) \\.\mailslot\TestSlot1
The client can be on the same computer as the server, or on a different one. Mailslots can be used in any conventional or scripting language that allows use of Windows functions , so they are applicable to AHK to AHK or to C/C++, C#, VB.net, etc. A very simple VBScript mailslot client follows the AHK scripts.
When the mailslot server and client are on the same computer, messages can be very long. If the server and client are on different computers, the maximum message length is about 420 characters (MSDN docs inconsistent). If a message is too long, it is lost completely.
A mailslot client can broadcast as messsage to all computers in a domain simply by using * for the computer name. There is no problem with computers that don't have a server for the mailslot running - it appears that the message simply goes into the bit bucket for those computers.
Here are simple scripts for a mailslot server (actually the receiver of messages) and a client.
I'm not generally a big fan of using one line per DLLcall parameter, but it does make adding comments easier. If you haven't used DLLcall before, read all of the AHK Help for DLLcall carefully to learn how to match AHK types like UInt, UIntP, str, etc to parameters specified for Windows function calls.
SERVER script:
;Mailslot Server (server is actually the RECIPIENT of mail messages; client sends them) ;MSDN says Windows functions used all Windows 2K or higher (DLLs in kernel32) ;Tested only under XP Pro with SP3 ;This script has no hotkeys or user interface, so it must be closed using Exit in ;menu from right click of its notification area icon ;A proper implementation should use CloseHandle before exiting or when done with the mailslot /* USEFUL LINKS: http://msdn.microsoft.com/en-us/library/aa365147(v=VS.85).aspx CreateMailslot Function http://msdn.microsoft.com/en-us/library/aa365467(v=VS.85).aspx ReadFile Function http://msdn.microsoft.com/en-us/library/ms681381(v=VS.85).aspx System Error Codes http://msdn.microsoft.com/en-us/library/aa365785(v=VS.85).aspx Using mailslots (example in C/C++) */ #NoEnv ;"adding the line #NoEnv anywhere in the script improves DllCall's ; performance when unquoted parameter types are used (e.g. int vs. "int")." ;mailslot name MUST begin with "\\.\mailslot\", since it is created on local machine ;Name can include "pseudopath" e.g. \\.\mailslot\MainPostOffice\FredsBoxes\Box123 SlotName = \\.\mailslot\TestSlot1 ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~CREATE MAILSLOT SlotHandle := DLLCall("CreateMailslot" ;return is handle (integer) to the mailslot ,str, SlotName ,UInt, 0 ;using zero allows max message size ,UInt, 0 ;0 return immediately if no message; otherwise ms to wait, -1 for wait forever ,UInt, 0) ;security attributes if errorlevel ;this is an error reported by AHK in attempting the call { MsgBox An error occured in the AHK call to CreatMailslot.`nExiting. exitapp } else { if (a_lasterror <> 0) ;error reported by the CreateMailslot function itself (not AHK) { clipboard = http://msdn.microsoft.com/en-us/library/ms681381(v=VS.85).aspx MsgBox CreateMailSlot function returned an error.`nSystem Error Code: %a_lasterror%`n`nLink to error codes is on clipboard.`n`nExiting exitapp } else MsgBox Mailslot created with handle %SlotHandle% } ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;Mailslot is now ready to receive incoming mail from client application ;Reading from the mailslot is done using Windows ReadFile function ;Note that the mailslot was created with zero for wait time, so an attempted read will return immediately ;even if the mailslot is empty. ;Windows function GetMailslotInfo can be use to check, in advance of reading, how many messages are in ;the slot and how large the next message is ;MSDN doc's are inconsistent with regard to size limit for messages sent between computers on a network. ;One place says 400 for broadcast, another says 424 for messages between computers. ;This server and companion client will work for up to 420 under Windows XP. ;Messages sent from one process to another on the same computer can be much longer, with the limit set ;Max Msg Size at slot creation VarSetCapacity(ReadBuffer, 512, 0) loop ;a very simple loop to check the mailslot periodically { BytesRead := MailslotRead(SlotHandle) If (BytesRead > 0) msgbox A message was found in the mailslot:`n`n%ReadBuffer% sleep, 300 } MailslotRead(SlotHandle) ;reads one message at a time { global ReadBuffer ;could/should be passed as ByRef parameter BytesToRead := 500 DLLCall("ReadFile" ,UInt, slotHandle ;the handle from CreateMailSlot ,str , readBuffer ;see AHK DLLCall Help for why str used here ,UInt, BytesToRead ,UIntP, BytesActuallyRead ,UInt, 0) ;pointer to Overlapped structure, not used, so null if errorlevel Msgbox Error in ReadFile operation`nErrorLevel: %errorlevel% else return BytesActuallyRead }CLIENT script:
;Mailslot Client - the client for a mailslot is the process that SENDS to a mailslot that has been created ;by a mailslot server process ;Error checking pretty minimal - see Mailslot server script for better checking & more details of ;Windows error messages ;Sending to a mailslot is essentially the same as writing to a file ;SlotName, below, must exactly match the mailslot name created by the server EXCEPT ;if the client is running on a different computer the dot must be replaced by the name ;of the SERVER script's computer e.g. \\LabComputer1\mailslot\TestSlot1 ;A 'star' * can be used in place of computer name to broadcast to all computers on domain. SlotName = \\.\mailslot\TestSlot1 mailslotHandle := DllCall("CreateFile" ,Str , SlotName ;of form \\HostName\pipe\PipeName ,UInt, 0x40000000 ;Generic_Write access ,UInt, 3 ;0 = no sharing; 3 = read & write sharing ,UInt, 0 ;Security Attributes (a pointer; 0 here actually means NULL) ,UInt, 3 ;Creation Disposition (3 = OPEN_EXISTING) ,UInt, 0 ;Flags And Attributes ,UInt, 0) ;Template File (no template used; 0 means NULL; ignored for existing file, in any case) if errorlevel MsgBox An error occured in AHK call to DLL CreateFile else If (a_lasterror <> 0) ;error from DLL itself - see Server script for more info on errors { MsgBox Last error: %a_lasterror% exitapp } ;This script just sends a couple of messages, then exits message = This is a test messasge sent to a mailslot. The last character is a star. * SendToMailslot(mailslothandle, message) message = ( This is a multiline message that was sent without waiting for the first one to be read. ) SendToMailslot(mailslothandle, message) return SendToMailslot(slotHandle, StringToWrite) { DllCall("WriteFile" , uint, slotHandle , str, StringToWrite , uint, StrLen(StringToWrite)+1 ;+1 so terminating NULL is included , uintp,0, uint,0) If errorlevel MsgBox WriteFile failed.`nError level: %ErrorLevel%`nLast error: %A_LastError% }
A very simple client in VBScript
A script of this sort makes it extremely simple to communicate from VBScript to an AutoHotkey script.
'An very simple Mailslot client with VBScript 'No error checking 'Just demonstrates sending a single line Set FSO = CreateObject("Scripting.FileSystemObject") FileName = "\\.\mailslot\TestSlot1" set SlotFile = fso.CreateTextFile(FileName) 'use Write, not Writeline 'Null-terminate strings, if that is what mailslot server "expects" SlotFile.Write("A message from VBScript" & chr(0)) SlotFile.Close WScript.Quit
Named pipes can be handled with functions of the "same general shape", though they can be more complicated due to their "confirmed delivery" nature (calls to DLLs can be blocking). The do no have the message length limitation of mailslots.