Jump to content
Advert

Recommended Posts

  • Solution
Posted (edited)

Excellent thinking, argumentum !
Using the hidden AutoIt window title (AutoItWinSetTitle) as a dynamic registry to track which SciTE HWND already has an active overlay
is a smart way to auto-target 'orphaned' SciTE windows.
This completely eliminates the need to guess via [ACTIVE] focus.

However, there is a catch when launching this directly from SciTE's Tools Menu.
If the architecture matches, the script bypasses the restart and directly enters the main loop without ever releasing the initial calling process.
As a result, SciTE's UI/menu will stay blocked (waiting for an exit code).

To fix this, we need to inject the Detachment Launcher (ShellExecute + Exit) right after we successfully detect the orphaned SciTE handle,
forcing the first instance to return Exit code: 1 immediately.

Here is the adaptable version of your approach:

Func _SciTE_ArchSlector()
    Local $hSciTE = 0

    ; Check if a valid Handle was passed via Command Line parameters (from a previous restart/detach)
    If $CmdLine[0] > 0 Then
        Local $hParam = HWnd($CmdLine[1])
        If IsHWnd($hParam) And WinExists($hParam) Then $hSciTE = $hParam
    EndIf

    ; SINGLETON CHECK: prevent duplicate overlay instances
    If $hSciTE And WinExists('STUB-' & @ScriptName & '-' & String($hSciTE)) Then Exit 1 
    
    ; TARGET ACQUISITION: If no handle was passed via CMD, locate the active or orphaned SciTE window
    If Not $hSciTE Then
        Local $hWnd = WinGetHandle("[ACTIVE]")
        If _WinAPI_GetClassName($hWnd) = 'SciTEWindow' Then
            $hSciTE = $hWnd
        Else
            ; Active window is not SciTE, scan all available SciTE windows for an unmanaged one
            Local $aWinList = WinList('[CLASS:SciTEWindow]')
            If IsArray($aWinList) Then
                For $n = 1 To $aWinList[0][0]
                    If WinExists('STUB-' & @ScriptName & '-' & String($aWinList[$n][1])) Then ContinueLoop
                    $hSciTE = $aWinList[$n][1]
                    ExitLoop
                Next
            EndIf
        EndIf

        ; Double-check to prevent race conditions before launching the detached process
        If $hSciTE And WinExists('STUB-' & @ScriptName & '-' & String($hSciTE)) Then Exit 1

        ; Fire the independent background instance and instantly release SciTE's UI (Exit Code 1)
        If $hSciTE Then Exit ShellExecute(@AutoItExe, '"' & @ScriptFullPath & '" "' & String($hSciTE) & '"') * 0 + 1
        
    EndIf

    ; If still no valid SciTE window handle is found, we cannot proceed
    If Not $hSciTE Then Exit MsgBox(16, "Error", "SciTE Window not found.")

    ; ARCHITECTURE CHECK: Match execution environment with target SciTE process architecture
    Local $iPID
    _WinAPI_GetWindowThreadProcessId($hSciTE, $iPID)

    Local $bSciTEIs64 = False
    If @OSArch = "X64" Then
        If Not _WinAPI_IsWow64Process($iPID) Then $bSciTEIs64 = True
    EndIf

    ; Perform architecture hot-swap if required (Exit Code 2)
    If ($bSciTEIs64 And Not @AutoItX64) Or (Not $bSciTEIs64 And @AutoItX64) Then
        Local $sAutoItExe = $bSciTEIs64 ? StringReplace(@AutoItExe, "AutoIt3.exe", "AutoIt3_x64.exe") : StringReplace(@AutoItExe, "_x64.exe", ".exe")
        If FileExists($sAutoItExe) Then
            Exit ShellExecute($sAutoItExe, '"' & @ScriptFullPath & '" "' & String($hSciTE) & '"') * 0 + 2
        EndIf
    EndIf

    ; LOCKING: We are the final, correctly matched instance. Register the tracking stub.
    AutoItWinSetTitle('STUB-' & @ScriptName & '-' & String($hSciTE))

    Return $hSciTE
EndFunc   ;==>_SciTE_ArchSlector


Now it works perfectly, it dynamically finds the unmanaged SciTE window,
fires a detached background worker for it, and returns Exit code: 1 to SciTE in under 300ms!"


and it doesn't wait when you call it from SciTEUser.properties e.g.

# ------------------------------------------------------------------------------
# 47 SciTE_OverlayTab (Independent Background Launch)
# ------------------------------------------------------------------------------
command.name.47.*=Launch Overlay Tab
command.shortcut.47.*=Ctrl+Alt+Shift+O
command.47.*="$(autoit3dir)\autoit3.exe" /AutoIt3ExecuteScript "$(autoit3dir)\Extras\scite_overlaytab\SciTE_OverlayTab.au3"
command.subsystem.47.*=2
command.save.before.47.*=0
command.quiet.47.*=1
# ------------------------------------------------------------------------------


Now with this approach, even Singleton is not needed anymore 
I will update the original to version 1.2.1

 

Edited by ioa747

I know that I know nothing

Advert

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
×
×
  • Create New...