Search the Community
Showing results for tags 'atom'.
-
Here another approach to check if a script was already started using atoms and semaphores. Atom: #include <MsgBoxConstants.au3> Global $iSingleton = Singleton() If Not $iSingleton Then Exit MsgBox($MB_TOPMOST, "Singleton Test", "Process is already running!") EndIf MsgBox($MB_TOPMOST, "Singleton Test", "Singleton atom initialized: " & $iSingleton) Singleton_Delete($iSingleton) ; #FUNCTION# ==================================================================================================================== ; Name ..........: Singleton ; Description ...: Checks if the script has been started already. ; Syntax ........: Singleton([$sOccurrenceName = @ScriptFullPath]) ; Parameters ....: $sOccurrenceName - [optional] a string value. Default is @ScriptFullPath. ; Return values .: If the function succeeds, the return value is the newly created atom or 0 else error is set and false is returned. ; Author ........: UEZ ; Modified ......: ; Remarks .......: If Singleton finds the atom it will return 0 and the atom token will be set to extended macro. It can be used to get the atom string using _WinAPI_AtomGlobalGetName. ; Related .......: ; Link ..........: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globalfindatomw ; https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globaladdatomw ; Example .......: No ; =============================================================================================================================== Func Singleton($sOccurrenceName = @ScriptFullPath) Local $iFind = _WinAPI_AtomGlobalFind($sOccurrenceName) If @error Then Return SetError(1, 0, False) If $iFind Then Return SetExtended($iFind, 0) Local $iAtom = _WinAPI_AtomGlobalAdd($sOccurrenceName) If @error Then Return SetError(2, 0, False) Return $iAtom EndFunc ;==>Singleton ; #FUNCTION# ==================================================================================================================== ; Name ..........: Singleton_Delete ; Description ...: Deletes the atom generated by the first started script. ; Syntax ........: Singleton_Delete($iAtom) ; Parameters ....: $iAtom - an integer value which was generated by Singleton ; Return values .: True if successful else false. ; Author ........: UEZ ; Modified ......: ; Remarks .......: Don't forget to call Singleton_Delete before first started script ends. ; Related .......: ; Link ..........: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globaldeleteatom ; Example .......: No ; =============================================================================================================================== Func Singleton_Delete($iAtom) _WinAPI_AtomGlobalDelete($iAtom) If @error Then Return SetError(1, 0, False) Return True EndFunc ;==>Singleton_Delete ;internal functions Func _WinAPI_AtomGlobalFind($sAtomString) Local $aReturn = DllCall("kernel32.dll", "short", "GlobalFindAtomW", "wstr", $sAtomString) If @error Then Return SetError(1, 0, -1) Return $aReturn[0] EndFunc ;==>_WinAPI_AtomGlobalFind Func _WinAPI_AtomGlobalAdd($sAtomString) Local $aReturn = DllCall("kernel32.dll", "short", "GlobalAddAtomW", "wstr", $sAtomString) If @error Then Return SetError(1, 0, -1) Return $aReturn[0] EndFunc ;==>_WinAPI_AtomGlobalAdd Func _WinAPI_AtomGlobalDelete($nAtom) Local $aReturn = DllCall("kernel32.dll", "short", "GlobalDeleteAtom", "short", $nAtom) If @error Then Return SetError(1, 0, -1) Return $aReturn[0] = 0 EndFunc ;==>_WinAPI_AtomGlobalDelete Func _WinAPI_AtomGlobalGetName($nAtom, $iBufferSize = 512) Local $tBufferAtom = DllStructCreate("wchar name[" & $iBufferSize & "]") Local $aReturn = DllCall("kernel32.dll", "uint", "GlobalGetAtomNameW", "short", $nAtom, "struct*", $tBufferAtom, "int", $iBufferSize) If @error Or Not $aReturn[0] Then Return SetError(1, 0, -1) Return $tBufferAtom.name EndFunc ;==>_WinAPI_AtomGlobalGetName Semaphore: #include <MsgBoxConstants.au3> #include <WinAPIError.au3> Global $iSingleton = Singleton("&]8h/x87</htFV4-K*&.b.w~") If Not $iSingleton Then Exit MsgBox($MB_TOPMOST, "Singleton Test", "Process is already running!") EndIf MsgBox($MB_TOPMOST, "Singleton Test", "Singleton Semaphore initialized: " & $iSingleton) ; #FUNCTION# ==================================================================================================================== ; Name ..........: Singleton ; Description ...: Checks if the script has been started already. ; Syntax ........: Singleton($sOccurrenceName) ; Parameters ....: $sOccurrenceName - a string value which will be used to create the semaphore handle. ; Return values .: True if Singleton started the first time. False if script was already started ; Author ........: UEZ ; Modified ......: ; Remarks .......: The system closes the handle automatically when the process terminates. The semaphore object is destroyed when its last handle has been closed. ; Related .......: ; Link ..........: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsemaphorea ; Example .......: No ; =============================================================================================================================== Func Singleton($sOccurrenceName) If StringLen($sOccurrenceName) > 260 Then $sOccurrenceName = StringLeft($sOccurrenceName, 260) Local $aReturn = DllCall("kernel32.dll", "handle", "CreateSemaphoreA", "ptr", Null, "long", 0, "long", 1, "str", $sOccurrenceName) If @error Or Not $aReturn[0] Then Return SetError(1, 0, -1) Return SetExtended($aReturn[0], $aReturn[0] And _WinAPI_GetLastErrorMessage() = "The operation completed successfully.") EndFunc ;==>Singleton Just start the script twice to see if it works. The disadvantage of using atoms is that atoms have a memory that means when your app is crashing or you forgot to delete the atom then the atom does still have the $sOccurrenceName saved and thus Singleton will not work if you use the same same value for $sOccurrenceName. With semaphore you don't have this issue. Thanks to jj2007 and SARG.
-
Atom Table UDF Local and Global Atom Table Since I've had this collecting dust on my computer, I figured I'd release it to see if anyone can find some use for it. About Atom Tables - MSDN Example Global Atom Table Listing Basically, a bunch of strings can be stored locally (at program level) or globally (at O/S level) with unique numerical identifiers. This UDF lets you add, find, delete, and query these atoms. Description from the top of the AtomFunctions UDF: ---------------------------------------------------------------- Functions for accesing Local and Global Atom Tables, which contain strings up to 255 characters long The Atom tables are used for multiple types of Windows data including Window Classes (RegisterClass/Ex), Clipboard formats (RegisterClipboardFormat), Hotkeys (RegisterHotKey), and Dynamic Data Exchange (DDE) which is a form of interprocess communication. (Some of the above may use a Local rather than the Global Atom table) Other uses can include storing program strings to lookup by a 16-bit value and interprocess communication "RegisterHotKey function" @ MSDN states this: "An application must specify an id value in the range 0x0000 through 0xBFFF. A shared DLL must specify a value in the range 0xC000 through 0xFFFF (the range returned by the GlobalAddAtom function). To avoid conflicts with hot-key identifiers defined by other shared DLLs, a DLL should use the GlobalAddAtom function to obtain the hot-key identifier." Kernel Note: The Atom Table is a separate entity from Kernel Objects like Events, Mutexes, Files, etc. However, the Atom Table is (separately) accessible from within the Kernel, so its like a Kernish object? Notes on Atom Tables: Stringlimit is 255 characters NOT including a null-terminator. This is unfortunately not enough for a MAX_PATH string, which is 259+null-term. However, stripping off root-drive prefix get you within 1 character of the length (259 - 3 ["C:"] = 256] which may be enough - but using IPC, one can store 4 - 6 (8-12 in x64) extra chars in wParam/lParam.. See <AtomExample_IPC.au3> for an implementation of this There is both a Local and Global Atom Table. The Local one is local to this Process, and the Global one is available/accessible to/from all Processes. Atoms numbered 0 - 49151 (0xBFFF) are not actually part of the Atom Table. Querying these values will return a string representation of the number as an unsigned integer Example: 1234 is translated to "#1234" Adding atom strings that start with a "#" will return an unsigned integer if the numbers following "#" are an integer less than 49152 (1 - 49151 [0xBFFF]). Examples: "#1" => 1, "#49151" => 49151 Atoms numbered from 49152 - 65535 (from 0xC000 - 0xFFFF) DO reference the Atom Table strings Maximum Atom string length is 255 characters (not including a null-terminator) Functions: ; Local: ; _AtomTableInit() ; Initializes Local Atom Table. Optional [37 hash buckets allocated by default] ; _AtomAddLocal() ; Adds a string to the Local Atom table, returns # identifier [increments reference if already exists] ; _AtomGetNameLocal() ; Gets the Local Atom string associated with a numerical identifier ; _AtomDeleteLocal() ; Decrements the reference count of Local Atom, deletes when reaches 0 ; _AtomFindLocal() ; Finds # identifier for a string in the Local Atom table ; _AtomGetAllLocal() ; Returns all found Local atoms in an array ; Global: ; _AtomAddGlobal() ; Adds a string to the Global Atom table, returns # identifier [increments reference if already exists] ; _AtomGetNameGlobal() ; Gets the Global Atom string associated with a numerical identifier ; _AtomDeleteGlobal() ; Decrements the reference count of Global Atom, deletes when reaches 0 ; _AtomFindGlobal() ; Finds # identifier for a string in the Global Atom table ; _AtomGetAllGlobal() ; Returns all found Global atoms in an array ; 'Undocumented': ; _AtomGetGlobalTableUD() ; Using 'undocumented' function calls, gets info on all Global Atoms ; _AtomGetInfoUD() ; Returns reference count, pinned status, as well as Atom Name ---------------------------------------------------------------- There are a few 'undocumented' functions used for getting information on the Atom Table included (I can't help myself). In addition to the core UDF, there's 2 example files: AtomTableExample.au3 - This demonstrates various use - adding, deleting, and querying information in the Local and Global atom tables. (Extended info comes from 'undocumented' functions) AtomExample_IPC.au3 - This demonstrates how the Atom Table could be used in InterProcess Communication. A rather rough sketch, but shows how you could squeeze MAX_PATH pathnames into IPC messages combined with Atom tables. It's all a bit light on the documentation, but I just wanted to prevent this thing from collecting more dust Might prove useful to someone! History:: AtomTablesUDF.zip ~prev Downloads: 29