Jump to content

2 scripts for same variable


Recommended Posts

Hello

For example, i have 3 scripts TOTO.AU3, TATA.EXE (script builded), TITI.EXE (script builded) :

TOTO.AU3

$var = 1
run("tata.exe")
run("titi.exe")

sleep(10000)
$var = 2

sleep(10000)
$var = 3

sleep(10000)

TATA.EXE

while $var <> 2
  Sleep(1000)
Wend
msgbox( 0, "essai1", "")

TITI.EXE

while $var <> 3
  Sleep(1000)
Wend
msgbox( 0, "essai1", "")

I would like TOTO/TATA/TITI could read/write the same variable $var ?

Have you any idea ?

Thank

PS : I think writing the variable/value in a file if i haven't a better idea.

Edited by mortelrdv
Link to comment
Share on other sites

No. It's couldn't rewrite. Look for Read/WriteProcessMemory or use a file or TCP connection for data transfer between different programs..

P.S. TCP connection would be the most effective.

Edited by Godless

_____________________________________________________________________________

Link to comment
Share on other sites

What you're after call IPC: Inter Process Communication (Communication Inter-Processus en bon hexagonal). There are thousands of ways IPC can be achieved, some more suited to given constraints or system. Googgling will drag countless examples / ideas. Another simple way for really simple needs in the MS world: environment variables!

Be aware that if your shared data is read/written concurrently and asynchronously you need to protect against various disaster scenarii.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Here is a small example of two scripts which share a variable. The Master sets the value in a dllstruct (whatever value is in the edit) and the slave reads from the memory used by the dll. If you had other windows then they can do the same as the slave and read form the same memory address in the same way.

This is the master. When you run as a script it will also run the slave script, but the slave script must ba saved as "slave.au3" and be in the same folder as the master script.

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#AutoIt3Wrapper_Add_Constants=n
#include <guiconstants.au3>
#include <SecurityConstants.au3>
#include <nomadmemory.au3>
#include <editconstants.au3>


