Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (lpClassName As Any, lpWindowName As Any) As Long Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function SetWindowText Lib "user32.dll" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long Private Declare Function WaitForInputIdle Lib "user32.dll" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long Private Const PROCESS_QUERY_INFORMATION As Long = (&H400) Private Const PROCESS_VM_READ As Long = (&H10) Private Const GW_HWNDNEXT As Long = 2& Private Sub Form_Load() Dim hPid As Long Dim hTestPid As Long Dim hProcess As Long Dim hOpenedWindow As Long '# Shell renvoie un Process ID, identifiant global au niveau de Windows hPid = Shell("calc.exe") '# On demande la création d'un handle propre a ton application et son espace mémoire hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, hPid) If hProcess <> -1 Then '# On va attendre que le process soit en état de répondre : que sa fenetre existe bien et réponde. Do DoEvents Loop While WaitForInputIdle(hProcess, 200) '# On libère le hProcess CloseHandle hProcess '# On récupère la première fenetre du poste hOpenedWindow = FindWindow(ByVal 0&, ByVal 0&) Do While hOpenedWindow '# Si nous faisons bien face a une fenetre de niveau 0 If GetParent(hOpenedWindow) = 0& Then '# On récupère le ProcessID de la fenetre considérée GetWindowThreadProcessId hOpenedWindow, hTestPid '# Si elle appartient au processus que nous avons lancé... If hTestPid = hPid Then '# On quitte la boucle Exit Do End If End If '# Sinon, on passe a la fenetre suivante hOpenedWindow = GetWindow(hOpenedWindow, GW_HWNDNEXT) Loop '# On modifie le titre de la calculatrice, pour bien voir que nous avons trouvé son Handle. If hOpenedWindow Then SetWindowText hOpenedWindow, "HANDLE de la calculatrice trouvé...." Else MsgBox "Fenetre non trouvée" End If End If End Sub
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionDo DoEvents Loop While WaitForInputIdle(hProcess, 200)
Do DoEvents Loop While WaitForInputIdle(hProcess, 200)
hInst = Shell("ExeVir.exe") 'Sleep 1000 Do DoEvents Loop While WaitForInputIdle(hProcess, 200) 'Call WaitForInputIdle(hProcess, 200) ' Pour rechercher ensuite la bonne fenêtre hWndApp1 = FindWindowEx(hWndApp, 0&, vbNullString, "MonProg") ' c'est la Caption de l'application SetParent hWndApp1, Me.hWnd Sleep 10 hWndApp = hWndApp1 Me.Caption = GetText(hWndApp)