Page 1 of 1

Get Screen Saver Windows Handle

Posted: Wed May 24, 2017 12:36 am
by ramme
I searched the forum and found two likely snippets:
(1)

Code: Select all

Procedure.l EnumWindowsProc(hWnd.l, lParam.l)
  Shared RunProgram_WindowID.l
  If GetWindowThreadProcessId_(hWnd, 0) = lParam
    RunProgram_WindowID = hWnd
    ProcedureReturn #False
  Else
    ProcedureReturn #True
  EndIf
EndProcedure

Procedure.l RunProgram2(Filename.s, Parameter.s, Directory.s)
  Shared RunProgram_WindowID.l
  Info.STARTUPINFO
  Info\cb = SizeOf(STARTUPINFO)
  Debug CreateProcess_(@Filename, @Parameter, 0, 0, 0, 0, 0, @Directory, @Info, @ProcessInfo.PROCESS_INFORMATION)
  Debug GetLastError_()
  RunProgram_WindowID = 0
  EnumWindows_(@EnumWindowsProc(), ProcessInfo\dwThreadId)
  ProcedureReturn RunProgram_WindowID
EndProcedure

Debug RunProgram2("C:\Windows\Mandala.scr"," /s","C:\Windows\")

(2)

Code: Select all

; will return the correct hWnd for a PID if the PiD only runs a single thread.
; for apps that run more then one thread will return the handle from the first thread belonging to the PiD
; which may not be the handle you want.

Procedure InstanceToWnd(target_pid.l)
;Find the first window
test_hwnd = FindWindow_(0,0)
While test_hwnd <> 0
  ;Check If the window isn't a child
        If GetParent_(test_hwnd) = 0 ;Then
          ;Get the window's thread
            test_thread_id = GetWindowThreadProcessId_(test_hwnd, @test_pid)
              If test_pid = target_pid ;Then
                InstanceToWnd = test_hwnd
                Break ;Exit Do
              EndIf
        EndIf
        ;retrieve the Next window
        test_hwnd = GetWindow_(test_hwnd, #GW_HWNDNEXT)
Wend
ProcedureReturn test_hwnd
EndProcedure

Debug InstanceToWnd(ProgramID(RunProgram("C:\Windows\Mandala.scr"," -s","",#PB_Program_Open) ))
My apologies for not naming the authors, but I kinda got lost in the threads.
Neither of them returns the hWnd, however. Does anyone know why?