KaFu Posted December 15, 2009 Share Posted December 15, 2009 (edited) For an example on how to send and receive data between different instances of the same script and which passes commandline parameters to the first instance via WM_COPYDATA take a look at post 2.Just a short snippet which came to my mind as the _Singleton() standard UDF failed on my work-notebook for me (CreateMutexW returns winapi-error 126 - "The specified module could not be found" , I just assume it interferes with one of the 72 processes running).#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.0.0 Author: KaFu Script Function: _EnforceSingleInstance() function, Standard _Singleton() UDF failed on some machines for me #ce ---------------------------------------------------------------------------- _EnforceSingleInstance('e15ff08b-84ac-472a-89bf-5f92db683165') ; any 'unique' string; created with http://www.guidgen.com/Index.aspx MsgBox(0, "", "Test") Func _EnforceSingleInstance($GUID_Program) If $GUID_Program = "" Then Return SetError(1,'',1) if IsHWnd(WinGetHandle($GUID_Program)) then MsgBox(0,"Test","Second instance, will exit now...") Exit EndIf AutoItWinSetTitle($GUID_Program) Return WinGetHandle($GUID_Program) EndFunc ;==>_EnforceSingleInstanceShould work as long as the key is really 'unique' (as unique as it gets ), and script is not complied as CUI (I'm not sure but I assume then there is no default AutoItWin). Edited April 10, 2010 by KaFu Professor_Bernd 1 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
KaFu Posted December 15, 2009 Author Share Posted December 15, 2009 (edited) And while playing around... here is a version which passes commandline parameters to first instance via WM_COPYDATA before exiting... Compile and start multiple times with different commandline parameters to see whats happening... expandcollapse popup#NoTrayIcon #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> $hwnd_AutoIt = _EnforceSingleInstance('e15ff08b-84ac-472a-89bf-5f92db683165') ; any 'unique' string; created with http://www.guidgen.com/Index.aspx Opt("TrayIconHide", 1) $hGUI = GUICreate("My GUI " & $hwnd_AutoIt,300,300,Default,Default) ; will create a dialog box that when displayed is centered $label = GUICtrlCreateLabel($CmdLineRaw,10,10,300,100) GUISetState(@SW_SHOW) ; will display an empty dialog box ControlSetText($hwnd_AutoIt,'',ControlGetHandle($hwnd_AutoIt,'','Edit1'),$hGUI) ; to pass hWnd of main GUI to AutoIt default GUI GUIRegisterMsg($WM_COPYDATA, "WM_COPYDATA") While 1 sleep(10) $msg = GUIGetMsg() If $msg = $GUI_EVENT_CLOSE Then ExitLoop WEnd GUIDelete() Func _EnforceSingleInstance($GUID_Program = "") If $GUID_Program = "" Then Return $hwnd = WinGetHandle($GUID_Program) If IsHWnd($hwnd) Then $hwnd_Target = ControlGetText($hwnd,'',ControlGetHandle($hwnd,'','Edit1')) WM_COPYDATA_SendData(HWnd($hwnd_Target), $CmdLineRaw) Exit EndIf AutoItWinSetTitle($GUID_Program) Return WinGetHandle($GUID_Program) EndFunc ;==>_EnforceSingleInstance Func WM_COPYDATA($hWnd, $MsgID, $wParam, $lParam) ; http://www.autoitscript.com/forum/index.php?showtopic=105861&view=findpost&p=747887 ; Melba23, based on code from Yashied Local $tCOPYDATA = DllStructCreate("ulong_ptr;dword;ptr", $lParam) Local $tMsg = DllStructCreate("char[" & DllStructGetData($tCOPYDATA, 2) & "]", DllStructGetData($tCOPYDATA, 3)) $msg = DllStructGetData($tMsg, 1) if $msg = " " then GUICtrlSetData($label, "") else GUICtrlSetData($label, DllStructGetData($tMsg, 1)) endif Return 0 EndFunc ;==>WM_COPYDATA Func WM_COPYDATA_SendData($hWnd, $sData) If Not IsHWnd($hWnd) Then Return 0 if $sData = "" then $sData = " " Local $tCOPYDATA, $tMsg $tMsg = DllStructCreate("char[" & StringLen($sData) + 1 & "]") DllStructSetData($tMsg, 1, $sData) $tCOPYDATA = DllStructCreate("ulong_ptr;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 ;==>WM_COPYDATA_SendData Edited November 23, 2011 by KaFu Professor_Bernd and mikell 2 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
jvanegmond Posted December 15, 2009 Share Posted December 15, 2009 (edited) Why do you set a default parameter when in the next line you are enforcing the user not to enter a use the default parameter anyway? Edited December 15, 2009 by Manadar github.com/jvanegmond Link to comment Share on other sites More sharing options...
KaFu Posted December 15, 2009 Author Share Posted December 15, 2009 Why do you set a default parameter when in the next line you are enforcing the user not to enter a use the default parameter anyway?Lazyness, copy&paste... corrected... OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
jvanegmond Posted December 15, 2009 Share Posted December 15, 2009 I really like your second implementation. Good job. github.com/jvanegmond Link to comment Share on other sites More sharing options...
Ascend4nt Posted April 9, 2010 Share Posted April 9, 2010 Hey, this is a pretty neat workaround. I didn't even know about the 'AutoItWinSetTitle' function! Go figure. I like the WM_COPYDATA use too - I had intended on writing something that would pass parameters to an already-running program in the future. Now i don't have to do the work My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
KaFu Posted April 10, 2010 Author Share Posted April 10, 2010 (edited) Hey, this is a pretty neat workaround.Thanks for the praise , nice to hear, especially from you .This is how I balance the access to the Sqlite DB between searching and reporting instance in SMF (two instances of smf.exe running at the same time to give instant access to results).And btw, a 4 star rating for this post? Someone didn't seem understand what's going on here ... Edited April 10, 2010 by KaFu OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
guinness Posted September 10, 2010 Share Posted September 10, 2010 I posted on the General Help and Support about getting your example of WM_COPYDATA to work in x64 and Yashied kindly replied with a solution, replace the following instances of "dword;dword;ptr" to "ulong_ptr;dword;ptr".Here is the post. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
KaFu Posted September 10, 2010 Author Share Posted September 10, 2010 Followed that post, thanks for the addition . OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
dexto Posted September 28, 2010 Share Posted September 28, 2010 Very interesting! WM_COPYDATA implementation should be a part of standard includes and documented in a help file. Link to comment Share on other sites More sharing options...
mikell Posted August 18, 2021 Share Posted August 18, 2021 On 12/15/2009 at 5:31 PM, KaFu said: here is a version which passes commandline parameters to first instance via WM_COPYDATA before exiting I'm really a bit late , but... This 2nd code is a true gift. Still working, clever, extremely easy to implement and use in an existing script Thanks KaFu 1 Link to comment Share on other sites More sharing options...
Trong Posted August 18, 2021 Share Posted August 18, 2021 Test Script: For $i=1 to 200 Run(@ScriptDir&"\EnforceSingleInstance_86.exe") ConsoleWrite($i&@CRLF) Next Main script to compile: expandcollapse popup#NoTrayIcon #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Outfile=EnforceSingleInstance_86.exe #AutoIt3Wrapper_Outfile_x64=EnforceSingleInstance_64.exe #AutoIt3Wrapper_Compile_Both=y #AutoIt3Wrapper_UseX64=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <StaticConstants.au3> Opt("MustDeclareVars", 1) Global $sGUID = 'e15ff08b-84ac-472a-89bf-5f92db683165' Global $gMsg, $hwnd_AutoIt = _EnforceSingleInstance($sGUID) ; any 'unique' string; created with http://www.guidgen.com/Index.aspx Opt("TrayIconHide", 1) Global $hGUI = GUICreate("My GUI " & $hwnd_AutoIt, 550, 150, -1, -1, -1, BitOR($WS_EX_TOPMOST, $WS_EX_WINDOWEDGE)) GUICtrlCreateLabel("CmdLineRaw:", 5, 60, 79, 60, BitOR($SS_RIGHT, $SS_CENTERIMAGE), $GUI_WS_EX_PARENTDRAG) GUICtrlCreateLabel("Process PID:", 5, 5, 79, 17, BitOR($SS_RIGHT, $SS_CENTERIMAGE), $GUI_WS_EX_PARENTDRAG) GUICtrlCreateLabel("GUID:", 6, 32, 79, 17, BitOR($SS_RIGHT, $SS_CENTERIMAGE), $GUI_WS_EX_PARENTDRAG) GUICtrlCreateLabel(StringInStr(@OSArch, '64') ? "OS x64" : "OS x86", 192, 5, 52, 17, $SS_CENTERIMAGE, $GUI_WS_EX_PARENTDRAG) GUICtrlCreateLabel(@AutoItX64 ? "Exe x64" : "EXE x86", 249, 5, 54, 17, $SS_CENTERIMAGE, $GUI_WS_EX_PARENTDRAG) GUICtrlCreateLabel(@AutoItPID, 94, 5, 86, 17, $SS_CENTERIMAGE, $GUI_WS_EX_PARENTDRAG) GUICtrlCreateLabel(IsAdmin() ? "Runing on Admin" : "Not Run as Admin", 318, 5, 94, 17, $SS_CENTERIMAGE, $GUI_WS_EX_PARENTDRAG) Global $Label_CmdLineRaw = GUICtrlCreateLabel($CmdLineRaw, 94, 60, 450, 60, $SS_CENTERIMAGE, $GUI_WS_EX_PARENTDRAG) Global $Label_GUID = GUICtrlCreateLabel($sGUID, 94, 32, 450, 17, $SS_CENTERIMAGE, $GUI_WS_EX_PARENTDRAG) Global $Label_Time = GUICtrlCreateLabel("", 448, 5, 100, 17, BitOR($SS_CENTER, $SS_CENTERIMAGE), $GUI_WS_EX_PARENTDRAG) Global $Label_Status = GUICtrlCreateLabel("", 5, 128, 535, 17, BitOR($SS_CENTER, $SS_CENTERIMAGE), $GUI_WS_EX_PARENTDRAG) GUISetState(@SW_SHOW) ControlSetText($hwnd_AutoIt, '', ControlGetHandle($hwnd_AutoIt, '', 'Edit1'), $hGUI) ; to pass hWnd of main GUI to AutoIt default GUI GUIRegisterMsg($WM_COPYDATA, "WM_COPYDATA") AdlibRegister("_Check_Process_Same_Name", 1000) While 1 Sleep(10) $gMsg = GUIGetMsg() If $gMsg = $GUI_EVENT_CLOSE Then ExitLoop GUICtrlSetData($Label_Time, @HOUR & ":" & @MIN & ":" & @SEC & ":" & @MSEC) WEnd GUIDelete() Exit Func _Check_Process_Same_Name() Local $ScriptName = @ScriptName If Not @Compiled Then $ScriptName = StringRegExpReplace(@AutoItExe, "^.*\\", "") ;ConsoleWrite("- Script Name: "&$ScriptName&@CRLF) Local $ProcessList = '', $aProc = ProcessList($ScriptName) For $i = 1 To $aProc[0][0] If $aProc[$i][1] <> @AutoItPID Then $ProcessList &= $aProc[$i][1] & " / " Next If $ProcessList <> "" Then GUICtrlSetData($Label_Status, "Process Same Name: " & $ProcessList) Else GUICtrlSetData($Label_Status, "") EndIf $ProcessList = '' EndFunc ;==>_Check_Process_Same_Name Func _EnforceSingleInstance($GUID_Program = "") If $GUID_Program = "" Then Return Local $hwnd = WinGetHandle($GUID_Program) If IsHWnd($hwnd) Then Local $hwnd_Target = ControlGetText($hwnd, '', ControlGetHandle($hwnd, '', 'Edit1')) WM_COPYDATA_SendData(HWnd($hwnd_Target), $CmdLineRaw) Exit EndIf AutoItWinSetTitle($GUID_Program) Return WinGetHandle($GUID_Program) EndFunc ;==>_EnforceSingleInstance Func WM_COPYDATA($hwnd, $MsgID, $wParam, $lParam) ; http://www.autoitscript.com/forum/index.php?showtopic=105861&view=findpost&p=747887 ; Melba23, based on code from Yashied Local $tCOPYDATA = DllStructCreate("ulong_ptr;dword;ptr", $lParam) Local $tMsg = DllStructCreate("char[" & DllStructGetData($tCOPYDATA, 2) & "]", DllStructGetData($tCOPYDATA, 3)) Local $xMsg = DllStructGetData($tMsg, 1) If StringStripWS($xMsg, 8) = "" Then GUICtrlSetData($Label_CmdLineRaw, "") Else GUICtrlSetData($Label_CmdLineRaw, $xMsg) EndIf Return 0 EndFunc ;==>WM_COPYDATA Func WM_COPYDATA_SendData($hwnd, $sData) If Not IsHWnd($hwnd) Then Return 0 If $sData = "" Then $sData = " " Local $tMsg = DllStructCreate("char[" & StringLen($sData) + 1 & "]") DllStructSetData($tMsg, 1, $sData) Local $tCOPYDATA = DllStructCreate("ulong_ptr;dword;ptr") DllStructSetData($tCOPYDATA, 2, StringLen($sData) + 1) DllStructSetData($tCOPYDATA, 3, DllStructGetPtr($tMsg)) Local $aRet = DllCall("user32.dll", "lparam", "SendMessage", "hwnd", $hwnd, "int", $WM_COPYDATA, "wparam", 0, "lparam", DllStructGetPtr($tCOPYDATA)) If (@error) Or ($aRet[0] = -1) Then Return 0 Return 1 EndFunc ;==>WM_COPYDATA_SendData Result: I want to find a solution to "Execute a single instance" but the new session closes the old instance or the old one exits on its own. Regards, Link to comment Share on other sites More sharing options...
Jardz Posted August 18, 2021 Share Posted August 18, 2021 (edited) Hi VIP Does Opt(WinWaitDelay,'1') improve this? edit: Not sure what I was thinking!! Ran it a few times with delay reduced and it behaved, just a coincidence. edit2: I can't see how this method can truly work in VIP's test, the way I see it multiple CPU cycles are required to look for a window and rename it's own window if not found. Because of the process queue multiple processes can be taking turns to complete that task. Need to use a mutex or semaphore (semaphore if you want to set max nth of running) BTW. Singleton() (mutex method) has always worked for me. Regards Sjardz Edited August 19, 2021 by Jardz Trong 1 Link to comment Share on other sites More sharing options...
Trong Posted August 19, 2021 Share Posted August 19, 2021 (edited) Yes, using singleton does not subject to application repetition. I found the solution to my problem: Edited August 19, 2021 by VIP Problem solved Regards, 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