Advertisement
ascend4nt

RunWait Example - FreeBASIC

Jan 3rd, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define _UNICODE 1
  2. #define UNICODE 1
  3. #include "crt\string.bi"
  4. #include "windows.bi"
  5.  
  6. '' Author: Ascend4nt [Update 2012/01/03 -> fixed quotes position, example calls]
  7.  
  8. Function RunWait(ByRef sFile As Const WString, _
  9.     ByRef sParams As Const WString,_
  10.     ByRef sDir As Const WString, _
  11.     ByVal nShow As Integer) _
  12.      As Integer
  13.      
  14.     Dim stStartup As STARTUPINFOW
  15.     Dim stProcInfo As PROCESS_INFORMATION
  16.     Dim bSuccess As BOOL
  17.    
  18.     memset(@stStartup, 0, sizeof(STARTUPINFOW))
  19.     stStartup.cb = sizeof(STARTUPINFOW)
  20.     stStartup.dwFlags = STARTF_USESHOWWINDOW
  21.     stStartup.wShowWindow = nShow
  22.    
  23.     memset(@stProcInfo, 0, sizeof(PROCESS_INFORMATION))
  24.    
  25.     Dim pDir As LPCWSTR
  26.     If (sDir <> "") Then
  27.         pDir = @sDir
  28.     Else
  29.         pDir = 0
  30.     End If    
  31.    
  32.     bSuccess = CreateProcess(0, _
  33.         """" + sFile + """ " + sParams, _
  34.         NULL, NULL, FALSE, 0, NULL, pDir, @stStartup, @stProcInfo)
  35.  
  36.     '' Per MSDN, best practice - don't just return on a failed call -
  37.     '' close any handles that may have been returned
  38.     If (stProcInfo.hThread <> 0) Then
  39.         CloseHandle(stProcInfo.hThread)
  40.     End If
  41.  
  42.  
  43.     Dim dwExitCode As DWORD
  44.     dwExitCode = -1
  45.    
  46.     Dim hProcess As HANDLE
  47.     hProcess = stProcInfo.hProcess
  48.    
  49.     If (bSuccess <> FALSE) Then
  50.         '' Wait for process to end
  51.         WaitForSingleObject(hProcess, INFINITE)
  52.         '' and get Exit code
  53.         GetExitCodeProcess(hProcess, @dwExitCode)
  54.     End If
  55.    
  56.     If (hProcess <> 0) Then
  57.         CloseHandle(hProcess)
  58.     End If
  59.    
  60.     Return dwExitCode    
  61.      
  62. End Function
  63.  
  64.     '' Prevent critical errors pop-ups
  65.     SetErrorMode(SEM_NOOPENFILEERRORBOX Or SEM_FAILCRITICALERRORS)
  66.  
  67.     Dim nExitCode As Integer
  68.  
  69.     ''nExitCode = RunWait(Environ("comspec"),"", "", SW_SHOW)
  70.     ''nExitCode = RunWait(Environ("comspec")," /c test.bat", "", SW_SHOW)
  71.     nExitCode = RunWait("test.bat", "", "", SW_SHOW)
  72.    
  73.     Print "ExitCode = ";nExitCode
  74.     Sleep
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement