Search code examples
javawindowsnsis

How does one get the window handle of a Java Swing app in NSIS?


I'm using NSIS to create an update installer for a Swing based Java application. I need to know whether the application is already running and abort the installation if so. I asked this here, but it essentially boils down to identifying the application by its window title.

NSIS has a FindWindow function that takes the window handle as a compulsory parameter. Now for my question: How do you find out the window handle of a Swing app, given that you don't have Spy++ or WinSpy++, and are blocked by wonderful corporate IT from downloading additional tools? Do Java applications have a standard window handle name?


Solution

  • You can use NSIS to enumerate windows, the System plugin readme has a EnumChildWindows example that is very close to the code required to enumerate top level windows.

    !include LogicLib.nsh
    showinstdetails show
    section
    
    System::Get "(i.r1) iss"
    Pop $R0
    System::Call "user32::EnumWindows(k R0,i) i.s"
    loop:
        Pop $0
        StrCmp $0 "callback1" 0 done
        System::Call "user32::IsWindowVisible(ir1)i.r2"
        ${If} $2 <> 0
            System::Call "user32::GetWindowText(ir1,t.r2,i${NSIS_MAX_STRLEN})"
            System::Call "user32::GetClassName(ir1,t.r3,i${NSIS_MAX_STRLEN})"
            IntFmt $1 "0x%X" $1
            DetailPrint "$1 - [$3] $2"
        ${EndIf}
        Push 1 # callback's return value
        System::Call "$R0"
        Goto loop
    done:
    System::Free $R0
    
    sectionend
    

    If you control the Swing app code you can use other methods to check for a running instance, you can create a Win32 mutex or other named kernel objects and check for those...