Sign in to follow this  
Followers 0
SaVoR

Sending a close command to another AutoIt Script

6 posts in this topic

I was reading through the forums and I know about the WM_Close command, but I was wondering if there was a way to send an exit process command to only other autoit scripts? Or a command that autoit scripts will only recognize?

Share this post


Link to post
Share on other sites



SaVoR,

This code sends messages between AutoIt scripts - and closing one of the independent GUIs closes both. You need to compile it and then run it twice to get a pair of intercommunicating GUIs. Probably a bit more complicated than you need, but should give you some pointers on how to proceed.

; Based on code from Yashied

#include <GUIConstantsEx.au3>

Opt("WinTitleMatchMode", 3)

Global Const $WM_COPYDATA = 0x004A
Global $sThis_Win_Title, $sThat_Win_Title
Global $iY, $hInput, $hButton, $hLabel
Global $sMsg_To_Send, $sMsg_Rcvd, $sMsg_Set = ""

; Set GUI title
If WinExists("First Instance") Then
    If WinExists("Second Instance") Then Exit
    $sThis_Win_Title = "Second Instance"
    $sThat_Win_Title = "First Instance"
    $iY = 300
Else
    $sThis_Win_Title = "First Instance"
    $sThat_Win_Title = "Second Instance"
    $iY = 100
EndIf

; Create GUI
GUICreate($sThis_Win_Title, 400, 150, 100, $iY)

$hInput = GUICtrlCreateInput("", 20, 20, 360, 20)
$hButton = GUICtrlCreateButton("Send", 160, 60, 80, 30)
$hLabel = GUICtrlCreateLabel("", 20, 100, 360, 20)

GUIRegisterMsg($WM_COPYDATA, "_WM_COPYDATA")

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ; Send close message to other window
            _SendData(WinGetHandle($sThat_Win_Title), "@exit")
            Exit
        Case $hButton
            ; Send message to other window
            $sMsg_To_Send = GUICtrlRead($hInput)
            $hWnd = WinGetHandle($sThat_Win_Title)
            If (Not @error) And ($sMsg_To_Send <> "") Then _SendData($hWnd, $sMsg_To_Send)
    EndSwitch

    ; Check messages received
    If $sMsg_Rcvd = "@exit" Then Exit
    If $sMsg_Rcvd <> $sMsg_Set Then
        GUICtrlSetData($hLabel, $sMsg_Rcvd)
        $sMsg_Set = $sMsg_Rcvd
    EndIf

WEnd

Func _SendData($hWnd, $sData)

    Local $tCOPYDATA, $tMsg

    $tMsg = DllStructCreate("char[" & StringLen($sData) + 1 & "]")
    DllStructSetData($tMsg, 1, $sData)
    $tCOPYDATA = DllStructCreate("dword;dword;ptr")
    DllStructSetData($tCOPYDATA, 2, StringLen($sData) + 1)
    DllStructSetData($tCOPYDATA, 3, DllStructGetPtr($tMsg))
    $Ret = DllCall("user32.dll", "lparam", "SendMessage", "hwnd", $hWnd, "int", $WM_COPYDATA, "wparam", 0, "lparam", DllStructGetPtr($tCOPYDATA))
    If (@error) Or ($Ret[0] = -1) Then Return 0
    Return 1

EndFunc   ;==>_SendData

Func _WM_COPYDATA($hWnd, $msgID, $wParam, $lParam)

    Local $tCOPYDATA = DllStructCreate("dword;dword;ptr", $lParam)
    Local $tMsg = DllStructCreate("char[" & DllStructGetData($tCOPYDATA, 2) & "]", DllStructGetData($tCOPYDATA, 3))
    $sMsg_Rcvd = DllStructGetData($tMsg, 1)
    Return 0

EndFunc   ;==>_WM_COPYDATA

Ask if anything is unclear.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

SaVoR,

This code sends messages between AutoIt scripts - and closing one of the independent GUIs closes both. You need to compile it and then run it twice to get a pair of intercommunicating GUIs. Probably a bit more complicated than you need, but should give you some pointers on how to proceed.

; Based on code from Yashied

#include <GUIConstantsEx.au3>

Opt("WinTitleMatchMode", 3)

Global Const $WM_COPYDATA = 0x004A
Global $sThis_Win_Title, $sThat_Win_Title
Global $iY, $hInput, $hButton, $hLabel
Global $sMsg_To_Send, $sMsg_Rcvd, $sMsg_Set = ""

