Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Process name from Process ID  in BASIC

Posted on 2007-04-11
13
Medium Priority
?
965 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

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 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

610 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