blob: 6da1893f7010867a270a1fb448a156b9f53355a6 [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;
62 startup.wShowWindow = SW_HIDE|SW_MINIMIZE;
63
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 */
70 CREATE_NO_WINDOW, /* we don't want a console */
71 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.
145// The registry is also remapped.
146PVOID disableWow64FsRedirection() {
147
148 // The call we want to make is the following:
149 // PVOID oldWow64Value;
150 // Wow64DisableWow64FsRedirection(&oldWow64Value);
151 // However that method may not exist (e.g. on non-64 systems) so
152 // we must not call it directly.
153
154 PVOID oldWow64Value = 0;
155
156 HMODULE hmod = LoadLibrary("kernel32.dll");
157 if (hmod != NULL) {
158 FARPROC proc = GetProcAddress(hmod, "Wow64DisableWow64FsRedirection");
159 if (proc != NULL) {
160 typedef BOOL (WINAPI *disableWow64FuncType)(PVOID *);
161 disableWow64FuncType funcPtr = (disableWow64FuncType)proc;
162 funcPtr(&oldWow64Value);
163 }
164
165 FreeLibrary(hmod);
166 }
167
168 return oldWow64Value;
169}
170
171// Reverts the redirection disabled in disableWow64FsRedirection.
172void revertWow64FsRedirection(PVOID oldWow64Value) {
173
174 // The call we want to make is the following:
175 // Wow64RevertWow64FsRedirection(oldWow64Value);
176 // However that method may not exist (e.g. on non-64 systems) so
177 // we must not call it directly.
178
179 HMODULE hmod = LoadLibrary("kernel32.dll");
180 if (hmod != NULL) {
181 FARPROC proc = GetProcAddress(hmod, "Wow64RevertWow64FsRedirection");
182 if (proc != NULL) {
183 typedef BOOL (WINAPI *revertWow64FuncType)(PVOID);
184 revertWow64FuncType funcPtr = (revertWow64FuncType)proc;
185 funcPtr(oldWow64Value);
186 }
187
188 FreeLibrary(hmod);
189 }
190}
191
192#endif /* _WIN32 */