Results 1 to 3 of 3

Thread: Synchronizing ShellExecute

  1. #1

    Thread Starter
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    Synchronizing ShellExecute

    I've looked all over the net trying to find a way to synchronize a program executed with ShellExecute, and all the code I've found fails. For some reason, any attempt to run my self-terminating command-line program, such as CreateProcessA and Shell fails, and only ShellExecute really works. The following code is the closest I could get to it, but CreateProcess fails, and ShellExecute doesn't return a handle:

    VB Code:
    1. 'Declarations are omited to make it easier to read.
    2. Public Function ShellSynchronous(cmdline As String) As Long
    3. On Error GoTo ProcErr
    4.     Dim proc As PROCESS_INFORMATION
    5.     Dim start As STARTUPINFO
    6.     Dim ret As Long
    7.     Dim Count As Long
    8.    
    9.     start.cb = Len(start)
    10.     ret = CreateProcessA(0, cmdline, 0, 0, 1, NORMAL_PRIORITY_CLASS, 0, 0, start, proc)
    11.     Count = GetTickCount
    12.     ret = WaitForSingleObject(proc.hProcess, INFINITE)
    13.     'If (GetTickCount - Count) < 1000 Then
    14.     '    Sleep 1000
    15.     'End If
    16.     Call GetExitCodeProcess(proc.hProcess, ret)
    17.     Call CloseHandle(proc.hThread)
    18.     Call CloseHandle(proc.hProcess)
    19.     ShellSynchronous = ret
    20.  
    21.     Exit Function
    22.  
    23. ProcErr:
    24.     Err.Raise Err.Number
    25.  
    26. End Function
    Please, put a checkmark ( ) or the word [RESOLVED] in your topic title if it was resolved, and rate the person who resolved it.

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Synchronizing ShellExecute

    You mean you want your code to wait until the executed application finishes?

    Search for ShellAndWait. It does exactly that.

  3. #3

    Thread Starter
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    Re: Synchronizing ShellExecute

    Thanks, but as I mentioned previously, for some reason the shelled program fails when it's called by any means other than ShellExecute. ShellWait is the same code I posted above, which uses CreateProcess to run the DOS program. It might have relevance that the program takes command line parameters.
    Please, put a checkmark ( ) or the word [RESOLVED] in your topic title if it was resolved, and rate the person who resolved it.

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