Process name from Process ID in BASIC

How can I get the process name from Process ID in BASIC? (under WinXP)
lucavillaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sirbountyCommented:
Basic?
0
lucavillaAuthor Commented:
The automation program that I use, Automate, accept "BASIC scripts"
0
sirbountyCommented:
Is that 'pure' BASIC though?  Gosh, I haven't seen that in ages...
Would a wmi call work?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

lucavillaAuthor Commented:
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
sirbountyCommented:
Okay - that looks like Visual Basic - what does that not do that you're trying to accomplish?
0
lucavillaAuthor Commented:
This script returns process ID from window handle
I still need process name from process ID
0
mxjijoCommented:
0
lucavillaAuthor Commented:
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
mxjijoCommented:
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
EDDYKTCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lucavillaAuthor Commented:
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
EDDYKTCommented:
objProcess.ExecutablePath
0
lucavillaAuthor Commented:
You are a God EDDYKT!
Is there a way to give you 5000 points??
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.