strmbase: Standardize aggregation of the seeking passthrough object.
[wine.git] / programs / regsvr32 / regsvr32.c
bloba7fb12e0fc156d93648512beb48efdea086a9c8b
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 <ole2.h>
27 #include "regsvr32.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(regsvr32);
32 typedef HRESULT (WINAPI *DLLREGISTER) (void);
33 typedef HRESULT (WINAPI *DLLUNREGISTER) (void);
34 typedef HRESULT (WINAPI *DLLINSTALL) (BOOL,LPCWSTR);
36 static BOOL Silent = FALSE;
38 static void WINAPIV output_write(UINT id, ...)
40 WCHAR fmt[1024];
41 __ms_va_list va_args;
42 WCHAR *str;
43 DWORD len, nOut, ret;
45 if (Silent) return;
47 if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
49 WINE_FIXME("LoadString failed with %d\n", GetLastError());
50 return;
53 __ms_va_start(va_args, id);
54 SetLastError(NO_ERROR);
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() != NO_ERROR)
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 static const WCHAR exe_name[] = {'\\','r','e','g','s','v','r','3','2','.','e','x','e',0};
119 #ifndef _WIN64
120 static const WCHAR sysnative[] = {'\\','S','y','s','N','a','t','i','v','e',0};
121 BOOL wow64;
122 #endif
123 WCHAR systemdir[MAX_PATH];
124 LPCWSTR args;
125 WCHAR *cmdline;
126 STARTUPINFOW si = {0};
127 PROCESS_INFORMATION pi;
129 #ifdef _WIN64
130 TRACE("restarting as 32-bit\n");
131 GetSystemWow64DirectoryW(systemdir, MAX_PATH);
132 #else
133 TRACE("restarting as 64-bit\n");
135 if (!IsWow64Process(GetCurrentProcess(), &wow64) || !wow64)
137 TRACE("not running in wow64, can't restart as 64-bit\n");
138 return;
141 GetWindowsDirectoryW(systemdir, MAX_PATH);
142 wcscat(systemdir, sysnative);
143 #endif
145 args = find_arg_start(GetCommandLineW());
147 cmdline = HeapAlloc(GetProcessHeap(), 0,
148 (wcslen(systemdir)+wcslen(exe_name)+wcslen(args)+1)*sizeof(WCHAR));
150 wcscpy(cmdline, systemdir);
151 wcscat(cmdline, exe_name);
152 wcscat(cmdline, args);
154 si.cb = sizeof(si);
156 if (CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
158 DWORD exit_code;
159 WaitForSingleObject(pi.hProcess, INFINITE);
160 GetExitCodeProcess(pi.hProcess, &exit_code);
161 ExitProcess(exit_code);
163 else
165 WINE_TRACE("failed to restart, err=%d\n", GetLastError());
168 HeapFree(GetProcessHeap(), 0, cmdline);
171 #ifdef _WIN64
172 # define ALT_BINARY_TYPE SCS_32BIT_BINARY
173 #else
174 # define ALT_BINARY_TYPE SCS_64BIT_BINARY
175 #endif
178 * Loads procedure.
180 * Parameters:
181 * strDll - name of the dll.
182 * procName - name of the procedure to load from the dll.
183 * DllHandle - a variable that receives the handle of the loaded dll.
184 * firstDll - true if this is the first dll in the command line.
186 static VOID *LoadProc(const WCHAR* strDll, const char* procName, HMODULE* DllHandle, BOOL firstDll)
188 VOID* (*proc)(void);
190 *DllHandle = LoadLibraryExW(strDll, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
191 if(!*DllHandle)
193 DWORD binary_type;
194 if (firstDll && GetLastError() == ERROR_BAD_EXE_FORMAT &&
195 GetBinaryTypeW(strDll, &binary_type) &&
196 binary_type == ALT_BINARY_TYPE)
198 reexec_self();
200 output_write(STRING_DLL_LOAD_FAILED, strDll);
201 ExitProcess(LOADLIBRARY_FAILED);
203 proc = (VOID *) GetProcAddress(*DllHandle, procName);
204 if(!proc)
206 output_write(STRING_PROC_NOT_IMPLEMENTED, procName, strDll);
207 FreeLibrary(*DllHandle);
208 return NULL;
210 return proc;
213 static int RegisterDll(const WCHAR* strDll, BOOL firstDll)
215 HRESULT hr;
216 DLLREGISTER pfRegister;
217 HMODULE DllHandle = NULL;
219 pfRegister = LoadProc(strDll, "DllRegisterServer", &DllHandle, firstDll);
220 if (!pfRegister)
221 return GETPROCADDRESS_FAILED;
223 hr = pfRegister();
224 if(FAILED(hr))
226 output_write(STRING_REGISTER_FAILED, strDll);
227 return DLLSERVER_FAILED;
229 output_write(STRING_REGISTER_SUCCESSFUL, strDll);
231 if(DllHandle)
232 FreeLibrary(DllHandle);
233 return 0;
236 static int UnregisterDll(const WCHAR* strDll, BOOL firstDll)
238 HRESULT hr;
239 DLLUNREGISTER pfUnregister;
240 HMODULE DllHandle = NULL;
242 pfUnregister = LoadProc(strDll, "DllUnregisterServer", &DllHandle, firstDll);
243 if (!pfUnregister)
244 return GETPROCADDRESS_FAILED;
246 hr = pfUnregister();
247 if(FAILED(hr))
249 output_write(STRING_UNREGISTER_FAILED, strDll);
250 return DLLSERVER_FAILED;
252 output_write(STRING_UNREGISTER_SUCCESSFUL, strDll);
254 if(DllHandle)
255 FreeLibrary(DllHandle);
256 return 0;
259 static int InstallDll(BOOL install, const WCHAR *strDll, const WCHAR *command_line, BOOL firstDll)
261 HRESULT hr;
262 DLLINSTALL pfInstall;
263 HMODULE DllHandle = NULL;
265 pfInstall = LoadProc(strDll, "DllInstall", &DllHandle, firstDll);
266 if (!pfInstall)
267 return GETPROCADDRESS_FAILED;
269 hr = pfInstall(install, command_line);
270 if(FAILED(hr))
272 if (install)
273 output_write(STRING_INSTALL_FAILED, strDll);
274 else
275 output_write(STRING_UNINSTALL_FAILED, strDll);
276 return DLLSERVER_FAILED;
278 if (install)
279 output_write(STRING_INSTALL_SUCCESSFUL, strDll);
280 else
281 output_write(STRING_UNINSTALL_SUCCESSFUL, strDll);
283 if(DllHandle)
284 FreeLibrary(DllHandle);
285 return 0;
288 static WCHAR *parse_command_line(WCHAR *command_line)
290 if (command_line[0] == ':' && command_line[1])
292 int len = lstrlenW(command_line);
294 command_line++;
295 len--;
296 /* remove double quotes */
297 if (command_line[0] == '"')
299 command_line++;
300 len--;
301 if (command_line[0])
303 len--;
304 command_line[len] = 0;
307 if (command_line[0])
308 return command_line;
310 return NULL;
313 int __cdecl wmain(int argc, WCHAR* argv[])
315 int i, res, ret = 0;
316 BOOL CallRegister = TRUE;
317 BOOL CallInstall = FALSE;
318 BOOL Unregister = FALSE;
319 BOOL DllFound = FALSE;
320 WCHAR* wsCommandLine = NULL;
321 WCHAR EmptyLine[1] = {0};
323 OleInitialize(NULL);
325 /* We mirror the Microsoft version by processing all of the flags before
326 * the files (e.g. regsvr32 file1 /s file2 is silent even for file1).
328 * Note the complication that this version may be passed Unix format filenames
329 * which could be mistaken for flags. The Windows version conveniently
330 * requires each flag to be separate (e.g. no /su), so we will simply
331 * assume that anything longer than /. is a filename.
333 for(i = 1; i < argc; i++)
335 if (argv[i][0] == '/' || argv[i][0] == '-')
337 if (!argv[i][1])
338 return INVALID_ARG;
340 if (argv[i][2] && argv[i][2] != ':')
341 continue;
343 switch (towlower(argv[i][1]))
345 case 'u':
346 Unregister = TRUE;
347 break;
348 case 's':
349 Silent = TRUE;
350 break;
351 case 'i':
352 CallInstall = TRUE;
353 wsCommandLine = parse_command_line(argv[i] + 2); /* argv[i] + strlen("/i") */
354 if (!wsCommandLine)
355 wsCommandLine = EmptyLine;
356 break;
357 case 'n':
358 CallRegister = FALSE;
359 break;
360 case 'c':
361 /* console output */;
362 break;
363 default:
364 output_write(STRING_UNRECOGNIZED_SWITCH, argv[i]);
365 output_write(STRING_USAGE);
366 return INVALID_ARG;
368 argv[i] = NULL;
372 if (!CallInstall && !CallRegister) /* flags: /n or /u /n */
373 return INVALID_ARG;
375 for (i = 1; i < argc; i++)
377 if (argv[i])
379 WCHAR *DllName = argv[i];
380 BOOL firstDll = !DllFound;
381 res = 0;
383 DllFound = TRUE;
384 if (CallInstall && Unregister)
385 res = InstallDll(!Unregister, DllName, wsCommandLine, firstDll);
387 /* The Windows version stops processing the current file on the first error. */
388 if (res)
390 ret = res;
391 continue;
394 if (!CallInstall || CallRegister)
396 if(Unregister)
397 res = UnregisterDll(DllName, firstDll);
398 else
399 res = RegisterDll(DllName, firstDll);
402 if (res)
404 ret = res;
405 continue;
408 if (CallInstall && !Unregister)
409 res = InstallDll(!Unregister, DllName, wsCommandLine, firstDll);
411 if (res)
413 ret = res;
414 continue;
419 if (!DllFound)
421 output_write(STRING_HEADER);
422 output_write(STRING_USAGE);
423 return INVALID_ARG;
426 OleUninitialize();
428 /* return the most recent error code, even if later DLLs succeed */
429 return ret;