Opt("GUIOnEventMode", 1)
GUICreate("Master (sets variable)", 250, 70)
$input = GUICtrlCreateInput("1", 20, 20, 120, 28, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
GUISetState()


GUISetOnEvent($GUI_EVENT_CLOSE, "done")
$AutoItProdexePath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir");installDir for production

$output = DllStructCreate("int")
$opAdd = DllStructGetPtr($output)

$typeMesg = RegisterWindowMessage("someuniquetext10987")
GUIRegisterMsg($typeMesg, "tellAddr"); respond to requests for the address to use

$ToRun = '"' & $AutoItProdexePath & '\AutoIt3.exe"' & ' "' & @ScriptDir & '\slave.au3"'
Run($ToRun)

WinWait("Slave1", "", 30);wait up to 30 sec for slave
If Not WinExists("Slave1") Then Exit

Global $CurrentVal, $Lastval = -1

While 1

    $CurrentVal = Number(GUICtrlRead($input))
    If $CurrentVal <> $lastval Then
    DllStructSetData($output, 1, $CurrentVal)
    $lastval = $CurrentVal
        ConsoleWrite("set to "& $lastval & @CRLF)
    EndIf
    If not WinExists("Slave1") then exit
WEnd





Func tellAddr($hWndGUI, $MsgID, $WParam, $LParam)
    If $Wparam = 1 then Return $opAdd

EndFunc ;==>tellAddr

;register window message
;returns a unique message number. if the message has already been registered then the
;the unique number for $stext is returned.
;Useful for two windows to communicate
Func RegisterWindowMessage($sText)
    Local $aRet = DllCall('user32.dll', 'int', 'RegisterWindowMessage', 'str', $sText)

    Return $aRet[0]
EndFunc ;==>RegisterWindowMessage


Func done()

    Exit
EndFunc

This is the slave script which is run for you by the master. Instead, you could have 2 exe's and the master need not run the slave (remove the "Run" line)

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#AutoIt3Wrapper_Add_Constants=n
#include <guiconstants.au3>
#include <SecurityConstants.au3>
#include <nomadmemory.au3>
#include <SendMessage.au3>


Opt("GUIOnEventMode", 1)
$masterTitle = "Master (sets variable)"
If Not WinExists($masterTitle) Then Exit;must run master first

$wp = WinGetPos($masterTitle)
GUICreate("Slave1", 250, 70,$wp[0],$wp[1]+$wp[3])
$labmem = GUICtrlCreateLabel("00000000000", 20, 20, 120, 28)
GUISetState()


GUISetOnEvent($GUI_EVENT_CLOSE, "done")

;get info on master
Global $pid = WinGetProcess($masterTitle), $hid = WinGetHandle($masterTitle)

Global $MemOpen = _MemoryOpen($pid), $memadd = 0


$typeMesg = RegisterWindowMessage("someuniquetext10987")

$memadd = _SendMessage($hid, $typeMesg, 1);tell master we want address of variable
;master will reply with address to use


$Lastval = -1

While 1

    $CurrentVal = Get(1)
    If $Lastval <> $CurrentVal Then
    GUICtrlSetData($labmem, $CurrentVal)
    $lastVal = $CurrentVal
    EndIf
 sleep(50)

if not winexists($masterTitle) then exit

WEnd





Func Get($var)
    ;only reads one int in this script so $var ignored
    Return _MemoryRead($memadd, $MemOpen, "int")

EndFunc ;==>Get

Func Set($var, $val);not used
    ;only write one int in this script so $var ignored
    _MemoryWrite($memadd, $MemOpen, $val, "int")
EndFunc ;==>Set

Func Done()
    Exit
EndFunc ;==>Done


Func RegisterWindowMessage($sText)
    Local $aRet = DllCall('user32.dll', 'int', 'RegisterWindowMessage', 'str', $sText)
    Return $aRet[0]
EndFunc ;==>RegisterWindowMessage
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • Moderators

mortelrdv,

My current favourite is MailSlot from trancexx. I have built (a very little) on her work and here is the result - you need to compile the script a couple of times and rename the .exe each time to something different (I used the boring Demo_1, Demo_2, etc). Then run the .exes and once they are all up, press the "Update Accounts" button so every instance is aware of all the others. Then spend some time sending messages back and forth. :D

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

#include "MailSlot.au3"

Global $sMailSlotListFile = @AppDataCommonDir & "\MailSlot\MailSlotList.lst"
Global $sMailSlotTitle = StringRegExpReplace(@ScriptName, "\..*$", "")
Global $sRandomMailSlotname = _RandomStr()

ConsoleWrite( $sMailSlotListFile  & @CRLF)

Global Const $sMailSlotName_Receive = "\\.\mailslot\" & $sRandomMailSlotname

; Add MailSlot to list file
IniWrite($sMailSlotListFile, "MailSlots", $sMailSlotTitle, $sRandomMailSlotname)

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

Global $iNumberOfMessagesOverall
Global $sMailSlotName_Send = ""

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

GUICtrlCreateLabel("Message To Send To:", 10, 22, 150, 25)
GUICtrlSetColor(-1, 0x0000CC)
GUICtrlSetFont(-1, 11)

Global $hSend_Combo = GUICtrlCreateCombo("", 180, 22, 100, 25)
_UpdateMail()

Global $hEdit_Send = GUICtrlCreateEdit("", 15, 50, 300, 50, 0x0040) ; $ES_AUTOVSCROLL

GUICtrlCreateLabel("Message Received:", 10, 122, 150, 25)
GUICtrlSetColor(-1, 0x0000CC)
GUICtrlSetFont(-1, 11)

Global $hEdit_Read = GUICtrlCreateEdit("", 15, 150, 300, 200, 0x0800) ; $ES_READONLY

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

Global $hButton_Update = GUICtrlCreateButton("&Update Accounts", 330, 50, 100, 25)

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

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

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

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

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

GUISetState()

While 1

    Switch GUIGetMsg()
        Case - 3, $hButtonCloseApp
            IniDelete($sMailSlotListFile, "MailSlots", $sMailSlotTitle)
            Exit
        Case $hButtonSend
            If GUICtrlRead($hSend_Combo) = "" Then
                MsgBox(64, "MailSlot demo", "No account selected!", 0, $hGUI)
            Else
                $sMailSlotName_Send = "\\.\mailslot\" & IniRead($sMailSlotListFile, "MailSlots", GUICtrlRead($hSend_Combo), "")
                If $sMailSlotName_Send <> "\\.\mailslot\" Then
                    _SendMail($sMailSlotName_Send)
                Else
                    MsgBox(48, "MailSlot demo", "Not valid account!", 0, $hGUI)
                EndIf
            EndIf
        Case $hButton_Update
            _UpdateMail()
        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_Receive)
            EndIf
    EndSwitch

