Results 1 to 8 of 8

Thread: [RESOLVED] Need to wait for a program to end

  1. #1

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Resolved [RESOLVED] Need to wait for a program to end

    I have a function that launches an external program and I need it to wait until it finishes before the next external program is launched. Essentially what I need to do is to add "ShellWait" functionality to my function. Normally I use the pID to do this, but I don't know how to get the pID given the way in which the code launches the program. I need to launch the external program in a way that uses another user account that has admin rights. Here is the function I am using...

    Code:
    Public Function RunAsUser(ByVal UserName As String, _
            ByVal Password As String, _
            ByVal DomainName As String, _
            ByVal CommandLine As String, _
            ByVal CurrentDirectory As String) As Long
    
        Dim si As STARTUPINFO
        Dim pi As PROCESS_INFORMATION
        
        Dim wUser As String
        Dim wDomain As String
        Dim wPassword As String
        Dim wCommandLine As String
        Dim wCurrentDir As String
        Dim Result As Long
        
        si.cb = Len(si)
        si.dwFlags = STARTF_USESHOWWINDOW
        si.wShowWindow = SW_HIDE
            
        wUser = StrConv(UserName + Chr$(0), vbUnicode)
        wDomain = StrConv(DomainName + Chr$(0), vbUnicode)
        wPassword = StrConv(Password + Chr$(0), vbUnicode)
        wCommandLine = StrConv(CommandLine + Chr$(0), vbUnicode)
        wCurrentDir = StrConv(CurrentDirectory + Chr$(0), vbUnicode)
        
        Result = CreateProcessWithLogonW(wUser, wDomain, wPassword, _
              LOGON_WITH_PROFILE, 0&, wCommandLine, _
              CREATE_DEFAULT_ERROR_MODE, 0&, wCurrentDir, si, pi)
        ' CreateProcessWithLogonW() does not
        If Result <> 0 Then
            CloseHandle pi.hThread
            CloseHandle pi.hProcess
            RunAsUser = 0
        Else
            RunAsUser = Err.LastDllError
            MsgBox "CreateProcessWithLogonW() failed with error " & Err.LastDllError, vbExclamation
        End If
    
    End Function
    
    Public Function FileExists(ByVal filespec As String) As Boolean
      On Error Resume Next
      FileExists = (GetAttr(filespec) And vbDirectory) = vbNormal
    End Function
    
    
    'This is how I call the function
    Call RunAsUser("myUser", "myPassword", ".", "\\myServer\myShare\myProgram.exe", "\\myServer\myShare")
    If I knew how to get the pID of the program I am launching, then I could add code that would wait until the program terminated, but I have no idea how to get the pID. Does anyone know how I could launch a program using this function and then wait for the program to terminate before I continue on?

    Thanks!

  2. #2

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Re: Need to wait for a program to end

    Oh, I think I can get the pID by using "pi.dwProcessId". Maybe I should have thought first before posting!

    I will try this and see if I can make it work.

  3. #3

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Re: Need to wait for a program to end

    Tested it, it works fine.

  4. #4
    New Member
    Join Date
    Sep 2008
    Posts
    2

    Re: Need to wait for a program to end

    Thanks a lot.

    Can you post your complete vb6 code. for example you did not declare about CreateProcessWithLogonW etc

    Thanks

  5. #5
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Need to wait for a program to end

    BrianPaul, when you have resolved an issue (whether by yourself or with the help of others) it would be helpful to other people who help here if you were to mark the thread as resolved (see the dropdown menu in "tools" above).

    That way we don't waste our time looking at resolved issues where our help is not needed. Personally, when someone neglects to mark a thread as resolved I make a mental note of their name and skip over their future requests and assume they've got their help and just neglected to resolve the post.
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  6. #6
    New Member
    Join Date
    Sep 2008
    Posts
    2

    Re: Need to wait for a program to end

    I want to invole one exe with parameter.

    C:\temp.exe parameter

    Thanks in Advance

  7. #7
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Need to wait for a program to end

    Quote Originally Posted by jaiprakashv
    Thanks in Advance
    Thanks in advance? For what?

    If you want help, try starting your own post rather than piggybacking on someone else's

    Plus, the answer's already out there if you check the forums
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  8. #8

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Re: Need to wait for a program to end

    Quote Originally Posted by jaiprakashv
    Thanks a lot.

    Can you post your complete vb6 code. for example you did not declare about CreateProcessWithLogonW etc

    Thanks
    Sorry, I forgot about this post. Here's the rest of the code, jaiprakashv...


    Code:
    Option Explicit
    '********Begin declarations for RunAsUser********
    'Public Const SW_SHOWNOACTIVATE = 4
    Public Const STARTF_USESHOWWINDOW = &H1
    Public Const SW_HIDE = 0
    Private Const CREATE_DEFAULT_ERROR_MODE = &H4000000
    Private Const LOGON_WITH_PROFILE = &H1
    Private Const LOGON_NETCREDENTIALS_ONLY = &H2
    Private Const LOGON32_LOGON_INTERACTIVE = 2
    Private Const LOGON32_PROVIDER_DEFAULT = 0
    
    Private Type STARTUPINFO
        cb As Long
        lpReserved As Long ' !!! must be Long for Unicode string
        lpDesktop As Long  ' !!! must be Long for Unicode string
        lpTitle As Long    ' !!! must be Long for Unicode string
        dwX As Long
        dwY As Long
        dwXSize As Long
        dwYSize As Long
        dwXCountChars As Long
        dwYCountChars As Long
        dwFillAttribute As Long
        dwFlags As Long
        wShowWindow As Integer
        cbReserved2 As Integer
        lpReserved2 As Long
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
    End Type
    
    Private Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
    End Type
    
    '  LogonUser() requires that the caller has the following permission
    '  Permission                        Display Name
    '  --------------------------------------------------------------------
    '  SE_TCB_NAME                      Act as part of the operating system
    
    '  CreateProcessAsUser() requires that the caller has the following permissions
    '  Permission                        Display Name
    '  ---------------------------------------------------------------
    '  SE_ASSIGNPRIMARYTOKEN_NAME       Replace a process level token
    '  SE_INCREASE_QUOTA_NAME           Increase quotas
      
    Private Declare Function LogonUser Lib "advapi32.dll" Alias _
            "LogonUserA" _
            (ByVal lpszUsername As String, _
            ByVal lpszDomain As String, _
            ByVal lpszPassword As String, _
            ByVal dwLogonType As Long, _
            ByVal dwLogonProvider As Long, _
            phToken As Long) As Long
    
    Private Declare Function CreateProcessAsUser Lib "advapi32.dll" _
            Alias "CreateProcessAsUserA" _
            (ByVal hToken As Long, _
            ByVal lpApplicationName As Long, _
            ByVal lpCommandLine As String, _
            ByVal lpProcessAttributes As Long, _
            ByVal lpThreadAttributes As Long, _
            ByVal bInheritHandles As Long, _
            ByVal dwCreationFlags As Long, _
            ByVal lpEnvironment As Long, _
            ByVal lpCurrentDirectory As String, _
            lpStartupInfo As STARTUPINFO, _
            lpProcessInformation As PROCESS_INFORMATION) As Long
    
    ' CreateProcessWithLogonW API is available only on Windows 2000 and later.
    Private Declare Function CreateProcessWithLogonW Lib "advapi32.dll" _
            (ByVal lpUsername As String, _
            ByVal lpDomain As String, _
            ByVal lpPassword As String, _
            ByVal dwLogonFlags As Long, _
            ByVal lpApplicationName As Long, _
            ByVal lpCommandLine As String, _
            ByVal dwCreationFlags As Long, _
            ByVal lpEnvironment As Long, _
            ByVal lpCurrentDirectory As String, _
            ByRef lpStartupInfo As STARTUPINFO, _
            ByRef lpProcessInformation As PROCESS_INFORMATION) As Long
          
    Private Declare Function CloseHandle Lib "kernel32.dll" _
            (ByVal hObject As Long) As Long
                                 
    Private Declare Function SetErrorMode Lib "kernel32.dll" _
            (ByVal uMode As Long) As Long
            
    Private Type OSVERSIONINFO
        dwOSVersionInfoSize As Long
        dwMajorVersion As Long
        dwMinorVersion As Long
        dwBuildNumber As Long
        dwPlatformId As Long
        szCSDVersion As String * 128
    End Type
                                 
    ' Version Checking APIs
    Private Declare Function GetVersionExA Lib "kernel32.dll" _
        (lpVersionInformation As OSVERSIONINFO) As Integer
    
    Private Const VER_PLATFORM_WIN32_NT = &H2
    '********End declarations for RunAsUser********
    
    '********************************************************************
    '                   RunAsUser for Windows 2000 and Later
    '********************************************************************
    Public Function RunAsUser(ByVal UserName As String, _
            ByVal Password As String, _
            ByVal DomainName As String, _
            ByVal CommandLine As String, _
            ByVal CurrentDirectory As String) As Long
    
        Dim si As STARTUPINFO
        Dim pi As PROCESS_INFORMATION
        
        Dim wUser As String
        Dim wDomain As String
        Dim wPassword As String
        Dim wCommandLine As String
        Dim wCurrentDir As String
        Dim Result As Long
        
        si.cb = Len(si)
        si.dwFlags = STARTF_USESHOWWINDOW
        si.wShowWindow = SW_HIDE
            
        wUser = StrConv(UserName + Chr$(0), vbUnicode)
        wDomain = StrConv(DomainName + Chr$(0), vbUnicode)
        wPassword = StrConv(Password + Chr$(0), vbUnicode)
        wCommandLine = StrConv(CommandLine + Chr$(0), vbUnicode)
        wCurrentDir = StrConv(CurrentDirectory + Chr$(0), vbUnicode)
        
        Result = CreateProcessWithLogonW(wUser, wDomain, wPassword, _
              LOGON_WITH_PROFILE, 0&, wCommandLine, _
              CREATE_DEFAULT_ERROR_MODE, 0&, wCurrentDir, si, pi)
        ' CreateProcessWithLogonW() does not
        If Result <> 0 Then
            CloseHandle pi.hThread
            CloseHandle pi.hProcess
            RunAsUser = 0
        Else
            RunAsUser = Err.LastDllError
            MsgBox "CreateProcessWithLogonW() failed with error " & Err.LastDllError, vbExclamation
        End If
    
    End Function

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