Window handle from process handle?
Just a slight modification to allow case insensitive searches for a window title.
npath
Code: Select all
Procedure FindPartWin(part$)
r=GetWindow_(GetDesktopWindow_(),#GW_CHILD)
Repeat
t$=Space(999) : GetWindowText_(r,t$,999)
If FindString(LCase(t$), LCase(part$),1)<>0 And IsWindowVisible_(r)=#True
w=r
Else
r=GetWindow_(r,#GW_HWNDNEXT)
EndIf
Until r=0 Or w<>0
ProcedureReturn w
EndProcedure
hwnd = FindPartWin("notepad")
If hwnd > 0
ShowWindow_(hwnd, #SW_SHOWNORMAL)
SetForegroundWindow_(hwnd)
Else
MessageRequester("", "Window not found.")
EndIf
Re: Window handle from process handle?
you compare processid (lpProc) with processhandle (PeekL(lParam)) !Mistrel wrote:I wrote this function to try and get a window handle from the process handle but it can't find any matching processes. Is there some subtle error in my code or is it my logic?
Code: Select all
Procedure GetProcessId_(hProcess) ; for windows < Vista or Windows XP SP1
Protected hThread
Protected Pid
Protected GCPI
GCPI = GetProcAddress_(GetModuleHandle_("Kernel32"),"GetCurrentProcessId");
hThread = CreateRemoteThread_(hProcess,0,0,GCPI,0,0,0)
If Not hThread
ProcedureReturn 0
EndIf
WaitForSingleObject_(hThread,#INFINITE)
GetExitCodeThread_(hThread,@Pid)
CloseHandle_(hThread)
ProcedureReturn Pid
EndProcedure
Procedure EnumWindowsProc(hWnd,lParam)
GetWindowThreadProcessId_(hWnd,@lpProc)
If lpProc=PeekL(lParam) ; process passed to EnumWindows is process found at this hwnd
PokeL(lParam,hWnd) ; set ptr to hwnd
ProcedureReturn 0
EndIf
ProcedureReturn 1
EndProcedure
Procedure GetProcHwnd(lpProc)
Protected pid = GetProcessId_(lpProc)
ptr=pid
Repeat : Until EnumWindows_(@EnumWindowsProc(),@ptr)
If ptr=pid ; if no handle is returned no matching process was found
ProcedureReturn 0
EndIf
; some form of GetWindow_() here for top-level window..
ProcedureReturn ptr
EndProcedure
program=RunProgram("Notepad.exe","","",#PB_Program_Open)
id=ProgramID(program)
hproc=OpenProcess_(#PROCESS_ALL_ACCESS,0,id)
Sleep_(200) ; wait for Notepad window
hwnd = GetProcHwnd(hproc)
child = FindWindowEx_(hwnd,0,"edit",0)
SendMessage_(child,#WM_SETTEXT,0,"this is from purebasic")
@npath - wasn't mine, I just collected it from the forum and couldn't remember who did it originally. Tried searching the forum when I posted it so I could do a link to it but for some reason I'm having problems searching the forums as lately they always seem to just time out on me or something and i get a page could not be found. Credit goes to PB for the code.
@ PB, sorry, I couldn't remember who did it, but I did mention that in my post. I usually try to keep a link and the originator of a snippet with the snippet for credit purposes but I didn't this time for some reason.
@ PB, sorry, I couldn't remember who did it, but I did mention that in my post. I usually try to keep a link and the originator of a snippet with the snippet for credit purposes but I didn't this time for some reason.
PB wrote:> SFSxOI, Thanks for the snippet "FindPartWin". This is something I have
> been working on recently. Very useful.
No problem.
-
- Enthusiast
- Posts: 135
- Joined: Sat Aug 18, 2007 7:09 am
- Location: Netherlands
Re: Window handle from process handle?
All very nice and well, but doesn't work for all programs.hallodri wrote:you compare processid (lpProc) with processhandle (PeekL(lParam)) !Mistrel wrote:I wrote this function to try and get a window handle from the process handle but it can't find any matching processes. Is there some subtle error in my code or is it my logic?
Code: Select all
Procedure GetProcessId_(hProcess) ; for windows < Vista or Windows XP SP1 Protected hThread Protected Pid Protected GCPI GCPI = GetProcAddress_(GetModuleHandle_("Kernel32"),"GetCurrentProcessId"); hThread = CreateRemoteThread_(hProcess,0,0,GCPI,0,0,0) If Not hThread ProcedureReturn 0 EndIf WaitForSingleObject_(hThread,#INFINITE) GetExitCodeThread_(hThread,@Pid) CloseHandle_(hThread) ProcedureReturn Pid EndProcedure Procedure EnumWindowsProc(hWnd,lParam) GetWindowThreadProcessId_(hWnd,@lpProc) If lpProc=PeekL(lParam) ; process passed to EnumWindows is process found at this hwnd PokeL(lParam,hWnd) ; set ptr to hwnd ProcedureReturn 0 EndIf ProcedureReturn 1 EndProcedure Procedure GetProcHwnd(lpProc) Protected pid = GetProcessId_(lpProc) ptr=pid Repeat : Until EnumWindows_(@EnumWindowsProc(),@ptr) If ptr=pid ; if no handle is returned no matching process was found ProcedureReturn 0 EndIf ; some form of GetWindow_() here for top-level window.. ProcedureReturn ptr EndProcedure program=RunProgram("Notepad.exe","","",#PB_Program_Open) id=ProgramID(program) hproc=OpenProcess_(#PROCESS_ALL_ACCESS,0,id) Sleep_(200) ; wait for Notepad window hwnd = GetProcHwnd(hproc) child = FindWindowEx_(hwnd,0,"edit",0) SendMessage_(child,#WM_SETTEXT,0,"this is from purebasic")
Try MS outlook express, you get a very vage handle.
This simple basic code works also with outlook:
Code: Select all
hWnd = GetForegroundWindow_()
app$ = "C:\Program Files\Outlook Express\msimn.exe" ; Full path needed!
ThreadID = RunProgram(app$, "", GetPathPart(app$), #PB_Program_Open) ;,#SW_NORMAL)
timeout = 500
Repeat
Delay(200): timeout-1
hWnd1 = GetForegroundWindow_()
If hWnd<>hWnd1
GetWindowThreadProcessId_(hWnd1,@programid)
EndIf
Until (hWnd1<>hWnd And programid=ProgramID(ThreadID)) Or timeout<1
If (hWnd1<>hWnd And programid = ProgramID(ThreadID)) And timeout>1
hWnd = hWnd1
ShowWindow_(hWnd, #SW_MINIMIZE) ;HIDE)
EndIf
-
- Enthusiast
- Posts: 135
- Joined: Sat Aug 18, 2007 7:09 am
- Location: Netherlands