| |
|
|
Georg Teles | Simple detect one Fensterhandle anhand the Prozessnamens or. PID
Declare exe_$, hwnd_&
' from EXE-names Window-lever ermittlen
exe_$ = SubStr$( Par$( 0 ), -1, "\\" )
'exe_$ = "irgendeine.exe"
' DEF
Def CreateToolhelp32Snapshot(2) ! "Kernel32", "CreateToolhelp32Snapshot"
Def Process32First(2) ! "Kernel32", "Process32First"
Def Process32Next(2) ! "Kernel32", "Process32Next"
Def CloseHandle(1) ! "Kernel32", "CloseHandle"
Def EnumWindows(2) ! "User32", "EnumWindows"
Def GetWindowThreadProcessId(2) ! "User32", "GetWindowThreadProcessId"
' Constant
Def &TH32CS_SNAPPROCESS $2
'
Proc exe2lever
Parameters prozess$
Declare hcTH32S&, out#, pid&
Clear pid&
Struct PROCESSENTRY32 = dwSize&, cntUsage&, th32ProcessID&, th32DefaultHeapID&, th32ModuleID&, Threads&, th32ParentProcessID&, pcPriClassBase&, dwFlags&, szExeFile$( 260 )
Dim out#, PROCESSENTRY32
out#.dwSize& = SizeOf( out# )
hcTH32S& = CreateToolhelp32Snapshot( &TH32CS_SNAPPROCESS, 0 )
If hcTH32S&
If Process32First( hcTH32S&, out# )
Repeat
If Lower$( out#.szExeFile$ ) = Lower$( prozess$ )
' PID of EXE-names read
pid& = out#.th32ProcessID&
EndIf
Until ( pid& = hardship( 0 ) ) | ( Process32Next( hcTH32S&, out# ) = 0 )
' All windows go stampeded To hits with the sought PID debarked becomes
' though is it the first hits ! d.H. if EXE multiple runs, is this the Result the first Treffers !
EnumWindows( ProcAddr( "winproc", 2 ), pid& )
EndIf
CloseHandle( hcTH32S& )
EndIf
Dispose out#
Return 0
ENDPROC
' EnumWindowsProc
Proc winproc
Parameters hWnd&, lParam&
Declare lpdwProcessID&
Clear lpdwProcessID&
GetWindowThreadProcessId( hWnd&, addr( lpdwProcessID& ) )
Ifnot hardship( lpdwProcessID& = lparam& )
hwnd_& = hwnd&'----------------------------------------------| GLOBALE Variable « hwnd_& » !
return 0
EndIf
return 1
ENDPROC
' Hauptprogramm
CLS
exe2lever( exe_$ )
' Window-lever & Window-Text the sought Prozesses
print hwnd_&;" - ";GetText$( hwnd_& )
Waitinput
( deference, ausbaubar ! ) |
|
|
| |
|
|
|
Michael W. | Ifnot hardship() |
|
|
| XProfan X3System: Windows 8/10, XProfan X4 Programmieren, das spannendste Detektivspiel der Welt. | 01/14/15 ▲ |
|
|
|
|
| |
|
| |
|
|
|
Georg Teles | exactly. in the original Source ( Delphi ) stood it so similar, found I a little witty. therefore too so übernommen with the Info «( deference, ausbaubar ! )» ( or would Baustellenquellcode sooner fit )
not sure whether one in Delphi it rather make could but here The WinProc original
fountain: [...] |
|
|
| |
|
|
|
| Achso, there go 2 items Done - once the Returnwert staid - thatswhy the hardship(result) settle and thereafter again look whether windowHandle staid go should. from Pascalsicht ergibts sense.
In C or Java wäres sichtbarer...
result=ProcessID!=LParam
if (!result) ...
but too in Delphi/Pascal can well...
result:= processid<>Lparam
but the know Roland rather. |
|
|
| |
|
|
|
RGH | Yes, too there standing Result for Return Value the procedure. (alternatively can but still How former To Turbo-Pascal-times instead of "Result" whom Prozedurnamen write.)
Greeting Roland |
|
|
| XProfan X2Intel Duo E8400 3,0 GHz / 4 GB RAM / 1000 GB HDD - ATI Radeon HD 4770 512 MB - Windows 7 Home Premium 32Bit - XProfan X4 | 01/14/15 ▲ |
|
|
|
|
Georg Teles | Aah understand,
The Variable Result is a manner Return with the Difference, that Pascal The command To End; abarbeitet & WindowHandle is here a Globale Variable |
|
|
| |
|
|
|
Georg Teles | Nabend,
work straight in a Tool, with the one Program launched becomes (here cmd.exe), using and subsequently terminiert.
circa exakt the launched Program to find ergo thereafter which FensterHandle To steer, without on Findindow access (there cmd.exe Yes number of times walk can and herewith impossible becomes, that one other cmd.exe Window gesteuert becomes), becomes The function WinProc using.
Def CreateToolhelp32Snapshot(2) ! "KERNEL32", "CreateToolhelp32Snapshot"
Def Process32First(2) ! "KERNEL32", "Process32First"
Def Process32Next(2) ! "KERNEL32", "Process32Next"
Def CloseHandle(1) ! "KERNEL32", "CloseHandle"
Def EnumWindows(2) ! "USER32", "EnumWindows"
Def GetWindowThreadProcessId(2) ! "USER32", "GetWindowThreadProcessId"
Def &TH32CS_SNAPPROCESS $2
Def GetProcessId(1) ! "KERNEL32", "GetProcessId"
Def CreateProcess(10) ! "KERNEL32","CreateProcessA"
Def GetExitCodeProcess(2) ! "KERNEL32","GetExitCodeProcess"
Def TerminateProcess(2) ! "KERNEL32","TerminateProcess"
Declare Command#,StartupInfo#,ProcessInfo#,ExitCode#
Declare Terminate&,Start&,Program$
Declare lst&
Declare fwnd&, pwnd&, phnd&, gpid&
Window 1280,0-1200,800
Programm$ = "cmd.exe"
phnd& = createProcessExe(Program$)
Print "Create Process: "+Str $(phnd&)+" (0 = Fehler; >0 = Process lever)"
Print
Sleep 500
fwnd& = FindWindow("C:\windows\SYSTEM32\cmd.exe")
gpid& = GetProcessID(phnd&)
exe2whandle("cmd.exe")
Print "Gesuchtes Hanlde (probably!):"
Print "FindWindow / fwnd& (WindowHandle): ";Str $(fwnd&)
Print
Print "Ergebnis:"
Print "createProzess / phnd& (Process lever): ";Str $(phnd&)
Print "createProzess / gpid& (ProcessID): ";Str $(gpid&)
Print "createProzess / pwnd& (WindowHandle): ";Str $(pwnd&)
Print
waitinput
Print "Prozess exits: "+Str $(KillProcess(phnd&))+" (1 = OK; 0 = Error)"
waitinput
Proc exe2whandle
Parameters prozess$
Declare hcTH32S&, out#, pid&
Clear pid&
Struct PROCESSENTRY32 = dwSize&, cntUsage&, th32ProcessID&, th32DefaultHeapID&, th32ModuleID&, Threads&, th32ParentProcessID&, pcPriClassBase&, dwFlags&, szExeFile$( 260 )
Dim out#, PROCESSENTRY32
out#.dwSize& = SizeOf(out#)
hcTH32S& = CreateToolhelp32Snapshot(&TH32CS_SNAPPROCESS,0)
If hcTH32S&
If Process32First(hcTH32S&,out#)
Repeat
If Lower$(out#.szExeFile$) = Lower$(prozess$)
' PID of EXE-names read
pid& = out#.th32ProcessID&
EndIf
Until (pid& = hardship(0)) | (Process32Next(hcTH32S&,out#)=0)
' All windows go stampeded To hits with the sought PID debarked becomes
' though is it the first hits ! d.H. if EXE multiple runs, is this the Result the first Treffers !
EnumWindows(ProcAddr("winproc",2),pid&)
EndIf
CloseHandle(hcTH32S&)
EndIf
Dispose out#
Return 0
ENDPROC
Proc winproc
Parameters hWnd&, lParam&
Declare lpdwProcessID&
Clear lpdwProcessID&
GetWindowThreadProcessId(hWnd&,addr(lpdwProcessID&))
If (lpdwProcessID& = lparam&)
If lpdwProcessID& = gpid&
AddStrings(lst&,Str $(hwnd&)+"|"+Str $(lpdwProcessID&))
pwnd& = hwnd&'----------------------------------------------| pwnd& is here global !
return 0
EndIf
EndIf
return 1
ENDPROC
Proc createProcessExe
Parameters command$
Declare handle&
Dim Command#,(Len(command$)+1)
Dim StartupInfo#,68
Clear startupinfo#
Long StartupInfo#,0=68
Dim ProcessInfo#,16
Clear ProcessInfo#
String Command#,0 = command$
CreateProcess(0,Command#,0,0,0,0,0,0,StartupInfo#,ProcessInfo#)
Let handle& = Long(ProcessInfo#,0)
Dispose Command#
Dispose StartupInfo#
Dispose ProcessInfo#
Return handle&
ENDPROC
Proc KillProcess
Parameters hprocess&
Dim exitcode#,4
GetExitCodeProcess(hprocess&,exitcode#)
TerminateProcess(hprocess&,Long(exitcode#,0))
Dispose exitcode#
ENDPROC
EDIT: near It's all right still plainer with WinExec
Def CreateToolhelp32Snapshot(2) ! "KERNEL32", "CreateToolhelp32Snapshot"
Def Process32First(2) ! "KERNEL32", "Process32First"
Def Process32Next(2) ! "KERNEL32", "Process32Next"
Def CloseHandle(1) ! "KERNEL32", "CloseHandle"
Def EnumWindows(2) ! "USER32", "EnumWindows"
Def GetWindowThreadProcessId(2) ! "USER32", "GetWindowThreadProcessId"
Def &TH32CS_SNAPPROCESS $2
Declare lst&, Program$
Declare fwnd&, pwnd&, phnd&, gpid&
Window 1280,0-1200,800
Programm$ = "cmd.exe"
gpid& = WinExec(Program$,1)
Print "PID WinExec: "+Str $(gpid&)+" (0 = Fehler; >0 = PID)"
Print
Sleep 100
fwnd& = FindWindow("C:\windows\SYSTEM32\cmd.exe")
exe2whandle("cmd.exe")
Print "Gesuchtes Hanlde (probably!):"
Print "FindWindow / fwnd& (WindowHandle): ";Str $(fwnd&)
Print
Print "Ergebnis:"
Print "createProzess / gpid& (ProcessID): ";Str $(gpid&)
Print "createProzess / pwnd& (WindowHandle): ";Str $(pwnd&)
Print
waitinput
Print "Prozess exits: "+Str $(Process("Kill", gpid&, 0))+" (1 = OK; 0 = Error)"
waitinput
Proc exe2whandle
Parameters prozess$
Declare hcTH32S&, out#, pid&
Clear pid&
Struct PROCESSENTRY32 = dwSize&, cntUsage&, th32ProcessID&, th32DefaultHeapID&, th32ModuleID&, Threads&, th32ParentProcessID&, pcPriClassBase&, dwFlags&, szExeFile$( 260 )
Dim out#, PROCESSENTRY32
out#.dwSize& = SizeOf(out#)
hcTH32S& = CreateToolhelp32Snapshot(&TH32CS_SNAPPROCESS,0)
If hcTH32S&
If Process32First(hcTH32S&,out#)
Repeat
If Lower$(out#.szExeFile$) = Lower$(prozess$)
' PID of EXE-names read
pid& = out#.th32ProcessID&
EndIf
Until (pid& = hardship(0)) | (Process32Next(hcTH32S&,out#)=0)
' All windows go stampeded To hits with the sought PID debarked becomes
' though is it the first hits ! d.H. if EXE multiple runs, is this the Result the first Treffers !
EnumWindows(ProcAddr("winproc",2),pid&)
EndIf
CloseHandle(hcTH32S&)
EndIf
Dispose out#
Return 0
ENDPROC
Proc winproc
Parameters hWnd&, lParam&
Declare lpdwProcessID&
Clear lpdwProcessID&
GetWindowThreadProcessId(hWnd&,addr(lpdwProcessID&))
If (lpdwProcessID& = lparam&)
If lpdwProcessID& = gpid&
AddStrings(lst&,Str $(hwnd&)+"|"+Str $(lpdwProcessID&))
pwnd& = hwnd&'----------------------------------------------| pwnd& is here global !
return 0
EndIf
EndIf
return 1
ENDPROC
Regards |
|
|
| |
|
|