Jump to content

Recommended Posts

Posted (edited)

Hello Team,

I've encountered a strange behavior and currently can not track down the reasons. Maybe it's related to my specific Installation only? Or hopefully someone else can reproduce the issue.

I've got two different approaches for a singleton script, one with CreateSemaphoreW and one with CreateFileMappingW.

; https://www.autoitscript.com/forum/topic/185203-new-_singleton-need-opinion/
; by Terenz

Local $hSemaphore = _SingletonSemaphore
If _SingletonSemaphore("TestSemaphore", 1) = 0 Then
    MsgBox(16, "Warning", "An occurrence of test is already running")
    Exit
EndIf
MsgBox(64, "OK", "The first occurrence of test is running")
DllCall("kernel32.dll", "bool", "CloseHandle", "handle", $hSemaphore)

Func _SingletonSemaphore($sOccurrenceName, $iFlag = 0)
    Local Const $ERROR_ALREADY_EXISTS = 183
    $hStartEvent = DllCall('kernel32.dll', 'handle', 'CreateSemaphoreW', 'struct*', 0, 'long', 0, 'long', 1, 'wstr', $sOccurrenceName)
    If @error Then Return SetError(@error, @extended, 0)
    Local $hError = DllCall("kernel32.dll", "dword", "GetLastError")
    If $hError[0] = $ERROR_ALREADY_EXISTS Then
        DllCall("kernel32.dll", "bool", "CloseHandle", "handle", $hStartEvent[0])
        If @error Then Return SetError(@error, @extended, 0)
        If BitAND($iFlag, 1) Then
            Return SetError($hError[0], $hError[0], 0)
        Else
            Exit -1
        EndIf
    EndIf
    Return $hStartEvent[0]
EndFunc   ;==>_SingletonSemaphore

 

; https://www.autoitscript.com/forum/topic/185203-new-_singleton-need-opinion/
; by Terenz

Local $hFilemapping = _SingletonMap("Testmapping", 1)
If $hFilemapping = 0 Then
    MsgBox(16, "Warning", "An occurrence of test is already running")
    Exit
EndIf
MsgBox(64, "OK", "The first occurrence of test is running")
DllCall("kernel32.dll", "bool", "CloseHandle", "handle", $hFilemapping)

Func _SingletonMap($sOccurrenceName, $iFlag = 0)
    Local Const $ERROR_ALREADY_EXISTS = 183
    Local $tInt64 = DllStructCreate('int64')
    Local $tQWord = DllStructCreate('dword;dword', DllStructGetPtr($tInt64))
    DllStructSetData($tInt64, 1, 1)
    Local $iSize_HiDWord = DllStructGetData($tQWord, 2), $iSize_LoDWord = DllStructGetData($tQWord, 1)
    Local $hStartEvent = DllCall('kernel32.dll', 'handle', 'CreateFileMappingW', 'handle', -1, 'struct*', 0, 'dword', 0x0004, 'dword', $iSize_HiDWord, 'dword', $iSize_LoDWord, 'wstr', $sOccurrenceName)
    If @error Then Return SetError(@error, @extended, 0)

    Local $hError = DllCall("kernel32.dll", "dword", "GetLastError")
    If $hError[0] = $ERROR_ALREADY_EXISTS Then
        DllCall("kernel32.dll", "bool", "CloseHandle", "handle", $hStartEvent[0])
        If @error Then Return SetError(@error, @extended, 0)
        If BitAND($iFlag, 1) Then
            Return SetError($hError[0], $hError[0], 0)
        Else
            Exit -1
        EndIf
    EndIf
    Return $hStartEvent[0]
EndFunc   ;==>_SingletonMap

 

Now if I compile the code and run it from the created executable, the first instance shows the "first occurrence" message and all subsequent instances (while the fist message box is still open) will result in the "already running" message box.

If I leave the "first occurrence" message box open then run the code in parallel form SciTE, that code will produce another "first occurrence" message box, but I expected an "already running" message box here too.

What might be the reason for the different results, running the code directly or through SciTE?

Edited by KaFu
  • Developers
Posted

Did you check if there is another error returned by?:

