Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21278

Get File size from a running process? from a constant name given

$
0
0
Hi. I 'm stuck with this

I can get the process path with this code:



Code:

Option Explicit

    'General Declarations
   
   
Private Const PROCESS_QUERY_INFORMATION As Long = (&H400)

Private Const PROCESS_VM_READ As Long = (&H10)


Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long

Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef lpcbNeeded As Long) As Long

Private Declare Function GetModuleFileNameEx Lib "psapi.dll" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As String, ByVal nSize As Long) As Long

   
      Private Declare Function Process32First Lib "kernel32" ( _
        ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long

      Private Declare Function Process32Next Lib "kernel32" ( _
        ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long




      Private Declare Function GetModuleFileNameExA Lib "psapi.dll" _
        (ByVal hProcess As Long, ByVal hModule As Long, _
            ByVal ModuleName As String, ByVal nSize As Long) As Long


      Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" ( _
        ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long

      Private Declare Function GetVersionExA Lib "kernel32" _
        (lpVersionInformation As OSVERSIONINFO) As Integer

      Private Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long          ' This process
        th32DefaultHeapID As Long
        th32ModuleID As Long            ' Associated exe
        cntThreads As Long
        th32ParentProcessID As Long    ' This process's parent process
        pcPriClassBase As Long          ' Base priority of process threads
        dwFlags As Long
        szExeFile As String * 260      ' MAX_PATH
      End Type

      Private Type OSVERSIONINFO
        dwOSVersionInfoSize As Long
        dwMajorVersion As Long
        dwMinorVersion As Long
        dwBuildNumber As Long
        dwPlatformId As Long          '1 = Windows 95.
                                        '2 = Windows NT

        szCSDVersion As String * 128
      End Type

      Private Const MAX_PATH = 260
      Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
      Private Const SYNCHRONIZE = &H100000
      'STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF
      Private Const PROCESS_ALL_ACCESS = &H1F0FFF
      Private Const TH32CS_SNAPPROCESS = &H2&
      Private Const hNull = 0

      Function StrZToStr(s As String) As String
        StrZToStr = Left$(s, Len(s) - 1)
      End Function

      Private Function getVersion() As Long
        Dim osinfo As OSVERSIONINFO
        Dim retvalue As Integer
        osinfo.dwOSVersionInfoSize = 148
        osinfo.szCSDVersion = Space$(128)
        retvalue = GetVersionExA(osinfo)
        getVersion = osinfo.dwPlatformId
      End Function



      Private Sub Command1_Click()
      List1.Clear
      Select Case getVersion()

      Case 1 'Windows 95/98

        Dim f As Long, sname As String
        Dim hSnap As Long, proc As PROCESSENTRY32
        hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
        If hSnap = hNull Then Exit Sub
        proc.dwSize = Len(proc)
        ' Iterate through the processes
        f = Process32First(hSnap, proc)
        Do While f
          sname = StrZToStr(proc.szExeFile)
          List1.AddItem sname
          f = Process32Next(hSnap, proc)
        Loop

      Case 2 'Windows NT

        Dim cb As Long
        Dim cbNeeded As Long
        Dim NumElements As Long
        Dim ProcessIDs() As Long
        Dim cbNeeded2 As Long
        Dim NumElements2 As Long
        Dim Modules(1 To 200) As Long
        Dim lRet As Long
        Dim ModuleName As String
        Dim nSize As Long
        Dim hProcess As Long
        Dim I As Long
        'Get the array containing the process id's for each process object
        cb = 8
        cbNeeded = 96
        Do While cb <= cbNeeded
            cb = cb * 2
            ReDim ProcessIDs(cb / 4) As Long
            lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
        Loop
        NumElements = cbNeeded / 4

        For I = 1 To NumElements
            'Get a handle to the Process
            hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
              Or PROCESS_VM_READ, 0, ProcessIDs(I))
            'Got a Process handle
            If hProcess <> 0 Then
                'Get an array of the module handles for the specified
                'process
                lRet = EnumProcessModules(hProcess, Modules(1), 200, _
                                            cbNeeded2)
                'If the Module Array is retrieved, Get the ModuleFileName
                If lRet <> 0 Then
                  ModuleName = Space(MAX_PATH)
                  nSize = 500
                  lRet = GetModuleFileNameExA(hProcess, Modules(1), _
                                  ModuleName, nSize)
                  List1.AddItem Left(ModuleName, lRet)
                End If
            End If
          'Close the handle to the process
        lRet = CloseHandle(hProcess)
        Next

      End Select
      End Sub


And I can get the size from a file with this:



Code:

Private Sub ProcSize_Click()
Dim fln, result

Dim fname As String

fname = "C:\MyUniqueprocTest.exe"

On Error GoTo Hell
fln = FileLen(Trim(fname))
result = CInt((fln / 1024) / 1024) & fln & ""
Text1.Text = result

Hell:
End Sub



But what i want to know is how do I use the first code above with the Procsize button function to get the size from a process I already know the name of, rather than using a listbox to list the process with, so I can use a textbox with the name of the process I want to check for its size?

In a much simpler way, If I already know what process i want to check the size for, what do i need change , I use the above to get the real process path with and want to use it with the procsize button i have set up?
I hope that was not confusing!

Viewing all articles
Browse latest Browse all 21278

Trending Articles