blob: bef1aa7106acc3f4d0d4c5ba7c1d10b007ddaff5 [file] [log] [blame]
Raphael628920b2011-11-22 10:40:13 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifdef _WIN32
18
19#include "utils.h"
20
21#define _CRT_SECURE_NO_WARNINGS 1
22
23bool gDebug = false;
24
25// Displays a message in an ok+info dialog box.
26void msgBox(const char* text, ...) {
27 CString formatted;
28 va_list ap;
29 va_start(ap, text);
30 formatted.setv(text, ap);
31 va_end(ap);
32
33 MessageBoxA(NULL, formatted.cstr(), "Android SDK Manager", MB_OK | MB_ICONINFORMATION);
34}
35
36// Displays GetLastError prefixed with a description in an error dialog box
37void displayLastError(const char *description, ...) {
38 CString formatted;
39 va_list ap;
40 va_start(ap, description);
41 formatted.setv(description, ap);
42 va_end(ap);
43
44 CString error;
45 error.setLastWin32Error();
46 formatted.add("\r\n");
47 formatted.add(error.cstr());
48 MessageBox(NULL, formatted.cstr(), "Android SDK Manager - Error", MB_OK | MB_ICONERROR);
49}
50
51// Executes the command line. Does not wait for the program to finish.
52// The return code is from CreateProcess (0 means failure), not the running app.
53int execNoWait(const char *app, const char *params, const char *workDir) {
54 STARTUPINFO startup;
55 PROCESS_INFORMATION pinfo;
56
57 ZeroMemory(&pinfo, sizeof(pinfo));
58
59 ZeroMemory(&startup, sizeof(startup));
60 startup.cb = sizeof(startup);
61 startup.dwFlags = STARTF_USESHOWWINDOW;
Raphael1bb74402011-11-29 14:33:26 -080062 startup.wShowWindow = SW_SHOWDEFAULT;
Raphael628920b2011-11-22 10:40:13 -080063
64 int ret = CreateProcessA(
65 (LPSTR) app, /* program path */
66 (LPSTR) params, /* command-line */
67 NULL, /* process handle is not inheritable */
68 NULL, /* thread handle is not inheritable */
69 TRUE, /* yes, inherit some handles */
Raphael1bb74402011-11-29 14:33:26 -080070 0, /* create flags */
Raphael628920b2011-11-22 10:40:13 -080071 NULL, /* use parent's environment block */
72 workDir, /* use parent's starting directory */
73 &startup, /* startup info, i.e. std handles */
74 &pinfo);
75
76 if (ret) {
77 CloseHandle(pinfo.hProcess);
78 CloseHandle(pinfo.hThread);
79 }
80
81 return ret;
82}
83
84// Executes command, waits for completion and returns exit code.
85// As indicated in MSDN for CreateProcess, callers should double-quote the program name
86// e.g. cmd="\"c:\program files\myapp.exe\" arg1 arg2";
87int execWait(const char *cmd) {
88 STARTUPINFO startup;
89 PROCESS_INFORMATION pinfo;
90
91 ZeroMemory(&pinfo, sizeof(pinfo));
92
93 ZeroMemory(&startup, sizeof(startup));
94 startup.cb = sizeof(startup);
95 startup.dwFlags = STARTF_USESHOWWINDOW;
96 startup.wShowWindow = SW_HIDE|SW_MINIMIZE;
97
98 int ret = CreateProcessA(
99 NULL, /* program path */
100 (LPSTR) cmd, /* command-line */
101 NULL, /* process handle is not inheritable */
102 NULL, /* thread handle is not inheritable */
103 TRUE, /* yes, inherit some handles */
104 CREATE_NO_WINDOW, /* we don't want a console */
105 NULL, /* use parent's environment block */
106 NULL, /* use parent's starting directory */
107 &startup, /* startup info, i.e. std handles */
108 &pinfo);
109
110 int result = -1;
111 if (ret) {
112 WaitForSingleObject(pinfo.hProcess, INFINITE);
113
114 DWORD exitCode;
115 if (GetExitCodeProcess(pinfo.hProcess, &exitCode)) {
116 // this should not return STILL_ACTIVE (259)
117 result = exitCode;
118 }
119 CloseHandle(pinfo.hProcess);
120 CloseHandle(pinfo.hThread);
121 }
122
123 return result;
124}
125
126bool getModuleDir(CPath *outDir) {
127 CHAR programDir[MAX_PATH];
128 int ret = GetModuleFileName(NULL, programDir, sizeof(programDir));
129 if (ret != 0) {
130 // Remove the last segment to keep only the directory.
131 int pos = ret - 1;
132 while (pos > 0 && programDir[pos] != '\\') {
133 --pos;
134 }
135 outDir->set(programDir, pos);
136 return true;
137 }
138 return false;
139}
140
141// Disables the FS redirection done by WOW64.
142// Because this runs as a 32-bit app, Windows automagically remaps some
143// folder under the hood (e.g. "Programs Files(x86)" is mapped as "Program Files").
144// This prevents the app from correctly searching for java.exe in these folders.
Raphael1bb74402011-11-29 14:33:26 -0800145// The registry is also remapped. This method disables this redirection.
146// Caller should restore the redirection later by using revertWow64FsRedirection().
Raphael628920b2011-11-22 10:40:13 -0800147PVOID disableWow64FsRedirection() {
148
149 // The call we want to make is the following:
150 // PVOID oldWow64Value;
151 // Wow64DisableWow64FsRedirection(&oldWow64Value);
Raphael1bb74402011-11-29 14:33:26 -0800152 // However that method may not exist (e.g. on XP non-64 systems) so
Raphael628920b2011-11-22 10:40:13 -0800153 // we must not call it directly.
154
155 PVOID oldWow64Value = 0;
156
157 HMODULE hmod = LoadLibrary("kernel32.dll");
158 if (hmod != NULL) {
159 FARPROC proc = GetProcAddress(hmod, "Wow64DisableWow64FsRedirection");
160 if (proc != NULL) {
161 typedef BOOL (WINAPI *disableWow64FuncType)(PVOID *);
162 disableWow64FuncType funcPtr = (disableWow64FuncType)proc;
163 funcPtr(&oldWow64Value);
164 }
165
166 FreeLibrary(hmod);
167 }
168
169 return oldWow64Value;
170}
171
172// Reverts the redirection disabled in disableWow64FsRedirection.
173void revertWow64FsRedirection(PVOID oldWow64Value) {
174
175 // The call we want to make is the following:
176 // Wow64RevertWow64FsRedirection(oldWow64Value);
Raphael1bb74402011-11-29 14:33:26 -0800177 // However that method may not exist (e.g. on XP non-64 systems) so
Raphael628920b2011-11-22 10:40:13 -0800178 // we must not call it directly.
179
180 HMODULE hmod = LoadLibrary("kernel32.dll");
181 if (hmod != NULL) {
182 FARPROC proc = GetProcAddress(hmod, "Wow64RevertWow64FsRedirection");
183 if (proc != NULL) {
184 typedef BOOL (WINAPI *revertWow64FuncType)(PVOID);
185 revertWow64FuncType funcPtr = (revertWow64FuncType)proc;
186 funcPtr(oldWow64Value);
187 }
188
189 FreeLibrary(hmod);
190 }
191}
192
193#endif /* _WIN32 */