Solved

Process name from Process ID  in BASIC

Posted on 2007-04-11
13
917 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Crystal reports - Formula Field code need assistance with code 17 52
How to Add / Edit Windows Menu 4 52
Macro Excel - Multiple If conditions 2 65
SSRS troubles 4 20
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

895 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

15 Experts available now in Live!

Get 1:1 Help Now