regsvr32: Use a string literal for an empty string.
[wine.git] / programs / regsvr32 / regsvr32.c
blobecfd95234dd7b5e20f4a9c7836ae7b6aed615db9
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 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
55 fmt, 0, 0, (LPWSTR)&str, 0, &va_args);
56 __ms_va_end(va_args);
57 if (len == 0 && GetLastError() != ERROR_NO_WORK_DONE)
59 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
60 return;
63 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &nOut, NULL);
65 /* WriteConsole fails if its output is redirected to a file.
66 * If this occurs, we should use an OEM codepage and call WriteFile.
68 if (!ret)
70 DWORD lenA;
71 char *strA;
73 lenA = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, NULL, 0, NULL, NULL);
74 strA = HeapAlloc(GetProcessHeap(), 0, lenA);
75 if (strA)
77 WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, strA, lenA, NULL, NULL);
78 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, lenA, &nOut, FALSE);
79 HeapFree(GetProcessHeap(), 0, strA);
82 LocalFree(str);
85 static LPCWSTR find_arg_start(LPCWSTR cmdline)
87 LPCWSTR s;
88 BOOL in_quotes;
89 int bcount;
91 bcount=0;
92 in_quotes=FALSE;
93 s=cmdline;
94 while (1) {
95 if (*s==0 || ((*s=='\t' || *s==' ') && !in_quotes)) {
96 /* end of this command line argument */
97 break;
98 } else if (*s=='\\') {
99 /* '\', count them */
100 bcount++;
101 } else if ((*s=='"') && ((bcount & 1)==0)) {
102 /* unescaped '"' */
103 in_quotes=!in_quotes;
104 bcount=0;
105 } else {
106 /* a regular character */
107 bcount=0;
109 s++;
111 return s;
114 static void reexec_self(void)
116 /* restart current process as 32-bit or 64-bit with same command line */
117 #ifndef _WIN64
118 BOOL wow64;
119 #endif
120 WCHAR systemdir[MAX_PATH];
121 LPCWSTR args;
122 WCHAR *cmdline;
123 STARTUPINFOW si = {0};
124 PROCESS_INFORMATION pi;
126 #ifdef _WIN64
127 TRACE("restarting as 32-bit\n");
128 GetSystemWow64DirectoryW(systemdir, MAX_PATH);
129 #else
130 TRACE("restarting as 64-bit\n");
132 if (!IsWow64Process(GetCurrentProcess(), &wow64) || !wow64)
134 TRACE("not running in wow64, can't restart as 64-bit\n");
135 return;
138 GetWindowsDirectoryW(systemdir, MAX_PATH);
139 wcscat(systemdir, L"\\SysNative");
140 #endif
142 args = find_arg_start(GetCommandLineW());
144 cmdline = HeapAlloc(GetProcessHeap(), 0,
145 (wcslen(systemdir)+wcslen(L"\\regsvr32.exe")+wcslen(args)+1)*sizeof(WCHAR));
147 wcscpy(cmdline, systemdir);
148 wcscat(cmdline, L"\\regsvr32.exe");
149 wcscat(cmdline, args);
151 si.cb = sizeof(si);
153 if (CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
155 DWORD exit_code;
156 WaitForSingleObject(pi.hProcess, INFINITE);
157 GetExitCodeProcess(pi.hProcess, &exit_code);
158 ExitProcess(exit_code);
160 else
162 WINE_TRACE("failed to restart, err=%d\n", GetLastError());
165 HeapFree(GetProcessHeap(), 0, cmdline);
168 #ifdef _WIN64
169 # define ALT_BINARY_TYPE SCS_32BIT_BINARY
170 #else
171 # define ALT_BINARY_TYPE SCS_64BIT_BINARY
172 #endif
175 * Loads procedure.
177 * Parameters:
178 * strDll - name of the dll.
179 * procName - name of the procedure to load from the dll.
180 * DllHandle - a variable that receives the handle of the loaded dll.
181 * firstDll - true if this is the first dll in the command line.
183 static VOID *LoadProc(const WCHAR* strDll, const char* procName, HMODULE* DllHandle, BOOL firstDll)
185 VOID* (*proc)(void);
187 *DllHandle = LoadLibraryExW(strDll, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
188 if(!*DllHandle)
190 DWORD binary_type;
191 if (firstDll && GetLastError() == ERROR_BAD_EXE_FORMAT &&
192 GetBinaryTypeW(strDll, &binary_type) &&
193 binary_type == ALT_BINARY_TYPE)
195 reexec_self();
197 output_write(STRING_DLL_LOAD_FAILED, strDll);
198 ExitProcess(LOADLIBRARY_FAILED);
200 proc = (VOID *) GetProcAddress(*DllHandle, procName);
201 if(!proc)
203 output_write(STRING_PROC_NOT_IMPLEMENTED, procName, strDll);
204 FreeLibrary(*DllHandle);
205 return NULL;
207 return proc;
210 static int RegisterDll(const WCHAR* strDll, BOOL firstDll)
212 HRESULT hr;
213 DLLREGISTER pfRegister;
214 HMODULE DllHandle = NULL;
216 pfRegister = LoadProc(strDll, "DllRegisterServer", &DllHandle, firstDll);
217 if (!pfRegister)
218 return GETPROCADDRESS_FAILED;
220 hr = pfRegister();
221 if(FAILED(hr))
223 output_write(STRING_REGISTER_FAILED, strDll);
224 return DLLSERVER_FAILED;
226 output_write(STRING_REGISTER_SUCCESSFUL, strDll);
228 if(DllHandle)
229 FreeLibrary(DllHandle);
230 return 0;
233 static int UnregisterDll(const WCHAR* strDll, BOOL firstDll)
235 HRESULT hr;
236 DLLUNREGISTER pfUnregister;
237 HMODULE DllHandle = NULL;
239 pfUnregister = LoadProc(strDll, "DllUnregisterServer", &DllHandle, firstDll);
240 if (!pfUnregister)
241 return GETPROCADDRESS_FAILED;
243 hr = pfUnregister();
244 if(FAILED(hr))
246 output_write(STRING_UNREGISTER_FAILED, strDll);
247 return DLLSERVER_FAILED;
249 output_write(STRING_UNREGISTER_SUCCESSFUL, strDll);
251 if(DllHandle)
252 FreeLibrary(DllHandle);
253 return 0;
256 static int InstallDll(BOOL install, const WCHAR *strDll, const WCHAR *command_line, BOOL firstDll)
258 HRESULT hr;
259 DLLINSTALL pfInstall;
260 HMODULE DllHandle = NULL;
262 pfInstall = LoadProc(strDll, "DllInstall", &DllHandle, firstDll);
263 if (!pfInstall)
264 return GETPROCADDRESS_FAILED;
266 hr = pfInstall(install, command_line);
267 if(FAILED(hr))
269 if (install)
270 output_write(STRING_INSTALL_FAILED, strDll);
271 else
272 output_write(STRING_UNINSTALL_FAILED, strDll);
273 return DLLSERVER_FAILED;
275 if (install)
276 output_write(STRING_INSTALL_SUCCESSFUL, strDll);
277 else
278 output_write(STRING_UNINSTALL_SUCCESSFUL, strDll);
280 if(DllHandle)
281 FreeLibrary(DllHandle);
282 return 0;
285 static WCHAR *parse_command_line(WCHAR *command_line)
287 if (command_line[0] == ':' && command_line[1])
289 int len = lstrlenW(command_line);
291 command_line++;
292 len--;
293 /* remove double quotes */
294 if (command_line[0] == '"')
296 command_line++;
297 len--;
298 if (command_line[0])
300 len--;
301 command_line[len] = 0;
304 if (command_line[0])
305 return command_line;
307 return NULL;
310 int __cdecl wmain(int argc, WCHAR* argv[])
312 int i, res, ret = 0;
313 BOOL CallRegister = TRUE;
314 BOOL CallInstall = FALSE;
315 BOOL Unregister = FALSE;
316 BOOL DllFound = FALSE;
317 WCHAR* wsCommandLine = NULL;
318 WCHAR EmptyLine[] = L"";
320 OleInitialize(NULL);
322 /* We mirror the Microsoft version by processing all of the flags before
323 * the files (e.g. regsvr32 file1 /s file2 is silent even for file1).
325 * Note the complication that this version may be passed Unix format filenames
326 * which could be mistaken for flags. The Windows version conveniently
327 * requires each flag to be separate (e.g. no /su), so we will simply
328 * assume that anything longer than /. is a filename.
330 for(i = 1; i < argc; i++)
332 if (argv[i][0] == '/' || argv[i][0] == '-')
334 if (!argv[i][1])
335 return INVALID_ARG;
337 if (argv[i][2] && argv[i][2] != ':')
338 continue;
340 switch (towlower(argv[i][1]))
342 case 'u':
343 Unregister = TRUE;
344 break;
345 case 's':
346 Silent = TRUE;
347 break;
348 case 'i':
349 CallInstall = TRUE;
350 wsCommandLine = parse_command_line(argv[i] + 2); /* argv[i] + strlen("/i") */
351 if (!wsCommandLine)
352 wsCommandLine = EmptyLine;
353 break;
354 case 'n':
355 CallRegister = FALSE;
356 break;
357 case 'c':
358 /* console output */;
359 break;
360 default:
361 output_write(STRING_UNRECOGNIZED_SWITCH, argv[i]);
362 output_write(STRING_USAGE);
363 return INVALID_ARG;
365 argv[i] = NULL;
369 if (!CallInstall && !CallRegister) /* flags: /n or /u /n */
370 return INVALID_ARG;
372 for (i = 1; i < argc; i++)
374 if (argv[i])
376 WCHAR *DllName = argv[i];
377 BOOL firstDll = !DllFound;
378 res = 0;
380 DllFound = TRUE;
381 if (CallInstall && Unregister)
382 res = InstallDll(!Unregister, DllName, wsCommandLine, firstDll);
384 /* The Windows version stops processing the current file on the first error. */
385 if (res)
387 ret = res;
388 continue;
391 if (!CallInstall || CallRegister)
393 if(Unregister)
394 res = UnregisterDll(DllName, firstDll);
395 else
396 res = RegisterDll(DllName, firstDll);
399 if (res)
401 ret = res;
402 continue;
405 if (CallInstall && !Unregister)
406 res = InstallDll(!Unregister, DllName, wsCommandLine, firstDll);
408 if (res)
410 ret = res;
411 continue;
416 if (!DllFound)
418 output_write(STRING_HEADER);
419 output_write(STRING_USAGE);
420 return INVALID_ARG;
423 OleUninitialize();
425 /* return the most recent error code, even if later DLLs succeed */
426 return ret;