xref: /aosp_15_r20/external/lzma/CPP/Windows/ProcessUtils.cpp (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 // ProcessUtils.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "../Common/StringConvert.h"
6 
7 #include "ProcessUtils.h"
8 
9 #ifndef _UNICODE
10 extern bool g_IsNT;
11 #endif
12 
13 namespace NWindows {
14 
15 #ifndef UNDER_CE
GetQuotedString(const UString & s)16 static UString GetQuotedString(const UString &s)
17 {
18   UString s2 ('\"');
19   s2 += s;
20   s2.Add_Char('\"');
21   return s2;
22 }
23 #endif
24 
Create(LPCWSTR imageName,const UString & params,LPCWSTR curDir)25 WRes CProcess::Create(LPCWSTR imageName, const UString &params, LPCWSTR curDir)
26 {
27   /*
28   OutputDebugStringW(L"CProcess::Create");
29   OutputDebugStringW(imageName);
30   if (params)
31   {
32     OutputDebugStringW(L"params:");
33     OutputDebugStringW(params);
34   }
35   if (curDir)
36   {
37     OutputDebugStringW(L"cur dir:");
38     OutputDebugStringW(curDir);
39   }
40   */
41 
42   Close();
43   const UString params2 =
44       #ifndef UNDER_CE
45       GetQuotedString(imageName) + L' ' +
46       #endif
47       params;
48   #ifdef UNDER_CE
49   curDir = NULL;
50   #else
51   imageName = NULL;
52   #endif
53   PROCESS_INFORMATION pi;
54   BOOL result;
55   #ifndef _UNICODE
56   if (!g_IsNT)
57   {
58     STARTUPINFOA si;
59     si.cb = sizeof(si);
60     si.lpReserved = NULL;
61     si.lpDesktop = NULL;
62     si.lpTitle = NULL;
63     si.dwFlags = 0;
64     si.cbReserved2 = 0;
65     si.lpReserved2 = NULL;
66 
67     CSysString curDirA;
68     if (curDir != 0)
69       curDirA = GetSystemString(curDir);
70     const AString s = GetSystemString(params2);
71     result = ::CreateProcessA(NULL, s.Ptr_non_const(),
72         NULL, NULL, FALSE, 0, NULL, ((curDir != 0) ? (LPCSTR)curDirA: 0), &si, &pi);
73   }
74   else
75   #endif
76   {
77     STARTUPINFOW si;
78     si.cb = sizeof(si);
79     si.lpReserved = NULL;
80     si.lpDesktop = NULL;
81     si.lpTitle = NULL;
82     si.dwFlags = 0;
83     si.cbReserved2 = 0;
84     si.lpReserved2 = NULL;
85 
86     result = CreateProcessW(imageName, params2.Ptr_non_const(),
87         NULL, NULL, FALSE, 0, NULL, curDir, &si, &pi);
88   }
89   if (result == 0)
90     return ::GetLastError();
91   ::CloseHandle(pi.hThread);
92   _handle = pi.hProcess;
93   return 0;
94 }
95 
MyCreateProcess(LPCWSTR imageName,const UString & params)96 WRes MyCreateProcess(LPCWSTR imageName, const UString &params)
97 {
98   CProcess process;
99   return process.Create(imageName, params, NULL);
100 }
101 
102 }
103