Window handle from process handle?

Just starting out? Need help? Post your questions and find answers here.
npath
User
User
Posts: 74
Joined: Tue Feb 15, 2005 5:15 pm

Post by npath »

PB,

Thanks for the snippet "FindPartWin". This is something I have been working on recently. Very useful.

npath
Last edited by npath on Sat Dec 15, 2007 12:28 am, edited 1 time in total.
npath
User
User
Posts: 74
Joined: Tue Feb 15, 2005 5:15 pm

Post by npath »

PB,

Your code works only around 50% of the time on my system:

Vista, Intel Core 2 Duo


npath
npath
User
User
Posts: 74
Joined: Tue Feb 15, 2005 5:15 pm

Post by npath »

Just a slight modification to allow case insensitive searches for a window title.

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
npath
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> SFSxOI, Thanks for the snippet "FindPartWin". This is something I have
> been working on recently. Very useful.

No problem. ;)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> Your code works only around 50% of the time on my system:
> Vista, Intel Core 2 Duo

It seems dual core processors have problems then. Thanks for testing.
User avatar
hallodri
Enthusiast
Enthusiast
Posts: 208
Joined: Tue Nov 08, 2005 7:59 am
Location: Germany
Contact:

Re: Window handle from process handle?

Post by hallodri »

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?
you compare processid (lpProc) with processhandle (PeekL(lParam)) !

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")



SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

@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 wrote:> SFSxOI, Thanks for the snippet "FindPartWin". This is something I have
> been working on recently. Very useful.

No problem. ;)
Philippe-felixer76-2
Enthusiast
Enthusiast
Posts: 135
Joined: Sat Aug 18, 2007 7:09 am
Location: Netherlands

Re: Window handle from process handle?

Post by Philippe-felixer76-2 »

hallodri wrote:
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?
you compare processid (lpProc) with processhandle (PeekL(lParam)) !

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")



All very nice and well, but doesn't work for all programs.
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
SeregaZ
Enthusiast
Enthusiast
Posts: 628
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Post by SeregaZ »

are you sure this code with find part name working? and support them another language - example russion? :))))
on my PC is not working. find only if you put in parametr full name
aaaand not working if you put part name with space.
Philippe-felixer76-2
Enthusiast
Enthusiast
Posts: 135
Joined: Sat Aug 18, 2007 7:09 am
Location: Netherlands

Post by Philippe-felixer76-2 »

SeregaZ wrote:are you sure this code with find part name working? and support them another language - example russion? :))))
on my PC is not working. find only if you put in parametr full name
aaaand not working if you put part name with space.
Ehh.. no .. not sure.. ;-)
Post Reply