Solved

Process name from Process ID  in BASIC

Posted on 2007-04-11
13
907 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now