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...
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?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")
Thanks!




Reply With Quote