Results 1 to 4 of 4

Thread: Unable to open Shell process handle

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    10

    Unable to open Shell process handle

    In Excel I have a button that when I click it shell's out to a .exe file someone else made. I used the API function ShellExecute because I wanted to specify parameters.

    I want to wait until the .exe file is done running until I continue on with my code. So I have been trying to use OpenProcess and GetExitCodeProcess API functions. The problem is whenever I try to get the .exe file's process handle it comes back as 0. I am not sure what the problem is. Any ideas would be greatly appreciated.

    Thanks!

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Unable to open Shell process handle

    You may also use the WaitForSingleObject API to do this, but post your code and lets see what may be going on.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    10

    Re: Unable to open Shell process handle

    Here is my code...

    VB Code:
    1. Function RunRe_Synch() As Boolean
    2. Dim lTaskID As Long
    3. Dim lProcess As Long
    4. Dim lExitCode As Long
    5. Dim lResult As Long
    6.  
    7. Dim File, Operation, Parameters, Directory As String
    8.  
    9. File = "C:\ReSynch\WorksetVersion\executable\Re_Synch.exe"
    10. Operation = "Open"
    11. Parameters = "C:\ReSynch\WorksetVersion\executable\"
    12. Directory = "C:\ReSynch\WorksetVersion\executable\"
    13.  
    14. 'Run the Shell Function
    15. lTaskID = ShellExecute(Application.hwnd, Operation, File, Parameters, Directory, 1)
    16.  
    17. 'Check for errors.
    18.     If lTaskID = 0 Then MsgBox ("Shell function error.")
    19.    
    20.     'Get the process handle from the task ID returned by Shell.
    21.     lProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0&, lTaskID)
    22.    
    23.     'Check for errors.
    24.     If lProcess = 0 Then MsgBox ("Unable to open Shell process handle.")
    25.    
    26.    
    27.     'Loop while the shelled process is still running.
    28.     Do
    29.         'ExitCode will be set to STILL_ACTIVE as long as the shelled process is running.
    30.         lResult = GetExitCodeProcess(lProcess, lExitCode)
    31.         DoEvents
    32.     Loop While lExitCode = STILL_ACTIVE
    33.    
    34.     RunRe_Synch = True
    35.  
    36.  
    37. End Function

    The error catches arn't done... I just have message boxes popping up now. The problem I have is that my lProcess equals 0 everytime I run my program.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Unable to open Shell process handle

    The lTaskID being returned from ShellExecute is not really the process id.
    Return Values

    If the function succeeds, the return value is the instance handle of the application that was run.

    If the function fails, the return value is an error value that is less than or equal to 32.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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