| Option Explicit ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const WAIT_INFINITE= -1&
Private Type STARTUPINFO
  cb As Long
  lpReserved As String
  lpDesktop As String
  lpTitle As 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 Long
  cbReserved2 As Long
  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
Private Declare Function CreateProcess Lib "kernel32" _
   Alias "CreateProcessA" _
  (ByVal lpAppName 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 Long, _
   lpStartupInfo As STARTUPINFO, _
   lpProcessInformation As PROCESS_INFORMATION) As Long
    
Private Declare Function WaitForSingleObject Lib "kernel32" _
  (ByVal hHandle As Long, _
   ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
  (ByVal hObject As Long) As Long
    
    
Private Sub Command1_Click()
    RunProcess "c:\windows\notepad.exe"
End Sub
Private Sub RunProcess (cmdline As String)
    Dim proc As PROCESS_INFORMATION
    Dim start As STARTUPINFO
   
  'Initialize the STARTUPINFO structure by
  'passing to start the size of the STARTUPINFO
  'type. Setting the .cb member is the only 
  'item of the structure needed to launch the program
   start.cb = Len(start)
   
  'Start the application
   Call CreateProcess(0&, cmdline, 0&, 0&, 1&, _
                      NORMAL_PRIORITY_CLASS, 0&, 0&, _
                      start, proc)
   
  'Wait for the application to finish
   Call WaitForSingleObject(proc.hProcess, WAIT_INFINITE)
   
  'Close the handle to the process
   Call CloseHandle(proc.hProcess)
  'Close the handle to the thread created
   Call CloseHandle(proc.hThread)
   MsgBox "The Shelled process " & cmdline & " has ended."
End Sub |