jscript: Support passing EXPRVAL_JSVAL through JS stack.
[wine/zf.git] / programs / regsvr32 / regsvr32.c
blobee915ed3e2118d49084e7c3149a0693ae037f507
1 /*
2 * PURPOSE: Register OLE components in the registry
4 * Copyright 2001 ReactOS project
5 * Copyright 2001 Jurgen Van Gael [jurgen.vangael@student.kuleuven.ac.be]
6 * Copyright 2002 Andriy Palamarchuk
7 * Copyright 2014, 2015 Hugh McMaster
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define WIN32_LEAN_AND_MEAN
25 #include <windows.h>
26 #include <winternl.h>
27 #include <ole2.h>
28 #include "regsvr32.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(regsvr32);
33 typedef HRESULT (WINAPI *DLLREGISTER) (void);
34 typedef HRESULT (WINAPI *DLLUNREGISTER) (void);
35 typedef HRESULT (WINAPI *DLLINSTALL) (BOOL,LPCWSTR);
37 static BOOL Silent = FALSE;
39 static void WINAPIV output_write(UINT id, ...)
41 WCHAR fmt[1024];
42 __ms_va_list va_args;
43 WCHAR *str;
44 DWORD len, nOut, ret;
46 if (Silent) return;
48 if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
50 WINE_FIXME("LoadString failed with %d\n", GetLastError());
51 return;
54 __ms_va_start(va_args, id);
55 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
56 fmt, 0, 0, (LPWSTR)&str, 0, &va_args);
57 __ms_va_end(va_args);
58 if (len == 0 && GetLastError() != ERROR_NO_WORK_DONE)
60 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
61 return;
64 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &nOut, NULL);
66 /* WriteConsole fails if its output is redirected to a file.
67 * If this occurs, we should use an OEM codepage and call WriteFile.
69 if (!ret)
71 DWORD lenA;
72 char *strA;
74 lenA = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, NULL, 0, NULL, NULL);
75 strA = HeapAlloc(GetProcessHeap(), 0, lenA);
76 if (strA)
78 WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, strA, lenA, NULL, NULL);
79 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, lenA, &nOut, FALSE);
80 HeapFree(GetProcessHeap(), 0, strA);
83 LocalFree(str);
86 static LPCWSTR find_arg_start(LPCWSTR cmdline)
88 LPCWSTR s;
89 BOOL in_quotes;
90 int bcount;
92 bcount=0;
93 in_quotes=FALSE;
94 s=cmdline;
95 while (1) {
96 if (*s==0 || ((*s=='\t' || *s==' ') && !in_quotes)) {
97 /* end of this command line argument */
98 break;
99 } else if (*s=='\\') {
100 /* '\', count them */
101 bcount++;
102 } else if ((*s=='"') && ((bcount & 1)==0)) {
103 /* unescaped '"' */
104 in_quotes=!in_quotes;
105 bcount=0;
106 } else {
107 /* a regular character */
108 bcount=0;
110 s++;
112 return s;
115 static void reexec_self(void)
117 /* restart current process as 32-bit or 64-bit with same command line */
118 #ifndef _WIN64
119 BOOL wow64;
120 #endif
121 WCHAR systemdir[MAX_PATH];
122 LPCWSTR args;
123 WCHAR *cmdline;
124 STARTUPINFOW si = {0};
125 PROCESS_INFORMATION pi;
127 #ifdef _WIN64
128 TRACE("restarting as 32-bit\n");
129 GetSystemWow64DirectoryW(systemdir, MAX_PATH);
130 #else
131 TRACE("restarting as 64-bit\n");
133 if (!IsWow64Process(GetCurrentProcess(), &wow64) || !wow64)
135 TRACE("not running in wow64, can't restart as 64-bit\n");
136 return;
139 GetWindowsDirectoryW(systemdir, MAX_PATH);
140 wcscat(systemdir, L"\\SysNative");
141 #endif
143 args = find_arg_start(GetCommandLineW());
145 cmdline = HeapAlloc(GetProcessHeap(), 0,
146 (wcslen(systemdir)+wcslen(L"\\regsvr32.exe")+wcslen(args)+1)*sizeof(WCHAR));
148 wcscpy(cmdline, systemdir);
149 wcscat(cmdline, L"\\regsvr32.exe");
150 wcscat(cmdline, args);
152 si.cb = sizeof(si);
154 if (CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
156 DWORD exit_code;
157 WaitForSingleObject(pi.hProcess, INFINITE);
158 GetExitCodeProcess(pi.hProcess, &exit_code);
159 ExitProcess(exit_code);
161 else
163 WINE_TRACE("failed to restart, err=%d\n", GetLastError());
166 HeapFree(GetProcessHeap(), 0, cmdline);
169 #ifdef _WIN64
170 # define ALT_MACHINE IMAGE_FILE_MACHINE_I386
171 #else
172 # define ALT_MACHINE IMAGE_FILE_MACHINE_AMD64
173 #endif
176 * Loads procedure.
178 * Parameters:
179 * strDll - name of the dll.
180 * procName - name of the procedure to load from the dll.
181 * DllHandle - a variable that receives the handle of the loaded dll.
182 * firstDll - true if this is the first dll in the command line.
184 static VOID *LoadProc(const WCHAR* strDll, const char* procName, HMODULE* DllHandle, BOOL firstDll)
186 VOID* (*proc)(void);
188 *DllHandle = LoadLibraryExW(strDll, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
189 if(!*DllHandle)
191 HMODULE module;
192 if (firstDll && GetLastError() == ERROR_BAD_EXE_FORMAT &&
193 (module = LoadLibraryExW(strDll, 0, LOAD_LIBRARY_AS_IMAGE_RESOURCE)))
195 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( (HMODULE)((ULONG_PTR)module & ~3) );
196 if (nt->FileHeader.Machine == ALT_MACHINE) reexec_self();
198 output_write(STRING_DLL_LOAD_FAILED, strDll);
199 ExitProcess(LOADLIBRARY_FAILED);
201 proc = (VOID *) GetProcAddress(*DllHandle, procName);
202 if(!proc)
204 output_write(STRING_PROC_NOT_IMPLEMENTED, procName, strDll);
205 FreeLibrary(*DllHandle);
206 return NULL;
208 return proc;
211 static int RegisterDll(const WCHAR* strDll, BOOL firstDll)
213 HRESULT hr;
214 DLLREGISTER pfRegister;
215 HMODULE DllHandle = NULL;
217 pfRegister = LoadProc(strDll, "DllRegisterServer", &DllHandle, firstDll);
218 if (!pfRegister)
219 return GETPROCADDRESS_FAILED;
221 hr = pfRegister();
222 if(FAILED(hr))
224 output_write(STRING_REGISTER_FAILED, strDll);
225 return DLLSERVER_FAILED;
227 output_write(STRING_REGISTER_SUCCESSFUL, strDll);
229 if(DllHandle)
230 FreeLibrary(DllHandle);
231 return 0;
234 static int UnregisterDll(const WCHAR* strDll, BOOL firstDll)
236 HRESULT hr;
237 DLLUNREGISTER pfUnregister;
238 HMODULE DllHandle = NULL;
240 pfUnregister = LoadProc(strDll, "DllUnregisterServer", &DllHandle, firstDll);
241 if (!pfUnregister)
242 return GETPROCADDRESS_FAILED;
244 hr = pfUnregister();
245 if(FAILED(hr))
247 output_write(STRING_UNREGISTER_FAILED, strDll);
248 return DLLSERVER_FAILED;
250 output_write(STRING_UNREGISTER_SUCCESSFUL, strDll);
252 if(DllHandle)
253 FreeLibrary(DllHandle);
254 return 0;
257 static int InstallDll(BOOL install, const WCHAR *strDll, const WCHAR *command_line, BOOL firstDll)
259 HRESULT hr;
260 DLLINSTALL pfInstall;
261 HMODULE DllHandle = NULL;
263 pfInstall = LoadProc(strDll, "DllInstall", &DllHandle, firstDll);
264 if (!pfInstall)
265 return GETPROCADDRESS_FAILED;
267 hr = pfInstall(install, command_line);
268 if(FAILED(hr))
270 if (install)
271 output_write(STRING_INSTALL_FAILED, strDll);
272 else
273 output_write(STRING_UNINSTALL_FAILED, strDll);
274 return DLLSERVER_FAILED;
276 if (install)
277 output_write(STRING_INSTALL_SUCCESSFUL, strDll);
278 else
279 output_write(STRING_UNINSTALL_SUCCESSFUL, strDll);
281 if(DllHandle)
282 FreeLibrary(DllHandle);
283 return 0;
286 static WCHAR *parse_command_line(WCHAR *command_line)
288 if (command_line[0] == ':' && command_line[1])
290 int len = lstrlenW(command_line);
292 command_line++;
293 len--;
294 /* remove double quotes */
295 if (command_line[0] == '"')
297 command_line++;
298 len--;
299 if (command_line[0])
301 len--;
302 command_line[len] = 0;
305 if (command_line[0])
306 return command_line;
308 return NULL;
311 int __cdecl wmain(int argc, WCHAR* argv[])
313 int i, res, ret = 0;
314 BOOL CallRegister = TRUE;
315 BOOL CallInstall = FALSE;
316 BOOL Unregister = FALSE;
317 BOOL DllFound = FALSE;
318 WCHAR* wsCommandLine = NULL;
319 WCHAR EmptyLine[] = L"";
321 OleInitialize(NULL);
323 /* We mirror the Microsoft version by processing all of the flags before
324 * the files (e.g. regsvr32 file1 /s file2 is silent even for file1).
326 * Note the complication that this version may be passed Unix format filenames
327 * which could be mistaken for flags. The Windows version conveniently
328 * requires each flag to be separate (e.g. no /su), so we will simply
329 * assume that anything longer than /. is a filename.
331 for(i = 1; i < argc; i++)
333 if (argv[i][0] == '/' || argv[i][0] == '-')
335 if (!argv[i][1])
336 return INVALID_ARG;
338 if (argv[i][2] && argv[i][2] != ':')
339 continue;
341 switch (towlower(argv[i][1]))
343 case 'u':
344 Unregister = TRUE;
345 break;
346 case 's':
347 Silent = TRUE;
348 break;
349 case 'i':
350 CallInstall = TRUE;
351 wsCommandLine = parse_command_line(argv[i] + 2); /* argv[i] + strlen("/i") */
352 if (!wsCommandLine)
353 wsCommandLine = EmptyLine;
354 break;
355 case 'n':
356 CallRegister = FALSE;
357 break;
358 case 'c':
359 /* console output */;
360 break;
361 default:
362 output_write(STRING_UNRECOGNIZED_SWITCH, argv[i]);
363 output_write(STRING_USAGE);
364 return INVALID_ARG;
366 argv[i] = NULL;
370 if (!CallInstall && !CallRegister) /* flags: /n or /u /n */
371 return INVALID_ARG;
373 for (i = 1; i < argc; i++)
375 if (argv[i])
377 WCHAR *DllName = argv[i];
378 BOOL firstDll = !DllFound;
379 res = 0;
381 DllFound = TRUE;
382 if (CallInstall && Unregister)
383 res = InstallDll(!Unregister, DllName, wsCommandLine, firstDll);
385 /* The Windows version stops processing the current file on the first error. */
386 if (res)
388 ret = res;
389 continue;
392 if (!CallInstall || CallRegister)
394 if(Unregister)
395 res = UnregisterDll(DllName, firstDll);
396 else
397 res = RegisterDll(DllName, firstDll);
400 if (res)
402 ret = res;
403 continue;
406 if (CallInstall && !Unregister)
407 res = InstallDll(!Unregister, DllName, wsCommandLine, firstDll);
409 if (res)
411 ret = res;
412 continue;
417 if (!DllFound)
419 output_write(STRING_HEADER);
420 output_write(STRING_USAGE);
421 return INVALID_ARG;
424 OleUninitialize();
426 /* return the most recent error code, even if later DLLs succeed */
427 return ret;