By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,318 Members | 2,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,318 IT Pros & Developers. It's quick & easy.

Getting the process from the Application

P: 2
Hi,
If I have the Application Handle (of say Notepad), how can i get the thread/process name i.e. Notepad.exe or other information?
I tried to use this:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_process.asp
But unfortunately, the handle property does not contain the application handle, but the process's handle.

Thanks in Advance!

Regards,
Prakhar Birla
Nov 21 '06 #1
Share this Question
Share on Google+
1 Reply


P: 2
Thank you for the trouble people, but i figured it out for myself! Here it is, if someone else needs it
Expand|Select|Wrap|Line Numbers
  1. Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Long, ByRef lpdwProcessId As Long) As Long
  2.  
  3. Function getPIDfromHandle(target_hwnd As Long) As Long
  4. Dim test_pid As Long
  5. Dim test_threadid As Long
  6. test_hwnd = final_hwnd
  7. test_threadid = GetWindowThreadProcessId(target_hwnd, test_pid)
  8. getPIDfromHandle = test_pid
  9. End Function
  10.  
  11. Function getPathfromPID(PID As Long) As String
  12. Dim strComputer As String
  13. strComputer = "."
  14. Set objWMIService = GetObject("winmgmts:" _
  15.     & "{impersonationLevel=impersonate}!\\" _
  16.     & strComputer & "\root\cimv2")
  17.  
  18. Set colProcesses = objWMIService.ExecQuery( _
  19.     "select * from win32_process")
  20. getPathfromPID = Null
  21. For Each objProcess In colProcesses
  22.    If objProcess.ProcessID = PID Then
  23.    getPathfromPID = objProcess.ExecutablePath
  24.    Exit For
  25.    End If
  26. Next
  27. End Function
Nov 21 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.