WEnd


; Wrapper functions:

Func _UpdateMail()

    Local $aMailSlots = IniReadSection($sMailSlotListFile, "MailSlots")

    If Not IsArray($aMailSlots) Then MsgBox(64, "MailSlot demo", "No accounts available!", 0, $hGUI)

    Local $sMailSlotList = "|"
    For $i = 1 To $aMailSlots[0][0]
        If $aMailSlots[$i][1] <> $sRandomMailSlotname Then $sMailSlotList &= $aMailSlots[$i][0] & "|"
    Next
    GUICtrlSetData($hSend_Combo, $sMailSlotList)

EndFunc

Func _SendMail($sMailSlotName)

    Local $sDataToSend = GUICtrlRead($hEdit_Send)

    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_Send, "")
    Else
        MsgBox(64, "MailSlot demo", "Nothing to send.", 0, $hGUI)
    EndIf

EndFunc   ;==>_SendMail

Func _ReadMessage($hHandle)

    Local $iSize = _MailSlotCheckForNextMessage($hHandle)

    If $iSize Then
        Local $sData = _MailSlotRead($hMailSlot, $iSize, 1)
        $iNumberOfMessagesOverall += 1
        GUICtrlSetData($hEdit_Read, "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)
        IniDelete($sMailSlotListFile, "MailSlots", $sMailSlotTitle)
    Else
        MsgBox(48, "MailSlot demo error", "Account could not be closed!", 0, $hGUI)
    EndIf

EndFunc   ;==>_CloseMailAccount


Func _RestoreAccount($sMailSlotName_Receive)

    Local $hMailSlotHandle = _MailSlotCreate($sMailSlotName_Receive)

    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
        IniWrite($sMailSlotListFile, "MailSlots", $sMailSlotTitle, $sRandomMailSlotname)
    EndIf

EndFunc   ;==>_RestoreAccount

Func _RandomStr()
    Local $sString
    For $i = 1 To 13
        $sString &= Chr(Random(97, 122, 1))
    Next
    Return $sString
EndFunc   ;==>__RandomStr

And to save you searching - here is trancexx's UDF:

; MailSlot.au3
;.......script written by trancexx (trancexx at yahoo dot com)

#cs
    Available functions:
    _MailSlotCheckForNextMessage
    _MailSlotClose
    _MailSlotCreate
    _MailSlotGetMessageCount
    _MailSlotSetTimeout
    _MailSlotRead
    _MailSlotWrite
#ce

; #FUNCTION# ;===============================================================================
;
; Name...........: _MailSlotCheckForNextMessage
; Description ...: Checks for presence of a new message.
; Syntax.........: _MailSlotCheckForNextMessage ($hMailSlot)
; Parameters ....: $hMailSlot - Mailslot handle
; Return values .: Success - Returns 0 if there is no message or the size of a new message in bytes if there is one
;                          - Sets @error to 0
;                  Failure - Returns 0 and sets @error:
;                  |1 - GetMailslotInfo function or call to it failed.
; Author ........: trancexx
; Modified.......:
; Link ..........; http://msdn.microsoft.com/en-us/library/aa365435(VS.85).aspx
;
;==========================================================================================
Func _MailSlotCheckForNextMessage($hMailSlot)

    Local $aCall = DllCall("kernel32.dll", "int", "GetMailslotInfo", _
            "ptr", $hMailSlot, _
            "dword*", 0, _
            "dword*", 0, _
            "dword*", 0, _
            "dword*", 0)

    If @error Or Not $aCall[0] Then
        Return SetError(1, 0, 0)
    EndIf

    If $aCall[3] = -1 Or Not $aCall[4] Then
        Return 0
    Else
        Return $aCall[3]
    EndIf

EndFunc   ;==>_MailSlotCheckForNextMessage


; #FUNCTION# ;===============================================================================
;
; Name...........: _MailSlotClose
; Description ...: Closes mailslot.
; Syntax.........: _MailSlotClose ($hMailSlot)
; Parameters ....: $hMailSlot - Mailslot handle
; Return values .: Success - Returns 1
;                          - Sets @error to 0
;                  Failure - Returns 0 and sets @error:
;                  |1 - CloseHandle function or call to it failed.
; Author ........: trancexx
; Modified.......:
; Link ..........; http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
;
;==========================================================================================
Func _MailSlotClose($hMailSlot)

    Local $aCall = DllCall("kernel32.dll", "int", "CloseHandle", "ptr", $hMailSlot)

    If @error Or Not $aCall[0] Then
        Return SetError(1, 0, 0)
    EndIf

    Return 1

EndFunc   ;==>_MailSlotClose


; #FUNCTION# ;===============================================================================
;
; Name...........: _MailSlotCreate
; Description ...: Creates a mailslot with the specified name.
; Syntax.........: _MailSlotCreate ($sMailSlotName [,$iSize [, $iTimeOut [, $pSecurityAttributes])
; Parameters ....: $sMailSlotName - The name of the mailslot
;                  $iSize - The maximum size of a single message that can be written to the mailslot, in bytes. 0 means any size.
;                  $iTimeOut - The time a read operation can wait for a message to be written to the mailslot before a time-out occurs, in milliseconds.
;                              Can be 0 - returns immediately if no message is present
;                                     -1 (minus one) - waits forever for a message
;                  $pSecurityAttributes - A pointer to a SECURITY_ATTRIBUTES structure. 0 means the handle cannot be inherited.
; Return values .: Success - Returns a handle that a mailslot server can use to perform operations on the mailslot
;                          - Sets @error to 0
;                  Failure - Returns -1 and sets @error:
;                  |1 - CreateMailslotW function or call to it failed.
; Author ........: trancexx
; Modified.......:
; Remarks .......: Mailslot name must have the following form and must be unique: \\.\mailslot\[path]name
;                  The name may include multiple levels of pseudo directories separated by backslashes.
;                  For example \\.\mailslot\abc\def\ghi is valid name too.
; Link ..........; http://msdn.microsoft.com/en-us/library/aa365147(VS.85).aspx
;
;==========================================================================================
Func _MailSlotCreate($sMailSlotName, $iSize = 0, $iTimeOut = 0, $pSecurityAttributes = 0)

    Local $aCall = DllCall("kernel32.dll", "ptr", "CreateMailslotW", _
            "wstr", $sMailSlotName, _
            "dword", $iSize, _
            "dword", $iTimeOut, _
            "ptr", $pSecurityAttributes)

    If @error Or $aCall[0] = -1 Then
        Return SetError(1, 0, -1)
    EndIf

    Return $aCall[0]

EndFunc   ;==>_MailSlotCreate


; #FUNCTION# ;===============================================================================
;
; Name...........: _MailSlotGetMessageCount
; Description ...: Retrieves the number of messages in the mailslot.
; Syntax.........: _MailSlotGetMessageCount ($hMailSlot)
; Parameters ....: $hMailSlot - Mailslot handle
; Return values .: Success - Returns the number of the messages in the mailslot waiting to be read.
;                          - Sets @error to 0
;                  Failure - Returns 0 and sets @error:
;                  |1 - GetMailslotInfo function or call to it failed.
; Author ........: trancexx
; Modified.......:
; Link ..........; http://msdn.microsoft.com/en-us/library/aa365435(VS.85).aspx
;
;==========================================================================================
Func _MailSlotGetMessageCount($hMailSlot)

    Local $aCall = DllCall("kernel32.dll", "int", "GetMailslotInfo", _
            "ptr", $hMailSlot, _
            "dword*", 0, _
            "dword*", 0, _
            "dword*", 0, _
            "dword*", 0)

    If @error Or Not $aCall[0] Then
        Return SetError(1, 0, 0)
    EndIf

    If $aCall[3] = -1 Then
        Return 0
    Else
        Return $aCall[4]
    EndIf

EndFunc   ;==>_MailSlotGetMessageCount



; #FUNCTION# ;===============================================================================
;
; Name...........: _MailSlotSetTimeout
; Description ...: Sets the time-out value used by the specified mailslot for a read operation.
; Syntax.........: _MailSlotSetTimeout ($hMailSlot, $iTimeout)
; Parameters ....: $hMailSlot - Mailslot handle
;                  $iTimeout - The time a read operation can wait for a message to be written to the mailslot before a time-out occurs, in milliseconds.
;                              -1 (minus one) means for function to wait forever for a message.
;                              0 (zero) mans to return immediately if no message is present
; Return values .: Success - Returns 1.
;                          - Sets @error to 0
;                  Failure - Returns 0 and sets @error:
;                  |1 - SetMailslotInfo function or call to it failed.
; Author ........: trancexx
; Modified.......:
; Link ..........; http://msdn.microsoft.com/en-us/library/aa365786(VS.85).aspx
;
;==========================================================================================
Func _MailSlotSetTimeout($hMailSlot, $iTimeOut)

    Local $aCall = DllCall("kernel32.dll", "int", "SetMailslotInfo", _
            "ptr", $hMailSlot, _
            "dword", $iTimeOut)

    If @error Or Not $aCall[0] Then
        Return SetError(1, 0, 0)
    EndIf

    Return 1

EndFunc   ;==>_MailSlotSetTimeout


; #FUNCTION# ;===============================================================================
;
; Name...........: _MailSlotRead
; Description ...: Reads messages from the specified mailslot.
; Syntax.........: _MailSlotRead ($hMailSlot , $iSize [, $iMode])
; Parameters ....: $hMailSlot - Mailslot handle
;                  $iSize - The number of bytes to read.
;                  $iMode - Reading mode.
;                             Can be: 0 - read binary
;                                     1 - read ANSI
;                                     1 - read UTF8
; Return values .: Success - Returns read data
;                          - Sets @extended to number of read bytes
;                          - Sets @error to 0
;                  Special: Sets @error to -1 if specified buffer to read to is too small.
;                  Failure - Returns empty string and sets @error:
;                  |1 - DllCall() to ReadFile failed.
;                  |2 - GetLastError function or call to it failed.
;                  |3 - ReadFile function failed. @extended will be set to the return value of the GetLastError function.
; Author ........: trancexx
; Modified.......:
; Link ..........; http://msdn.microsoft.com/en-us/library/aa365467(VS.85).aspx
;                  http://msdn.microsoft.com/en-us/library/ms679360(VS.85).aspx
;
;==========================================================================================
Func _MailSlotRead($hMailSlot, $iSize, $iMode = 0)

    Local $tDataBuffer = DllStructCreate("byte[" & $iSize & "]")

    Local $aCall = DllCall("kernel32.dll", "int", "ReadFile", _
            "ptr", $hMailSlot, _
            "ptr", DllStructGetPtr($tDataBuffer), _
            "dword", $iSize, _
            "dword*", 0, _
            "ptr", 0)

    If @error Then
        Return SetError(1, 0, "")
    EndIf

    If Not $aCall[0] Then
        Local $aLastErrorCall = DllCall("kernel32.dll", "int", "GetLastError")
        If @error Then
            Return SetError(2, 0, "")
        EndIf
        If $aLastErrorCall[0] = 122 Then ; ERROR_INSUFFICIENT_BUFFER
            Return SetError(-1, 0, "")
        Else
            Return SetError(3, $aLastErrorCall[0], "")
        EndIf
    EndIf

    Local $vOut

    Switch $iMode
        Case 1
            $vOut = BinaryToString(DllStructGetData($tDataBuffer, 1))
        Case 2
            $vOut = BinaryToString(DllStructGetData($tDataBuffer, 1), 4)
        Case Else
            $vOut = DllStructGetData($tDataBuffer, 1)
    EndSwitch

    Return SetError(0, $aCall[4], $vOut)

EndFunc   ;==>_MailSlotRead


; #FUNCTION# ;===============================================================================
;
; Name...........: _MailSlotWrite
; Description ...: Writes message to the specified mailslot.
; Syntax.........: _MailSlotWrite ($sMailSlotName , $vData [, $iMode])
; Parameters ....: $hMailSlot - Mailslot name
;                  $vData - Data to write.
;                  $iMode - Writing mode.
;                             Can be: 0 - write binary
;                                     1 - write ANSI
;                                     1 - write UTF8
; Return values .: Success - Returns the number of written bytes
;                          - Sets @error to 0
;                  Failure - Returns empty string and sets @error:
;                  |1 - CreateFileW function or call to it failed.
;                  |2 - WriteFile function or call to it failed.
;                  |3 - Opened mail slot handle could not be closed.
;                  |4 - WriteFile function or call to it failed and additionally opened mail slot handle could not be closed.
; Author ........: trancexx
; Modified.......:
; Link ..........; http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
;                  http://msdn.microsoft.com/en-us/library/aa365747(VS.85).aspx
;                  http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
;
;==========================================================================================
Func _MailSlotWrite($sMailSlotName, $vData, $iMode = 0)

    Local $aCall = DllCall("kernel32.dll", "ptr", "CreateFileW", _
            "wstr", $sMailSlotName, _
            "dword", 0x40000000, _ ; GENERIC_WRITE
            "dword", 1, _ ; FILE_SHARE_READ
            "ptr", 0, _
            "dword", 3, _ ; OPEN_EXISTING
            "dword", 0, _ ; SECURITY_ANONYMOUS
            "ptr", 0)

    If @error Or $aCall[0] = -1 Then
        Return SetError(1, 0, 0)
    EndIf

    Local $hMailSlotHandle = $aCall[0]

    Local $bData

    Switch $iMode
        Case 1
            $bData = StringToBinary($vData, 1)
        Case 2
            $bData = StringToBinary($vData, 4)
        Case Else
            $bData = $vData
    EndSwitch

    Local $iBufferSize = BinaryLen($bData)

    Local $tDataBuffer = DllStructCreate("byte[" & $iBufferSize & "]")
    DllStructSetData($tDataBuffer, 1, $bData)

    $aCall = DllCall("kernel32.dll", "int", "WriteFile", _
            "ptr", $hMailSlotHandle, _
            "ptr", DllStructGetPtr($tDataBuffer), _
            "dword", $iBufferSize, _
            "dword*", 0, _
            "ptr", 0)

    If @error Or Not $aCall[0] Then
        $aCall = DllCall("kernel32.dll", "int", "CloseHandle", "ptr", $hMailSlotHandle)
        If @error Or Not $aCall[0] Then
            Return SetError(4, 0, 0)
        EndIf
        Return SetError(2, 0, 0)
    EndIf

    Local $iOut = $aCall[4]

    $aCall = DllCall("kernel32.dll", "int", "CloseHandle", "ptr", $hMailSlotHandle)
    If @error Or Not $aCall[0] Then
        Return SetError(3, 0, $iOut)
    EndIf

    Return $iOut

EndFunc   ;==>_MailSlotWrite

I refuse to accept any responsibility for the content of the error messages - go take it up with trancexx herself! :huggles:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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