Jump to content

Second instance of program -> cause change in first instance


 Share

Recommended Posts

  • Moderators

lee321987,

Yes......but (there is always a but!) what sort of variable? Number, string?

Compile this script and run it twice. The first instance receives a string from the second, which then exits.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

; Based on code from Yashied

#include <GUIConstantsEx.au3>

Opt("WinTitleMatchMode", 3)

Global Const $WM_COPYDATA = 0x004A
Global $hLabel, $sMsg_Rcvd, $sMsg_Set = ""

; Set GUI title
If WinExists("First Instance") Then
    $hWnd = WinGetHandle("First Instance")
    If (Not @error) Then _SendData($hWnd, "Hi from Second Instance")
    Sleep(1000)
    Exit
EndIf

; Create GUI
GUICreate("First Instance", 200, 100)

$hLabel = GUICtrlCreateLabel("", 10, 10, 180, 20)

GUISetState()

GUIRegisterMsg($WM_COPYDATA, "_WM_COPYDATA")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ; Check messages received
    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

Sending a number is even easier! ;)

M23

Edit: Added script.

Edited by Melba23

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

I must apologize - I worded my first post poorly. The second instance doesn't need to pass any data, I just need the first instance to be aware that a second instance was attempted (then a timer will reset).

However I'm grateful that you posted what you did, because I'm sure I will use it, and I'm interested in passing numbers too if you want to elaborate.

Link to comment
Share on other sites

  • Moderators

lee321987,

I do not think your first post was worded poorly at all - you have to send a message somehow!

Here is the "number" version - again compile and run twice:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

; Based on code from Yashied

#include <GUIConstantsEx.au3>
#include <WinApi.au3>

Opt("WinTitleMatchMode", 3)

Global Const $WM_MYMSG = _WinAPI_RegisterWindowMessage('WM_MYMSG')
Global Const $HWND_BROADCAST = 0xFFFF

Global $iValue_Rcvd = Default

; Set GUI title
If WinExists("First Instance") Then
    $hGUI2 = GUICreate("", 1, 1, 0, 0)
    GUISetState()
    _WinAPI_PostMessage($HWND_BROADCAST, $WM_MYMSG, 999999, $hGUI2)
    GUIDelete($hGUI2)
    Exit
EndIf

; Create GUI
$hGUI = GUICreate("First Instance", 200, 100)

$hLabel = GUICtrlCreateLabel("", 10, 10, 180, 20)

GUISetState()

GUIRegisterMsg($WM_MYMSG, 'WM_MYMSG')

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ; Check messages received
    If $iValue_Rcvd <> Default Then
        GUICtrlSetData($hLabel, Number($iValue_Rcvd))
        $iValue_Set = Default
    EndIf

WEnd

Func WM_MYMSG($hWnd, $iMsg, $wParam, $lParam)
    ; Only react if sent from another GUI
    If $lParam <> $hGUI Then $iValue_Rcvd = $wParam
EndFunc   ;==>WM_MYMSG

If all you want is to know if the second instance has been attempted, this is the easier code as you can see. ;)

M23

P.S. And in case you did not notice, you can thank Yashied for these scripts - I am nowhere near clever enough. :evil:

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