lee321987 Posted December 22, 2009 Share Posted December 22, 2009 Hello. I have a script that I won't only one instance to run, but when a second instance is attempted, it causes a variable to change in the first instance. Possible? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 22, 2009 Moderators Share Posted December 22, 2009 (edited) 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. expandcollapse popup#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 December 22, 2009 by Melba23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
lee321987 Posted December 22, 2009 Author Share Posted December 22, 2009 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 More sharing options...
Moderators Melba23 Posted December 22, 2009 Moderators Share Posted December 22, 2009 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: expandcollapse popup#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. 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now