Results 1 to 3 of 3

Thread: [RESOLVED] Excel Crash when call API function QueryFullProcessImageName in excel VBA of win7

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2015
    Posts
    2

    Resolved [RESOLVED] Excel Crash when call API function QueryFullProcessImageName in excel VBA of win7

    when call QueryFullProcessImageName(), then excel crash,please help

    Code:
    Option Explicit
    Private Const TH32CS_SNAPPROCESS = &H2
    Private Const INVALID_HANDLE_VALUE = -1&
    Private Const MAX_PATH = 260
    Private Const PROCESS_QUERY_INFORMATION = &H400 'Ê®½øÖƵÄ1024
    Private Const PROCESS_ALL_ACCESS As Long = &H1F0FFF
    Private Const PROCESS_VM_READ = 16
    Private Const PROCESS_QUERY_LIMITED_INFORMATION = &H1000
    
    Private Const TOKEN_QUERY = &H8
    Private Const TokenUser = 1
    
    Private Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szExeFile As String * MAX_PATH
    End Type
    
    
    
    Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
    Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, sPE32 As PROCESSENTRY32) As Long
    Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, sPE32 As PROCESSENTRY32) As Long
    Public Declare Function QueryFullProcessImageName Lib "Kernel32.dll" Alias "QueryFullProcessImageNameA" (ByVal hProcess As Long, ByVal dwFlags As Long, ByVal lpExeName As String, ByVal lpdwSize As Long) As Long
    
    
    
    Public Declare Function OpenProcess Lib "kernel32" ( _
        ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
        ByVal dwProcessId As Long) As Long
    
    Public Declare Function CloseHandle Lib "kernel32" ( _
        ByVal hObject As Long) As Long
    
    Private Function ExePathFromProcID(idproc As Long) As String
        Const MAX_PATH = 1024
        Const PROCESS_QUERY_INFORMATION = &H400
        Const PROCESS_VM_READ = &H10
    
    
    
        Dim sBuf As String
        Dim sChar As Long, l As Long, hProcess As Long
        sBuf = String$(MAX_PATH, Chr$(0))
        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, idproc)
        If hProcess Then
            sChar = QueryFullProcessImageName(hProcess, 0, sBuf, MAX_PATH)  'Crash Here
            If sChar Then
                sBuf = Left$(sBuf, sChar)
                ExePathFromProcID = sBuf
               ' Debug.Print sBuf
            End If
            CloseHandle hProcess
        End If
    End Function
    
    Sub test()
        Dim hSnapshot As Long
        Dim sPE32 As PROCESSENTRY32
        Dim lRet As Long
        Dim c As String
        hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
    
        If hSnapshot <> INVALID_HANDLE_VALUE Then
            sPE32.dwSize = Len(sPE32)
            lRet = Process32First(hSnapshot, sPE32)
            Do While lRet
                c = ExePathFromProcID(sPE32.th32ProcessID)
                Debug.Print sPE32.th32ProcessID
                Debug.Print c
                lRet = Process32Next(hSnapshot, sPE32)
            Loop
    
            CloseHandle hSnapshot
        End If
    
    
    End Sub

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Excel Crash when call API function QueryFullProcessImageName in excel VBA of win

    The final parameter in that API function should be ByRef not ByVal. When the API is called, that parameter is the size of sBuf, on return it is the number of bytes written to sBuf. When you change that, also change your code to pass a size variable, not the Max_Path constant.

    Edited: You cannot use the return value of that API other than to determine if it was successful or not. See MSDN documentation here
    Last edited by LaVolpe; Sep 26th, 2015 at 09:05 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2015
    Posts
    2

    Re: Excel Crash when call API function QueryFullProcessImageName in excel VBA of win

    Great! The final parameter in that API function should be ByRef not ByVal. Thanks your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width