|
1 /* |
|
2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 #include "WinSysInfo.h" |
|
27 #include "FileUtils.h" |
|
28 #include "WinErrorHandling.h" |
|
29 |
|
30 namespace SysInfo { |
|
31 |
|
32 tstring getTempDir() { |
|
33 std::vector<TCHAR> buffer(MAX_PATH); |
|
34 DWORD res = GetTempPath(static_cast<DWORD>(buffer.size()), buffer.data()); |
|
35 if (res > buffer.size()) { |
|
36 buffer.resize(res); |
|
37 GetTempPath(static_cast<DWORD>(buffer.size()), buffer.data()); |
|
38 } |
|
39 return FileUtils::removeTrailingSlash(buffer.data()); |
|
40 } |
|
41 |
|
42 namespace { |
|
43 |
|
44 template <class Func> |
|
45 tstring getSystemDirImpl(Func func, const std::string& label) { |
|
46 std::vector<TCHAR> buffer(MAX_PATH); |
|
47 for (int i=0; i<2; i++) { |
|
48 DWORD res = func(buffer.data(), static_cast<DWORD>(buffer.size())); |
|
49 if (!res) { |
|
50 JP_THROW(SysError(label + " failed", func)); |
|
51 } |
|
52 if (res < buffer.size()) { |
|
53 return FileUtils::removeTrailingSlash(buffer.data()); |
|
54 } |
|
55 buffer.resize(res + 1); |
|
56 } |
|
57 JP_THROW("Unexpected reply from" + label); |
|
58 } |
|
59 |
|
60 } // namespace |
|
61 |
|
62 tstring getSystem32Dir() { |
|
63 return getSystemDirImpl(GetSystemDirectory, "GetSystemDirectory"); |
|
64 } |
|
65 |
|
66 tstring getWIPath() { |
|
67 return FileUtils::mkpath() << getSystem32Dir() << _T("msiexec.exe"); |
|
68 } |
|
69 |
|
70 namespace { |
|
71 |
|
72 tstring getModulePath(HMODULE h) |
|
73 { |
|
74 std::vector<TCHAR> buf(MAX_PATH); |
|
75 DWORD len = 0; |
|
76 while (true) { |
|
77 len = GetModuleFileName(h, buf.data(), (DWORD)buf.size()); |
|
78 if (len < buf.size()) { |
|
79 break; |
|
80 } |
|
81 // buffer is too small, increase it |
|
82 buf.resize(buf.size() * 2); |
|
83 } |
|
84 |
|
85 if (len == 0) { |
|
86 // error occured |
|
87 JP_THROW(SysError("GetModuleFileName failed", GetModuleFileName)); |
|
88 } |
|
89 return tstring(buf.begin(), buf.begin() + len); |
|
90 } |
|
91 |
|
92 } // namespace |
|
93 |
|
94 tstring getProcessModulePath() { |
|
95 return getModulePath(NULL); |
|
96 } |
|
97 |
|
98 HMODULE getCurrentModuleHandle() |
|
99 { |
|
100 // get module handle for the address of this function |
|
101 LPCWSTR address = reinterpret_cast<LPCWSTR>(getCurrentModuleHandle); |
|
102 HMODULE hmodule = NULL; |
|
103 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
|
104 address, &hmodule)) |
|
105 { |
|
106 JP_THROW(SysError(tstrings::any() << "GetModuleHandleExW failed", GetModuleHandleExW)); |
|
107 } |
|
108 return hmodule; |
|
109 } |
|
110 |
|
111 tstring getCurrentModulePath() |
|
112 { |
|
113 return getModulePath(getCurrentModuleHandle()); |
|
114 } |
|
115 |
|
116 namespace { |
|
117 |
|
118 tstring getEnvVariableImpl(const tstring& name, bool* errorOccured=0) { |
|
119 std::vector<TCHAR> buf(10); |
|
120 SetLastError(ERROR_SUCCESS); |
|
121 const DWORD size = GetEnvironmentVariable(name.c_str(), buf.data(), |
|
122 DWORD(buf.size())); |
|
123 if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) { |
|
124 if (errorOccured) { |
|
125 *errorOccured = true; |
|
126 return tstring(); |
|
127 } |
|
128 JP_THROW(SysError(tstrings::any() << "GetEnvironmentVariable(" |
|
129 << name << ") failed. Variable not set", GetEnvironmentVariable)); |
|
130 } |
|
131 |
|
132 if (size > buf.size()) { |
|
133 buf.resize(size); |
|
134 GetEnvironmentVariable(name.c_str(), buf.data(), DWORD(buf.size())); |
|
135 if (GetLastError() != ERROR_SUCCESS) { |
|
136 if (errorOccured) { |
|
137 *errorOccured = true; |
|
138 return tstring(); |
|
139 } |
|
140 JP_THROW(SysError(tstrings::any() << "GetEnvironmentVariable(" |
|
141 << name << ") failed", GetEnvironmentVariable)); |
|
142 } |
|
143 } |
|
144 |
|
145 if (errorOccured) { |
|
146 *errorOccured = false; |
|
147 } |
|
148 return tstring(buf.data()); |
|
149 } |
|
150 |
|
151 } // namespace |
|
152 |
|
153 tstring getEnvVariable(const tstring& name) { |
|
154 return getEnvVariableImpl(name); |
|
155 } |
|
156 |
|
157 tstring getEnvVariable(const std::nothrow_t&, const tstring& name, |
|
158 const tstring& defValue) { |
|
159 bool errorOccured = false; |
|
160 const tstring reply = getEnvVariableImpl(name, &errorOccured); |
|
161 if (errorOccured) { |
|
162 return defValue; |
|
163 } |
|
164 return reply; |
|
165 } |
|
166 |
|
167 bool isEnvVariableSet(const tstring& name) { |
|
168 TCHAR unused[1]; |
|
169 SetLastError(ERROR_SUCCESS); |
|
170 GetEnvironmentVariable(name.c_str(), unused, _countof(unused)); |
|
171 return GetLastError() != ERROR_ENVVAR_NOT_FOUND; |
|
172 } |
|
173 |
|
174 } // end of namespace SysInfo |