; Set GUI title
If WinExists("First Instance") Then
    If WinExists("Second Instance") Then Exit
    $sThis_Win_Title = "Second Instance"
    $sThat_Win_Title = "First Instance"
    $iY = 300
Else
    $sThis_Win_Title = "First Instance"
    $sThat_Win_Title = "Second Instance"
    $iY = 100
EndIf

; Create GUI
GUICreate($sThis_Win_Title, 400, 150, 100, $iY)

$hInput = GUICtrlCreateInput("", 20, 20, 360, 20)
$hButton = GUICtrlCreateButton("Send", 160, 60, 80, 30)
$hLabel = GUICtrlCreateLabel("", 20, 100, 360, 20)

GUIRegisterMsg($WM_COPYDATA, "_WM_COPYDATA")

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ; Send close message to other window
            _SendData(WinGetHandle($sThat_Win_Title), "@exit")
            Exit
        Case $hButton
            ; Send message to other window
            $sMsg_To_Send = GUICtrlRead($hInput)
            $hWnd = WinGetHandle($sThat_Win_Title)
            If (Not @error) And ($sMsg_To_Send <> "") Then _SendData($hWnd, $sMsg_To_Send)
    EndSwitch

    ; Check messages received
    If $sMsg_Rcvd = "@exit" Then Exit
    If $sMsg_Rcvd <> $sMsg_Set Then
        GUICtrlSetData($hLabel, $sMsg_Rcvd)
        $sMsg_Set = $sMsg_Rcvd
    EndIf

WEnd

Func _SendData($hWnd, $sData)

    Local $tCOPYDATA, $tMsg

    $tMsg = DllStructCreate("char[" & StringLen($sData) + 1 & "]")
    DllStructSetData($tMsg, 1, $sData)
    $tCOPYDATA = DllStructCreate("dword;dword;ptr")
    DllStructSetData($tCOPYDATA, 2, StringLen($sData) + 1)
    DllStructSetData($tCOPYDATA, 3, DllStructGetPtr($tMsg))
    $Ret = DllCall("user32.dll", "lparam", "SendMessage", "hwnd", $hWnd, "int", $WM_COPYDATA, "wparam", 0, "lparam", DllStructGetPtr($tCOPYDATA))
    If (@error) Or ($Ret[0] = -1) Then Return 0
    Return 1

EndFunc   ;==>_SendData

Func _WM_COPYDATA($hWnd, $msgID, $wParam, $lParam)

    Local $tCOPYDATA = DllStructCreate("dword;dword;ptr", $lParam)
    Local $tMsg = DllStructCreate("char[" & DllStructGetData($tCOPYDATA, 2) & "]", DllStructGetData($tCOPYDATA, 3))
    $sMsg_Rcvd = DllStructGetData($tMsg, 1)
    Return 0

EndFunc   ;==>_WM_COPYDATA

Ask if anything is unclear.

M23

I found that in the forums as well. But what if its a program that you didn't set up to receive messages? Something that is similar to WinKill() or ProcessClose(), but only works on other AutoIt scripts?

Example:

$list = ProcessList()
for $i = 1 to $list[0][0]
  ProcessClose($list[$i][1])
next

except it will only work on autoit scripts, i dont want to close all the other processes though.

is there a command you can send that autoit specifically recognizes as a close?

Share this post


Link to post
Share on other sites

SaVoR,

I am confused. :)

If you created/opened the AutoIt scripts, then you should know the correct parameters to target the correct window/process and so be able to use WinKill or ProcessClose .

If you did not create/open the scripts, why are you trying to close them?

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

SaVoR,

I am confused. :)

If you created/opened the AutoIt scripts, then you should know the correct parameters to target the correct window/process and so be able to use WinKill or ProcessClose .

If you did not create/open the scripts, why are you trying to close them?

M23

Well the intention is for an Anti-Cheat. The cheat was written in AutoIt and I want to figure a for sure way to close all AutoIt processes. I figured out a few ways to do this, but if one were so inclined to figure out what ways, they could bypass it. If there isn't a way, I can just do the ones I found out, but I was just curious if there was another way.

Share this post


Link to post
Share on other sites

I figured it out.. thanks for trying to help :)

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
Sign in to follow this  
Followers 0