Page 2 of 2
Posted: Wed Dec 12, 2007 4:08 am
by npath
PB,
Thanks for the snippet "FindPartWin". This is something I have been working on recently. Very useful.
npath
Posted: Wed Dec 12, 2007 4:12 am
by npath
PB,
Your code works only around 50% of the time on my system:
Vista, Intel Core 2 Duo
npath
Posted: Wed Dec 12, 2007 4:54 am
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
Posted: Wed Dec 12, 2007 9:12 am
by PB
> SFSxOI, Thanks for the snippet "FindPartWin". This is something I have
> been working on recently. Very useful.
No problem.

Posted: Wed Dec 12, 2007 9:13 am
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.
Re: Window handle from process handle?
Posted: Wed Dec 12, 2007 10:44 am
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")
Posted: Wed Dec 12, 2007 9:53 pm
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.

Re: Window handle from process handle?
Posted: Sun Jan 20, 2008 11:41 am
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
Posted: Mon Feb 23, 2009 12:09 pm
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.
Posted: Mon Feb 23, 2009 9:27 pm
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..
