| 35 | | namespace fb |
|---|
| 36 | | namespace process |
|---|
| 37 | | function spawn(byref cmdLine as string) as uinteger |
|---|
| 38 | | dim result as uinteger |
|---|
| 39 | | dim as HANDLE StdInRd, StdOutRd, StdErrRd |
|---|
| 40 | | dim as HANDLE StdInWr, StdOutWr, StdErrWr |
|---|
| 41 | | |
|---|
| 42 | | dim pi as PROCESS_INFORMATION |
|---|
| 43 | | dim si as STARTUPINFO |
|---|
| 44 | | dim sa as SECURITY_ATTRIBUTES |
|---|
| 45 | | |
|---|
| 46 | | '// INIT |
|---|
| 47 | | with sa |
|---|
| 48 | | .nLength = sizeof( sa ) |
|---|
| 49 | | .bInheritHandle = TRUE |
|---|
| 50 | | .lpSecurityDescriptor = NULL |
|---|
| 51 | | end with |
|---|
| 52 | | |
|---|
| 53 | | '# Create the pipes |
|---|
| 54 | | '# StdIn |
|---|
| 55 | | if (CreatePipe( @StdInRd, @StdInWr, @sa, 0 ) = 0) then |
|---|
| 56 | | print "Error creating StdIn pipes." |
|---|
| 57 | | end 0 |
|---|
| 58 | | end if |
|---|
| 59 | | |
|---|
| 60 | | '# StdOut |
|---|
| 61 | | if (CreatePipe( @StdOutRd, @StdOutWr, @sa, 0 ) = 0) then |
|---|
| 62 | | print "Error creating StdOut pipes." |
|---|
| 63 | | end 0 |
|---|
| 64 | | end if |
|---|
| 65 | | |
|---|
| 66 | | '# StdErr |
|---|
| 67 | | if (CreatePipe( @StdErrRd, @StdErrWr, @sa, 0 ) = 0) then |
|---|
| 68 | | print "Error creating StdErr pipes." |
|---|
| 69 | | end 0 |
|---|
| 70 | | end if |
|---|
| 71 | | |
|---|
| 72 | | '# Ensure the handles to the pipe are not inherited. |
|---|
| 73 | | SetHandleInformation( StdInWr, HANDLE_FLAG_INHERIT, 0) |
|---|
| 74 | | SetHandleInformation( StdOutRd, HANDLE_FLAG_INHERIT, 0) |
|---|
| 75 | | SetHandleInformation( StdErrRd, HANDLE_FLAG_INHERIT, 0) |
|---|
| 76 | | |
|---|
| 77 | | '# Set the Std* handles ;-) |
|---|
| 78 | | with si |
|---|
| 79 | | .cb = sizeof( si ) |
|---|
| 80 | | .hStdError = StdErrWr |
|---|
| 81 | | .hStdOutput = StdOutWr |
|---|
| 82 | | .hStdInput = StdInRd |
|---|
| 83 | | .dwFlags = STARTF_USESTDHANDLES |
|---|
| 84 | | end with |
|---|
| 85 | | '// INIT |
|---|
| 86 | | |
|---|
| 87 | | if (CreateProcess( NULL, _ |
|---|
| 88 | | StrPtr( cmdLine ), _ |
|---|
| 89 | | NULL, _ |
|---|
| 90 | | NULL, _ |
|---|
| 91 | | TRUE, _ |
|---|
| 92 | | CREATE_NEW_PROCESS_GROUP or DETACHED_PROCESS, _ |
|---|
| 93 | | NULL, _ |
|---|
| 94 | | NULL, _ |
|---|
| 95 | | @si, _ |
|---|
| 96 | | @pi ) = 0) then |
|---|
| 97 | | else |
|---|
| 98 | | CloseHandle( pi.hProcess ) |
|---|
| 99 | | CloseHandle( pi.hThread ) |
|---|
| 100 | | result = pi.dwProcessId |
|---|
| 101 | | end if |
|---|
| 102 | | |
|---|
| 103 | | return result |
|---|
| 104 | | end function |
|---|
| 105 | | end namespace |
|---|
| 106 | | end namespace |
|---|
| | 35 | function spawn(byref cmdLine as string) as uinteger |
|---|
| | 36 | dim result as uinteger |
|---|
| | 37 | dim as HANDLE StdInRd, StdOutRd, StdErrRd |
|---|
| | 38 | dim as HANDLE StdInWr, StdOutWr, StdErrWr |
|---|
| | 39 | |
|---|
| | 40 | dim pi as PROCESS_INFORMATION |
|---|
| | 41 | dim si as STARTUPINFO |
|---|
| | 42 | dim sa as SECURITY_ATTRIBUTES |
|---|
| | 43 | |
|---|
| | 44 | '// INIT |
|---|
| | 45 | with sa |
|---|
| | 46 | .nLength = sizeof( sa ) |
|---|
| | 47 | .bInheritHandle = TRUE |
|---|
| | 48 | .lpSecurityDescriptor = NULL |
|---|
| | 49 | end with |
|---|
| | 50 | |
|---|
| | 51 | '# Create the pipes |
|---|
| | 52 | '# StdIn |
|---|
| | 53 | if (CreatePipe( @StdInRd, @StdInWr, @sa, 0 ) = 0) then |
|---|
| | 54 | print "Error creating StdIn pipes." |
|---|
| | 55 | end 0 |
|---|
| | 56 | end if |
|---|
| | 57 | |
|---|
| | 58 | '# StdOut |
|---|
| | 59 | if (CreatePipe( @StdOutRd, @StdOutWr, @sa, 0 ) = 0) then |
|---|
| | 60 | print "Error creating StdOut pipes." |
|---|
| | 61 | end 0 |
|---|
| | 62 | end if |
|---|
| | 63 | |
|---|
| | 64 | '# StdErr |
|---|
| | 65 | if (CreatePipe( @StdErrRd, @StdErrWr, @sa, 0 ) = 0) then |
|---|
| | 66 | print "Error creating StdErr pipes." |
|---|
| | 67 | end 0 |
|---|
| | 68 | end if |
|---|
| | 70 | '# Ensure the handles to the pipe are not inherited. |
|---|
| | 71 | SetHandleInformation( StdInWr, HANDLE_FLAG_INHERIT, 0) |
|---|
| | 72 | SetHandleInformation( StdOutRd, HANDLE_FLAG_INHERIT, 0) |
|---|
| | 73 | SetHandleInformation( StdErrRd, HANDLE_FLAG_INHERIT, 0) |
|---|
| | 74 | |
|---|
| | 75 | '# Set the Std* handles ;-) |
|---|
| | 76 | with si |
|---|
| | 77 | .cb = sizeof( si ) |
|---|
| | 78 | .hStdError = StdErrWr |
|---|
| | 79 | .hStdOutput = StdOutWr |
|---|
| | 80 | .hStdInput = StdInRd |
|---|
| | 81 | .dwFlags = STARTF_USESTDHANDLES |
|---|
| | 82 | end with |
|---|
| | 83 | '// INIT |
|---|
| | 84 | |
|---|
| | 85 | if (CreateProcess( NULL, _ |
|---|
| | 86 | StrPtr( cmdLine ), _ |
|---|
| | 87 | NULL, _ |
|---|
| | 88 | NULL, _ |
|---|
| | 89 | TRUE, _ |
|---|
| | 90 | CREATE_NEW_PROCESS_GROUP or DETACHED_PROCESS, _ |
|---|
| | 91 | NULL, _ |
|---|
| | 92 | NULL, _ |
|---|
| | 93 | @si, _ |
|---|
| | 94 | @pi ) = 0) then |
|---|
| | 95 | else |
|---|
| | 96 | CloseHandle( pi.hProcess ) |
|---|
| | 97 | CloseHandle( pi.hThread ) |
|---|
| | 98 | result = pi.dwProcessId |
|---|
| | 99 | end if |
|---|
| | 100 | |
|---|
| | 101 | return result |
|---|
| | 102 | end function |
|---|