Prepare release and bump version numbers to 2.13.0
[TortoiseGit.git] / src / Utils / CreateProcessHelper.h
blob2158e8cd3690f43655e343b8818cd24c15b3b342
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2016 - TortoiseGit
4 // Copyright (C) 2009-2010 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
21 #include "scope_exit_noexcept.h"
23 /**
24 * A helper class for invoking CreateProcess(). The lpProcessInformation
25 * can point to an uninitialized struct - it's memset to all zeroes inside.
28 class CCreateProcessHelper
30 public:
31 static bool CreateProcess(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPCWSTR lpCurrentDirectory, LPPROCESS_INFORMATION lpProcessInformation);
32 static bool CreateProcess(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPPROCESS_INFORMATION lpProcessInformation);
34 static bool CreateProcessDetached(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPCWSTR lpCurrentDirectory);
35 static bool CreateProcessDetached(LPCWSTR lpApplicationName, LPWSTR lpCommandLine);
36 static bool CreateProcessDetached(LPCWSTR lpApplicationName, LPCWSTR lpCommandLine);
39 inline bool CCreateProcessHelper::CreateProcess(LPCWSTR applicationName,
40 LPWSTR commandLine, LPCWSTR currentDirectory,
41 LPPROCESS_INFORMATION processInfo)
43 STARTUPINFO startupInfo = { 0 };
44 startupInfo.cb = sizeof(STARTUPINFO);
46 memset(processInfo, 0, sizeof(PROCESS_INFORMATION));
47 const BOOL result = ::CreateProcess(applicationName, commandLine, nullptr, nullptr, FALSE, CREATE_UNICODE_ENVIRONMENT, nullptr, currentDirectory, &startupInfo, processInfo);
48 return result != 0;
51 inline bool CCreateProcessHelper::CreateProcess(LPCWSTR applicationName,
52 LPWSTR commandLine, LPPROCESS_INFORMATION processInformation)
54 return CreateProcess( applicationName, commandLine, nullptr, processInformation );
57 inline bool CCreateProcessHelper::CreateProcessDetached(LPCWSTR lpApplicationName,
58 LPWSTR lpCommandLine, LPCWSTR lpCurrentDirectory)
60 PROCESS_INFORMATION process;
61 if (!CreateProcess(lpApplicationName, lpCommandLine, lpCurrentDirectory, &process))
62 return false;
64 CloseHandle(process.hThread);
65 CloseHandle(process.hProcess);
66 return true;
69 inline bool CCreateProcessHelper::CreateProcessDetached(LPCWSTR lpApplicationName,
70 LPWSTR lpCommandLine)
72 return CreateProcessDetached(lpApplicationName, lpCommandLine, nullptr);
75 inline bool CCreateProcessHelper::CreateProcessDetached(LPCWSTR lpApplicationName, LPCWSTR lpCommandLine)
77 // CreateProcess may modify buffer specified by lpCommandLine:
78 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425
79 // [[
80 // The Unicode version of this function, CreateProcessW, can modify
81 // the contents of this string. Therefore, this parameter cannot be a
82 // pointer to read - only memory(such as a const variable or a literal
83 // string).If this parameter is a constant string, the function may
84 // cause an access violation.
85 // ]]
86 LPWSTR commandLineBuf = nullptr;
87 if (lpCommandLine)
88 commandLineBuf = _wcsdup(lpCommandLine);
89 SCOPE_EXIT { free(reinterpret_cast<void*>(commandLineBuf)); };
91 return CreateProcessDetached(lpApplicationName, commandLineBuf, nullptr);