Solved

Process name from Process ID  in BASIC

Posted on 2007-04-11
13
948 Views
Last Modified: 2013-12-03
How can I get the process name from Process ID in BASIC? (under WinXP)
0
Comment
Question by:lucavilla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +1
13 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18890374
Basic?
0
 

Author Comment

by:lucavilla
ID: 18890495
The automation program that I use, Automate, accept "BASIC scripts"
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18890621
Is that 'pure' BASIC though?  Gosh, I haven't seen that in ages...
Would a wmi call work?
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:lucavilla
ID: 18890650
Sincerely I don't know if it's pure BASIC.
Anyway someone told me that a WMI call works.

I can also say that the following script worked for obtaining the process ID from window handle:

________________
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long

'Purpose     :  Returns the process id for the given window handle.
'Inputs      :  lHwndWindow         The handle of the window to return the process id of.
'Outputs     :  Returns the handle of process that owns the specified window handle.
'Author      :  Andrew Baker
'Date        :  01/11/2000 13:17
'Notes       :
'Revisions   :
'Assumptions :

Function GetProcessIdFromWindow(lHwndWindow As Long) As Long
    Dim lProcId As Long

    If lHwndWindow <> 0 Then
        'Get the process ID from the window handle
        Call GetWindowThreadProcessId(lHwndWindow, lProcId)
        GetProcessIdFromWindow = lProcId
    Else
        'Invalid window handle
        GetProcessIdFromWindow = 0
    End If
End Function
________________
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18890664
Okay - that looks like Visual Basic - what does that not do that you're trying to accomplish?
0
 

Author Comment

by:lucavilla
ID: 18890722
This script returns process ID from window handle
I still need process name from process ID
0
 
LVL 8

Expert Comment

by:mxjijo
ID: 18893399
0
 

Author Comment

by:lucavilla
ID: 18893582
mxjijo: I already saw that but it says that it's for getting the list of all the processes.
I need to get the process name (the .exe, or "image name"...) from the process ID.
0
 
LVL 8

Expert Comment

by:mxjijo
ID: 18893661
You need to scroll down :)

There is a function called GetProcessName(). You should be able to use it.
I am sorry I don't speak much BASIC, so can't tell you how to use it.

hope that helps


Public Function GetProcessName(ByVal lProcessID As Long) As String
    Dim szProcessName As String
    Dim lLen As Long, hProcess As Long
    Dim alhwndMod(0 To 1023) As Long
    Dim lcbNeeded As Long
    Dim lCounter As Long
    Dim lR As Long
    Const PROCESS_QUERY_INFORMATION = &H400
    Const PROCESS_VM_READ = &H10

    lLen = MAX_PATH
   
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, lProcessID)
   
    If (lProcessID = 0) Then
        GetProcessName = "System Idle Process"
    ElseIf (lProcessID = 2) Then
        GetProcessName = "System"
    Else
        'Get the process name
        If (hProcess <> 0) Then
            If (EnumProcessModules(hProcess, alhwndMod(0), 1024 * 4, lcbNeeded)) Then
                szProcessName = String$(lLen, 0)
                LSet szProcessName = "unknown"
                lR = GetModuleBaseName(hProcess, alhwndMod(lCounter), szProcessName, lLen)
                GetProcessName = Left$(szProcessName, InStr(szProcessName, vbNullChar) - 1)
            End If
        End If
    End If
    CloseHandle hProcess
End Function

'Demonstration routine
Sub Test()
    Dim lThisProc As Long, lNumProcs As Long, asProcNames() As String, alProcIDs() As Long
   
    'WIN NT Test
    lNumProcs = EnumProcs(asProcNames, alProcIDs, , True)
    For lThisProc = 1 To lNumProcs
        Debug.Print asProcNames(lThisProc) & vbTab & "ID:" & alProcIDs(lThisProc)
    Next
   
    '95/98/2000 Test
    lNumProcs = EnumProcs(asProcNames, alProcIDs)
    For lThisProc = 1 To lNumProcs
        Debug.Print asProcNames(lThisProc) & vbTab & "ID:" & alProcIDs(lThisProc)
    Next

End Sub
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 500 total points
ID: 18896898
or

Public Function GetProcessName(ByVal lProcessID As Long) As String
    Dim objWMIService As Object
    Dim colProcesses As Object
    Dim objProcess As Object
   
    On Error Resume Next
    Set objWMIService = GetObject("winmgmts:")
    Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process")
    For Each objProcess In colProcesses
        If (objProcess.Handle = lProcessID) Then
            GetProcessName = objProcess.Caption
            Exit Function
        End If
    Next
End Function
0
 

Author Comment

by:lucavilla
ID: 18899813
EDDYKT: WONDERFUL! :-D I love it!
Before I close this question accepting your solution, do you also know how to obtain the full path?

0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 18900190
objProcess.ExecutablePath
0
 

Author Comment

by:lucavilla
ID: 18901679
You are a God EDDYKT!
Is there a way to give you 5000 points??
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question