combase/tests: Add tests for the hstring_private struct.
[wine.git] / programs / regsvr32 / regsvr32.c
blobc56479015d8720cffc8277868cb5b7bf091d7dcc
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 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 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 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( WORD machine )
117 WCHAR app[MAX_PATH];
118 LPCWSTR args;
119 WCHAR *cmdline;
120 ULONG i, machines[8];
121 HANDLE process = 0;
122 STARTUPINFOW si = {0};
123 PROCESS_INFORMATION pi;
124 void *cookie;
126 NtQuerySystemInformationEx( SystemSupportedProcessorArchitectures, &process, sizeof(process),
127 machines, sizeof(machines), NULL );
128 for (i = 0; machines[i]; i++) if (LOWORD(machines[i]) == machine) break;
129 if (!machines[i]) return;
130 if (HIWORD(machines[i]) & 4 /* native machine */) machine = IMAGE_FILE_MACHINE_TARGET_HOST;
131 if (!GetSystemWow64Directory2W( app, MAX_PATH, machine )) return;
132 wcscat( app, L"\\regsvr32.exe" );
134 TRACE( "restarting as %s\n", debugstr_w(app) );
136 args = find_arg_start(GetCommandLineW());
138 cmdline = HeapAlloc(GetProcessHeap(), 0,
139 (wcslen(app)+wcslen(args)+1)*sizeof(WCHAR));
141 wcscpy(cmdline, app);
142 wcscat(cmdline, args);
144 si.cb = sizeof(si);
146 Wow64DisableWow64FsRedirection(&cookie);
147 if (CreateProcessW(app, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
149 DWORD exit_code;
150 WaitForSingleObject(pi.hProcess, INFINITE);
151 GetExitCodeProcess(pi.hProcess, &exit_code);
152 ExitProcess(exit_code);
154 else
156 WINE_TRACE("failed to restart, err=%d\n", GetLastError());
158 Wow64RevertWow64FsRedirection(cookie);
159 HeapFree(GetProcessHeap(), 0, cmdline);
163 * Loads procedure.
165 * Parameters:
166 * strDll - name of the dll.
167 * procName - name of the procedure to load from the dll.
168 * DllHandle - a variable that receives the handle of the loaded dll.
169 * firstDll - true if this is the first dll in the command line.
171 static VOID *LoadProc(const WCHAR* strDll, const char* procName, HMODULE* DllHandle, BOOL firstDll)
173 VOID* (*proc)(void);
175 *DllHandle = LoadLibraryExW(strDll, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
176 if(!*DllHandle)
178 HMODULE module;
179 if (firstDll && GetLastError() == ERROR_BAD_EXE_FORMAT &&
180 (module = LoadLibraryExW(strDll, 0, LOAD_LIBRARY_AS_IMAGE_RESOURCE)))
182 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( (HMODULE)((ULONG_PTR)module & ~3) );
183 reexec_self( nt->FileHeader.Machine );
185 output_write(STRING_DLL_LOAD_FAILED, strDll);
186 ExitProcess(LOADLIBRARY_FAILED);
188 proc = (VOID *) GetProcAddress(*DllHandle, procName);
189 if(!proc)
191 output_write(STRING_PROC_NOT_IMPLEMENTED, procName, strDll);
192 FreeLibrary(*DllHandle);
193 return NULL;
195 return proc;
198 static int RegisterDll(const WCHAR* strDll, BOOL firstDll)
200 HRESULT hr;
201 DLLREGISTER pfRegister;
202 HMODULE DllHandle = NULL;
204 pfRegister = LoadProc(strDll, "DllRegisterServer", &DllHandle, firstDll);
205 if (!pfRegister)
206 return GETPROCADDRESS_FAILED;
208 hr = pfRegister();
209 if(FAILED(hr))
211 output_write(STRING_REGISTER_FAILED, strDll);
212 return DLLSERVER_FAILED;
214 output_write(STRING_REGISTER_SUCCESSFUL, strDll);
216 if(DllHandle)
217 FreeLibrary(DllHandle);
218 return 0;
221 static int UnregisterDll(const WCHAR* strDll, BOOL firstDll)
223 HRESULT hr;
224 DLLUNREGISTER pfUnregister;
225 HMODULE DllHandle = NULL;
227 pfUnregister = LoadProc(strDll, "DllUnregisterServer", &DllHandle, firstDll);
228 if (!pfUnregister)
229 return GETPROCADDRESS_FAILED;
231 hr = pfUnregister();
232 if(FAILED(hr))
234 output_write(STRING_UNREGISTER_FAILED, strDll);
235 return DLLSERVER_FAILED;
237 output_write(STRING_UNREGISTER_SUCCESSFUL, strDll);
239 if(DllHandle)
240 FreeLibrary(DllHandle);
241 return 0;
244 static int InstallDll(BOOL install, const WCHAR *strDll, const WCHAR *command_line, BOOL firstDll)
246 HRESULT hr;
247 DLLINSTALL pfInstall;
248 HMODULE DllHandle = NULL;
250 pfInstall = LoadProc(strDll, "DllInstall", &DllHandle, firstDll);
251 if (!pfInstall)
252 return GETPROCADDRESS_FAILED;
254 hr = pfInstall(install, command_line);
255 if(FAILED(hr))
257 if (install)
258 output_write(STRING_INSTALL_FAILED, strDll);
259 else
260 output_write(STRING_UNINSTALL_FAILED, strDll);
261 return DLLSERVER_FAILED;
263 if (install)
264 output_write(STRING_INSTALL_SUCCESSFUL, strDll);
265 else
266 output_write(STRING_UNINSTALL_SUCCESSFUL, strDll);
268 if(DllHandle)
269 FreeLibrary(DllHandle);
270 return 0;
273 static WCHAR *parse_command_line(WCHAR *command_line)
275 if (command_line[0] == ':' && command_line[1])
277 int len = lstrlenW(command_line);
279 command_line++;
280 len--;
281 /* remove double quotes */
282 if (command_line[0] == '"')
284 command_line++;
285 len--;
286 if (command_line[0])
288 len--;
289 command_line[len] = 0;
292 if (command_line[0])
293 return command_line;
295 return NULL;
298 int __cdecl wmain(int argc, WCHAR* argv[])
300 int i, res, ret = 0;
301 BOOL CallRegister = TRUE;
302 BOOL CallInstall = FALSE;
303 BOOL Unregister = FALSE;
304 BOOL DllFound = FALSE;
305 WCHAR* wsCommandLine = NULL;
306 WCHAR EmptyLine[] = L"";
308 OleInitialize(NULL);
310 /* We mirror the Microsoft version by processing all of the flags before
311 * the files (e.g. regsvr32 file1 /s file2 is silent even for file1).
313 * Note the complication that this version may be passed Unix format filenames
314 * which could be mistaken for flags. The Windows version conveniently
315 * requires each flag to be separate (e.g. no /su), so we will simply
316 * assume that anything longer than /. is a filename.
318 for(i = 1; i < argc; i++)
320 if (argv[i][0] == '/' || argv[i][0] == '-')
322 if (!argv[i][1])
323 return INVALID_ARG;
325 if (argv[i][2] && argv[i][2] != ':')
326 continue;
328 switch (towlower(argv[i][1]))
330 case 'u':
331 Unregister = TRUE;
332 break;
333 case 's':
334 Silent = TRUE;
335 break;
336 case 'i':
337 CallInstall = TRUE;
338 wsCommandLine = parse_command_line(argv[i] + 2); /* argv[i] + strlen("/i") */
339 if (!wsCommandLine)
340 wsCommandLine = EmptyLine;
341 break;
342 case 'n':
343 CallRegister = FALSE;
344 break;
345 case 'c':
346 /* console output */;
347 break;
348 default:
349 output_write(STRING_UNRECOGNIZED_SWITCH, argv[i]);
350 output_write(STRING_USAGE);
351 return INVALID_ARG;
353 argv[i] = NULL;
357 if (!CallInstall && !CallRegister) /* flags: /n or /u /n */
358 return INVALID_ARG;
360 for (i = 1; i < argc; i++)
362 if (argv[i])
364 WCHAR *DllName = argv[i];
365 BOOL firstDll = !DllFound;
366 res = 0;
368 DllFound = TRUE;
369 if (CallInstall && Unregister)
370 res = InstallDll(!Unregister, DllName, wsCommandLine, firstDll);
372 /* The Windows version stops processing the current file on the first error. */
373 if (res)
375 ret = res;
376 continue;
379 if (!CallInstall || CallRegister)
381 if(Unregister)
382 res = UnregisterDll(DllName, firstDll);
383 else
384 res = RegisterDll(DllName, firstDll);
387 if (res)
389 ret = res;
390 continue;
393 if (CallInstall && !Unregister)
394 res = InstallDll(!Unregister, DllName, wsCommandLine, firstDll);
396 if (res)
398 ret = res;
399 continue;
404 if (!DllFound)
406 output_write(STRING_HEADER);
407 output_write(STRING_USAGE);
408 return INVALID_ARG;
411 OleUninitialize();
413 /* return the most recent error code, even if later DLLs succeed */
414 return ret;