wined3d: Fix / update reported driver model version.
[wine/zf.git] / programs / regsvr32 / regsvr32.c
blob730f1fef3dd3effde86881bdcbb488439ad1223e
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 static const WCHAR exe_name[] = {'\\','r','e','g','s','v','r','3','2','.','e','x','e',0};
118 #ifndef _WIN64
119 static const WCHAR sysnative[] = {'\\','S','y','s','N','a','t','i','v','e',0};
120 BOOL wow64;
121 #endif
122 WCHAR systemdir[MAX_PATH];
123 LPCWSTR args;
124 WCHAR *cmdline;
125 STARTUPINFOW si = {0};
126 PROCESS_INFORMATION pi;
128 #ifdef _WIN64
129 TRACE("restarting as 32-bit\n");
130 GetSystemWow64DirectoryW(systemdir, MAX_PATH);
131 #else
132 TRACE("restarting as 64-bit\n");
134 if (!IsWow64Process(GetCurrentProcess(), &wow64) || !wow64)
136 TRACE("not running in wow64, can't restart as 64-bit\n");
137 return;
140 GetWindowsDirectoryW(systemdir, MAX_PATH);
141 wcscat(systemdir, sysnative);
142 #endif
144 args = find_arg_start(GetCommandLineW());
146 cmdline = HeapAlloc(GetProcessHeap(), 0,
147 (wcslen(systemdir)+wcslen(exe_name)+wcslen(args)+1)*sizeof(WCHAR));
149 wcscpy(cmdline, systemdir);
150 wcscat(cmdline, exe_name);
151 wcscat(cmdline, args);
153 si.cb = sizeof(si);
155 if (CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
157 DWORD exit_code;
158 WaitForSingleObject(pi.hProcess, INFINITE);
159 GetExitCodeProcess(pi.hProcess, &exit_code);
160 ExitProcess(exit_code);
162 else
164 WINE_TRACE("failed to restart, err=%d\n", GetLastError());
167 HeapFree(GetProcessHeap(), 0, cmdline);
170 #ifdef _WIN64
171 # define ALT_BINARY_TYPE SCS_32BIT_BINARY
172 #else
173 # define ALT_BINARY_TYPE SCS_64BIT_BINARY
174 #endif
177 * Loads procedure.
179 * Parameters:
180 * strDll - name of the dll.
181 * procName - name of the procedure to load from the dll.
182 * DllHandle - a variable that receives the handle of the loaded dll.
183 * firstDll - true if this is the first dll in the command line.
185 static VOID *LoadProc(const WCHAR* strDll, const char* procName, HMODULE* DllHandle, BOOL firstDll)
187 VOID* (*proc)(void);
189 *DllHandle = LoadLibraryExW(strDll, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
190 if(!*DllHandle)
192 DWORD binary_type;
193 if (firstDll && GetLastError() == ERROR_BAD_EXE_FORMAT &&
194 GetBinaryTypeW(strDll, &binary_type) &&
195 binary_type == ALT_BINARY_TYPE)
197 reexec_self();
199 output_write(STRING_DLL_LOAD_FAILED, strDll);
200 ExitProcess(LOADLIBRARY_FAILED);
202 proc = (VOID *) GetProcAddress(*DllHandle, procName);
203 if(!proc)
205 output_write(STRING_PROC_NOT_IMPLEMENTED, procName, strDll);
206 FreeLibrary(*DllHandle);
207 return NULL;
209 return proc;
212 static int RegisterDll(const WCHAR* strDll, BOOL firstDll)
214 HRESULT hr;
215 DLLREGISTER pfRegister;
216 HMODULE DllHandle = NULL;
218 pfRegister = LoadProc(strDll, "DllRegisterServer", &DllHandle, firstDll);
219 if (!pfRegister)
220 return GETPROCADDRESS_FAILED;
222 hr = pfRegister();
223 if(FAILED(hr))
225 output_write(STRING_REGISTER_FAILED, strDll);
226 return DLLSERVER_FAILED;
228 output_write(STRING_REGISTER_SUCCESSFUL, strDll);
230 if(DllHandle)
231 FreeLibrary(DllHandle);
232 return 0;
235 static int UnregisterDll(const WCHAR* strDll, BOOL firstDll)
237 HRESULT hr;
238 DLLUNREGISTER pfUnregister;
239 HMODULE DllHandle = NULL;
241 pfUnregister = LoadProc(strDll, "DllUnregisterServer", &DllHandle, firstDll);
242 if (!pfUnregister)
243 return GETPROCADDRESS_FAILED;
245 hr = pfUnregister();
246 if(FAILED(hr))
248 output_write(STRING_UNREGISTER_FAILED, strDll);
249 return DLLSERVER_FAILED;
251 output_write(STRING_UNREGISTER_SUCCESSFUL, strDll);
253 if(DllHandle)
254 FreeLibrary(DllHandle);
255 return 0;
258 static int InstallDll(BOOL install, const WCHAR *strDll, const WCHAR *command_line, BOOL firstDll)
260 HRESULT hr;
261 DLLINSTALL pfInstall;
262 HMODULE DllHandle = NULL;
264 pfInstall = LoadProc(strDll, "DllInstall", &DllHandle, firstDll);
265 if (!pfInstall)
266 return GETPROCADDRESS_FAILED;
268 hr = pfInstall(install, command_line);
269 if(FAILED(hr))
271 if (install)
272 output_write(STRING_INSTALL_FAILED, strDll);
273 else
274 output_write(STRING_UNINSTALL_FAILED, strDll);
275 return DLLSERVER_FAILED;
277 if (install)
278 output_write(STRING_INSTALL_SUCCESSFUL, strDll);
279 else
280 output_write(STRING_UNINSTALL_SUCCESSFUL, strDll);
282 if(DllHandle)
283 FreeLibrary(DllHandle);
284 return 0;
287 static WCHAR *parse_command_line(WCHAR *command_line)
289 if (command_line[0] == ':' && command_line[1])
291 int len = lstrlenW(command_line);
293 command_line++;
294 len--;
295 /* remove double quotes */
296 if (command_line[0] == '"')
298 command_line++;
299 len--;
300 if (command_line[0])
302 len--;
303 command_line[len] = 0;
306 if (command_line[0])
307 return command_line;
309 return NULL;
312 int __cdecl wmain(int argc, WCHAR* argv[])
314 int i, res, ret = 0;
315 BOOL CallRegister = TRUE;
316 BOOL CallInstall = FALSE;
317 BOOL Unregister = FALSE;
318 BOOL DllFound = FALSE;
319 WCHAR* wsCommandLine = NULL;
320 WCHAR EmptyLine[1] = {0};
322 OleInitialize(NULL);
324 /* We mirror the Microsoft version by processing all of the flags before
325 * the files (e.g. regsvr32 file1 /s file2 is silent even for file1).
327 * Note the complication that this version may be passed Unix format filenames
328 * which could be mistaken for flags. The Windows version conveniently
329 * requires each flag to be separate (e.g. no /su), so we will simply
330 * assume that anything longer than /. is a filename.
332 for(i = 1; i < argc; i++)
334 if (argv[i][0] == '/' || argv[i][0] == '-')
336 if (!argv[i][1])
337 return INVALID_ARG;
339 if (argv[i][2] && argv[i][2] != ':')
340 continue;
342 switch (towlower(argv[i][1]))
344 case 'u':
345 Unregister = TRUE;
346 break;
347 case 's':
348 Silent = TRUE;
349 break;
350 case 'i':
351 CallInstall = TRUE;
352 wsCommandLine = parse_command_line(argv[i] + 2); /* argv[i] + strlen("/i") */
353 if (!wsCommandLine)
354 wsCommandLine = EmptyLine;
355 break;
356 case 'n':
357 CallRegister = FALSE;
358 break;
359 case 'c':
360 /* console output */;
361 break;
362 default:
363 output_write(STRING_UNRECOGNIZED_SWITCH, argv[i]);
364 output_write(STRING_USAGE);
365 return INVALID_ARG;
367 argv[i] = NULL;
371 if (!CallInstall && !CallRegister) /* flags: /n or /u /n */
372 return INVALID_ARG;
374 for (i = 1; i < argc; i++)
376 if (argv[i])
378 WCHAR *DllName = argv[i];
379 BOOL firstDll = !DllFound;
380 res = 0;
382 DllFound = TRUE;
383 if (CallInstall && Unregister)
384 res = InstallDll(!Unregister, DllName, wsCommandLine, firstDll);
386 /* The Windows version stops processing the current file on the first error. */
387 if (res)
389 ret = res;
390 continue;
393 if (!CallInstall || CallRegister)
395 if(Unregister)
396 res = UnregisterDll(DllName, firstDll);
397 else
398 res = RegisterDll(DllName, firstDll);
401 if (res)
403 ret = res;
404 continue;
407 if (CallInstall && !Unregister)
408 res = InstallDll(!Unregister, DllName, wsCommandLine, firstDll);
410 if (res)
412 ret = res;
413 continue;
418 if (!DllFound)
420 output_write(STRING_HEADER);
421 output_write(STRING_USAGE);
422 return INVALID_ARG;
425 OleUninitialize();
427 /* return the most recent error code, even if later DLLs succeed */
428 return ret;