Les Snippets

Connexion

Chemin de l'application associé à une fenêtre

Niveau requis pour utiliser/comprendre cette source : 2 ( Initié )
Créé le 21/07/2009 15:01:33 et initié par Charles Racaud [Liste]
Vue : 10105
Catégorie(s) : API, Fichier / Disque, WinForm
Langages dispo pour ce code :
- C# 1.x, C# 2.x, C# 3.x
- VB6, VBA
- VB 2005, VB 2008, VB.NET 1.x



Langage : C# 1.x , C# 2.x , C# 3.x
Date ajout : 21/07/2009
Posté par Charles Racaud [Liste]
//using System;
//using System.Runtime.InteropServices;
//using System.Text;
private const int MAX_PATH = 260;
private const int PROCESS_QUERY_INFORMATION = 0x400;
private const int PROCESS_VM_READ = 0x10;
[DllImport("user32", SetLastError = true)]
private static extern int GetWindowThreadProcessId(IntPtr hWnd, ref int lpdwProcessId);
[DllImport("kernel32", SetLastError = true)]
private static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
[DllImport("kernel32", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hObject);
[DllImport("psapi", SetLastError = true)]
private static extern int GetModuleFileNameEx(IntPtr hProcess, IntPtr hModules, StringBuilder lpFileName, int nSize);

private static string GetExeFilePathFromHandle(IntPtr Handle) {
  StringBuilder Buffer = new StringBuilder(MAX_PATH);
  int Pid = 0x0;
  IntPtr hProcess = IntPtr.Zero;
  GetWindowThreadProcessId(Handle, ref Pid);
  hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, Pid);
  Buffer.Length = GetModuleFileNameEx(hProcess, IntPtr.Zero, Buffer, MAX_PATH);
  CloseHandle(hProcess);
  return Buffer.ToString();
}
Langage : VB6 , VBA
Date ajout : 21/07/2009
Posté par Charles Racaud [Liste]
Private Const MAX_PATH As Long = 260&
Private Const PROCESS_QUERY_INFORMATION As Long = &H400
Private Const PROCESS_VM_READ As Long = &H10
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As LongAs Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As LongByVal bInheritHandle As LongByVal dwProcessId As LongAs Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As LongAs Long
Private Declare Function GetModuleFileNameEx Lib "psapi" Alias "GetModuleFileNameExW" (ByVal hProcess As LongByVal hModule As LongByVal lpFileName As LongByVal nSize As LongAs Long
Private Function GetExeFilePathFromHandle(ByVal hWnd As LongAs String
  Dim Buffer As String
  Dim BufferLength As Long
  Dim Pid As Long
  Dim hProcess As Long
  Buffer = VBA.String$(MAX_PATH, ChrW$(0))
  Call GetWindowThreadProcessId(hWnd, Pid)
  hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0&, Pid)
  BufferLength = GetModuleFileNameEx(hProcess, 0&, StrPtr(Buffer), MAX_PATH)
  Call CloseHandle(hProcess)
  GetExeFilePathFormHandle = VBA.Left$(Buffer, BufferLength)
End Function
Langage : VB.NET 1.x , VB 2005 , VB 2008
Date ajout : 21/07/2009
Posté par Charles Racaud [Liste]
'Imports System
'Imports System.Runtime.InteropServices
'Imports System.Text
Private Const MAX_PATH As Integer = 260
Private Const PROCESS_QUERY_INFORMATION As Integer = &H400
Private Const PROCESS_VM_READ As Integer = &H10
<DllImport("user32", SetLastError:=True)> _
Private Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, ByRef lpdwProcessId As IntegerAs Integer
End Function
<DllImport("kernel32", SetLastError:=True)> _
Private Function OpenProcess(ByVal dwDesiredAccess As IntegerByVal bInheritHandle As BooleanByVal dwProcessId As IntegerAs IntPtr
End Function
<DllImport("kernel32", SetLastError:=True)> _
Private Function CloseHandle(ByVal hObject As IntPtr) As Boolean
End Function
<DllImport("psapi", SetLastError:=True)> _
Private Function GetModuleFileNameEx(ByVal hProcess As IntPtr, ByVal hModule As IntPtr, ByVal lpFileName As StringBuilder, ByVal nSize As IntegerAs Integer
End Function

Private Function GetExeFilePathFromHandle(ByVal Handle As IntPtr) As String
  Dim Buffer As New StringBuilder(MAX_PATH)
  Dim Pid As Integer
  Dim hProcess As IntPtr
  Call GetWindowThreadProcessId(Handle, Pid)
  hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, False, Pid)
  Buffer.Length = GetModuleFileNameEx(hProcess, IntPtr.Zero, Buffer, MAX_PATH)
  Call CloseHandle(hProcess)
  Return Buffer.ToString()
End Function

Snippets en rapport avec : Application, Chemin, Handle, Hwnd, Fenêtre



Codes sources en rapport avec : Application, Chemin, Handle, Hwnd, Fenêtre

{Visual Basic, VB6, VB.NET, VB 2005} RÉCUPÉRER PID PROCESSUS, NOM DE FICHIER.EXE ET HANDLE FENÊTRE À PARTIR DE PID OU NOM DE FICHIER.EXE OU DE HANDLE FENÊTRE
6 fonctions pour processus. GetProcessPidByExeName, GetProcessPidByHwnd, GetProcessHwndByPid, GetPro...

{Visual Basic, VB6, VB.NET, VB 2005} FERMER UN PROGRAMME, UNE APPLICATION, UNE FENÊTRE... D'APRÈS UNE PARTIE DE SON NOM
Permet en quelques lignes de fermer un programme, une application, une fenêtre... en passant en para...

{Visual Basic, VB6, VB.NET, VB 2005} CAPTEUR DE HANDLE, DE TITRE, DE CLASS, DE POSITION DE TAILLE ET DE PATH
Ayant eu besoin de trouver des Handles pour mon programme de carnet d'adresses: http://www.vbfrance....

{Visual Basic, VB6, VB.NET, VB 2005} HANDLISTER
Ce petit programme permet de surveiller les applications qui sont démarrées ou arrêtées et d'écrire ...

{Visual Basic, VB6, VB.NET, VB 2005} SENDKEYSEX => SIMULER UNE TOUCHE DANS UNE FENETRE PRÉCISE
SendKeys, keybd_event & co c'est bien joli, seul souci majeur, on ne peut pas spécifier de fenêtre c...

{Delphi} ORGDOT : ARRANGEUR DE FENÊTRES WINDOWS
Mon code est issu d'un constat : les fonctions Mosaïque ... de la barre de tâche ne permettent pas d...

{Visual Basic, VB6, VB.NET, VB 2005} HANDLE , API ET MANIPULATION DE FENÊTRE S
Voici une source regroupant une bonne dizaines d'API permettant grâce au handle d'une fenêtre d'y op...

{Visual Basic, VB6, VB.NET, VB 2005} LANCER UN FICHIER ET RÉCUPÉRER LE HANDLE DE L'APPLICATION LANCÉE
Je vois que tout le monde galère pour retrouver le handle d'une fenêtre ou encore le handle de l'app...

{Visual Basic, VB6, VB.NET, VB 2005} PROCESS THREADS MODULES HANDLES DANS UN TREEVIEW EN .NET ( OUI MADAME )
Cette source est ma première tentative en vb .net. il faut savoir que j'ai pas la msdn .net... Qu'...

{Visual Basic, VB6, VB.NET, VB 2005} MANIPULATION DES PROCESS ET HANDLES - OPTIONS INÉDITES...
Bon ben comme d'habitude, il liste tous les programmes lancés sur l'ordi, même ceux qui sont cachés ...