Local $hError = DllCall("kernel32.dll", "dword", "GetLastError")
    If $hError[0] = $ERROR_ALREADY_EXISTS Then

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Posted

It's getting even more strange. When I use x64 (#AutoIt3Wrapper_UseX64=y), it seems to work for me too in both directions.

 

When I switch to x86 (#AutoIt3Wrapper_UseX64=n) and

- start SciTE first, the executable can detect the existing filemapping

- start the executable first, SciTE can not detect the existing filemapping

Posted

My first questions would be:

  • Which version of AutoIt do you run?
  • Which version of Windows do you run?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted

Win11pro 24H2 = All good, no mishaps. SciTE shouldn't be a factor as far as code not running.
Then to replicate my question is what version of windows and type ( IoT, Multi-user, etc. ), is it hardware, a VM ( then VM platform ) ?

Because it makes no sense, so the closer to your environment the better.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted (edited)

Thanks for testing and following up!

So it really seems like there's something freaky in my own setup going on, good to know that at least it's not a common problem. I'll post a result, if I ever find out what it was 🙃.

Best Regards
 

Edited by KaFu
Posted (edited)

Hi @UEZ, did you ever solve this wow64 related topic here?

I have the feeling that my topic could be related to the Wow64 redirection too.

Is there anything Wow64 related that the autoit3.exe compiler does? Or in the manifest? 

My 32bit compiled scripts work as expected, un-compiled scripts running as 32 bit from SciTE fail.

Edited by KaFu
Posted

Yes, tried that, didn't change the outcome. 

Also strange is that _WinAPI_IsWow64Process() still reports 1 after the successful call of _WinAPI_Wow64EnableWow64FsRedirection(False), I would have expected a 0 then. But maybe _WinAPI_IsWow64Process() still is true even if the redirection has been disable.

Also tried "Wow64DisableWow64FsRedirection", also didn't help.

Posted (edited)
2 hours ago, KaFu said:

Hi @UEZ, did you ever solve this wow64 related topic here?

I have the feeling that my topic could be related to the Wow64 redirection too.

Is there anything Wow64 related that the autoit3.exe compiler does? Or in the manifest? 

My 32bit compiled scripts work as expected, un-compiled scripts running as 32 bit from SciTE fail.

Hi KaFu,

I now have a new notebook and have not looked into the problem since then because it works with ioa747's suggestion. Maybe I will make some tests.

Btw, I have no problems with the CreateSemaphore code from post#1.

 

Edit: still same problem with x86.

#AutoIt3Wrapper_UseX64=n
ShellExecute("C:\Users\Public\Desktop\Adobe Acrobat.lnk")

 

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted (edited)

Just wondering why API call is different when using Freebasic:

'Coded by UEZ build 2025-03-29
#include "windows.bi"

'https://learn.microsoft.com/de-de/windows/win32/api/winbase/nf-winbase-createsemaphorea
Function _WinAPI_CreateSemaphore(sOccurrenceName As String, InitCount As Long = 0, MaxCount As Long = 1) As HANDLE
    Dim As HANDLE hSemaphore = CreateSemaphore(NULL, InitCount, MaxCount, sOccurrenceName)
    If GetLastError() = ERROR_ALREADY_EXISTS Then Return 0
    If hSemaphore = 0 Then Return -1
    Return hSemaphore
End Function

Dim As HANDLE hSemaphore = _WinAPI_CreateSemaphore("1a2b3c4d5e6f")
If hSemaphore = 0 Then End MessageBox(0, "An occurrence of this app (x64 or x86) is already running! Exiting...", "ERROR", MB_OK Or MB_ICONSTOP Or MB_TOPMOST)

MessageBox(0, "Started", "Information", MB_OK Or MB_TOPMOST Or MB_ICONINFORMATION)
If hSemaphore > 0 Then 
    ReleaseSemaphore(hSemaphore, 1, NULL)
    CloseHandle(hSemaphore)
End If

In FB I call the CreateSemaphore with the key whereas in Autoit the first call is empty. Any other call with the compiled exe regardless if it is x86 or x64 it will show the MsgBox.

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...