Advertisement
ascend4nt

ShellExWait Example - FreeBasic

Dec 30th, 2012
90
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 "windows.bi"
  4. #include "win\shellapi.bi"
  5. #include "win\objbase.bi"
  6.  
  7. '' Author: Ascend4nt
  8.  
  9. '' Alternative way (doesn't provide info on process started,
  10. '' nor does it wait for termination or provide exit code):
  11. ''ShellExecute(0,"","test.bat",0,0,SW_HIDE)
  12.  
  13. '' Main function:
  14.  
  15. Function ShellExWait(ByRef sFile As WString, _
  16.     ByRef sParams As WString,_
  17.     ByRef sDir As WString, _
  18.     ByVal nShow As Integer) _
  19.      As Integer
  20.    
  21.     Dim stShEx As SHELLEXECUTEINFOW
  22.     With stShEx
  23.         .cbSize = sizeof(SHELLEXECUTEINFOW)
  24.         .fMask = SEE_MASK_NOCLOSEPROCESS
  25.         .hwnd = 0
  26.         .lpVerb = 0
  27.         .lpFile = @sFile
  28.         .lpParameters = @sParams
  29.         .lpDirectory = @sDir
  30.         .nShow = nShow
  31.         .hInstApp = 0
  32.         .lpIDList = 0
  33.         .lpClass = 0
  34.         .hkeyClass = 0
  35.         .dwHotKey = 0
  36.         .hIcon = 0
  37.         .hProcess = 0
  38.     End With
  39.    
  40.     If (ShellExecuteEx(@stShEx) = FALSE) Then
  41.         Return -1
  42.     End If
  43.    
  44.     Dim hProcess As HANDLE
  45.     hProcess = stShEx.hProcess
  46.  
  47.     WaitForSingleObject(hProcess, INFINITE)
  48.    
  49.     Dim dwExitCode As DWORD
  50.     dwExitCode = -1
  51.  
  52.     GetExitCodeProcess(hProcess, @dwExitCode)
  53.     CloseHandle(hProcess)
  54.    
  55.     Return dwExitCode
  56. End Function
  57.  
  58.  
  59. '' == Program Start: ==
  60.  
  61.     '' Important for certain 'ShellExecute' commands (requires linking in libole32 [-l libole32]):
  62.     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED Or COINIT_DISABLE_OLE1DDE)
  63.  
  64.     '' Prevent critical errors pop-ups
  65.     SetErrorMode(SEM_NOOPENFILEERRORBOX Or SEM_FAILCRITICALERRORS)
  66.  
  67.     Dim nExitCode As Integer
  68.    
  69.     '' Change to SW_HIDE to not show the command prompt:
  70.     nExitCode = ShellExWait("test.bat", "", "", SW_SHOW)
  71.      
  72.     Print "ExitCode = ";nExitCode
  73.     Sleep
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement