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
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(regsvr32
);
34 typedef HRESULT (WINAPI
*DLLREGISTER
) (void);
35 typedef HRESULT (WINAPI
*DLLUNREGISTER
) (void);
36 typedef HRESULT (WINAPI
*DLLINSTALL
) (BOOL
,LPCWSTR
);
40 static void WINAPIV
output_write(BOOL with_usage
, UINT id
, ...)
48 current_lcid
= GetThreadLocale();
49 if (Silent
) /* force en-US not to have localized strings */
50 SetThreadLocale(MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
));
52 if (!LoadStringW(GetModuleHandleW(NULL
), id
, fmt
, ARRAY_SIZE(fmt
)))
54 WINE_FIXME("LoadString failed with %ld\n", GetLastError());
55 SetThreadLocale(current_lcid
);
59 va_start(va_args
, id
);
60 len
= FormatMessageW(FORMAT_MESSAGE_FROM_STRING
,
61 fmt
, 0, 0, buffer
, ARRAY_SIZE(buffer
), &va_args
);
63 if (len
== 0 && GetLastError() != ERROR_NO_WORK_DONE
)
65 WINE_FIXME("Could not format string: le=%lu, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt
));
66 SetThreadLocale(current_lcid
);
70 !LoadStringW(GetModuleHandleW(NULL
), STRING_USAGE
,
71 &buffer
[wcslen(buffer
)], ARRAY_SIZE(buffer
) - wcslen(buffer
)))
73 WINE_FIXME("LoadString failed with %ld\n", GetLastError());
74 SetThreadLocale(current_lcid
);
78 MESSAGE("%ls", buffer
);
80 MessageBoxW(NULL
, buffer
, L
"RegSvr32", MB_OK
);
81 SetThreadLocale(current_lcid
);
84 static LPCWSTR
find_arg_start(LPCWSTR cmdline
)
94 if (*s
==0 || ((*s
=='\t' || *s
==' ') && !in_quotes
)) {
95 /* end of this command line argument */
97 } else if (*s
=='\\') {
100 } else if ((*s
=='"') && ((bcount
& 1)==0)) {
102 in_quotes
=!in_quotes
;
105 /* a regular character */
113 static void reexec_self( WORD machine
)
115 SYSTEM_SUPPORTED_PROCESSOR_ARCHITECTURES_INFORMATION machines
[8];
120 STARTUPINFOW si
= {0};
121 PROCESS_INFORMATION pi
;
125 NtQuerySystemInformationEx( SystemSupportedProcessorArchitectures
, &process
, sizeof(process
),
126 machines
, sizeof(machines
), NULL
);
127 for (i
= 0; machines
[i
].Machine
; i
++) if (machines
[i
].Machine
== machine
) break;
128 if (!machines
[i
].Machine
) return;
129 if (machines
[i
].Native
) machine
= IMAGE_FILE_MACHINE_TARGET_HOST
;
130 if (!GetSystemWow64Directory2W( app
, MAX_PATH
, machine
)) return;
131 wcscat( app
, L
"\\regsvr32.exe" );
133 TRACE( "restarting as %s\n", debugstr_w(app
) );
135 args
= find_arg_start(GetCommandLineW());
137 cmdline
= HeapAlloc(GetProcessHeap(), 0,
138 (wcslen(app
)+wcslen(args
)+1)*sizeof(WCHAR
));
140 wcscpy(cmdline
, app
);
141 wcscat(cmdline
, args
);
145 Wow64DisableWow64FsRedirection(&cookie
);
146 if (CreateProcessW(app
, cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
))
149 WaitForSingleObject(pi
.hProcess
, INFINITE
);
150 GetExitCodeProcess(pi
.hProcess
, &exit_code
);
151 ExitProcess(exit_code
);
155 WINE_TRACE("failed to restart, err=%ld\n", GetLastError());
157 Wow64RevertWow64FsRedirection(cookie
);
158 HeapFree(GetProcessHeap(), 0, cmdline
);
165 * strDll - name of the dll.
166 * procName - name of the procedure to load from the dll.
167 * DllHandle - a variable that receives the handle of the loaded dll.
168 * firstDll - true if this is the first dll in the command line.
170 static VOID
*LoadProc(const WCHAR
* strDll
, const char* procName
, HMODULE
* DllHandle
, BOOL firstDll
)
174 *DllHandle
= LoadLibraryExW(strDll
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
);
178 if (firstDll
&& GetLastError() == ERROR_BAD_EXE_FORMAT
&&
179 (module
= LoadLibraryExW(strDll
, 0, LOAD_LIBRARY_AS_IMAGE_RESOURCE
)))
181 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( (HMODULE
)((ULONG_PTR
)module
& ~3) );
182 reexec_self( nt
->FileHeader
.Machine
);
184 output_write(FALSE
, STRING_DLL_LOAD_FAILED
, strDll
);
185 ExitProcess(LOADLIBRARY_FAILED
);
187 proc
= (VOID
*) GetProcAddress(*DllHandle
, procName
);
190 output_write(FALSE
, STRING_PROC_NOT_IMPLEMENTED
, procName
, strDll
);
191 FreeLibrary(*DllHandle
);
197 static int RegisterDll(const WCHAR
* strDll
, BOOL firstDll
)
200 DLLREGISTER pfRegister
;
201 HMODULE DllHandle
= NULL
;
203 pfRegister
= LoadProc(strDll
, "DllRegisterServer", &DllHandle
, firstDll
);
205 return GETPROCADDRESS_FAILED
;
210 output_write(FALSE
, STRING_REGISTER_FAILED
, strDll
);
211 return DLLSERVER_FAILED
;
213 output_write(FALSE
, STRING_REGISTER_SUCCESSFUL
, strDll
);
216 FreeLibrary(DllHandle
);
220 static int UnregisterDll(const WCHAR
* strDll
, BOOL firstDll
)
223 DLLUNREGISTER pfUnregister
;
224 HMODULE DllHandle
= NULL
;
226 pfUnregister
= LoadProc(strDll
, "DllUnregisterServer", &DllHandle
, firstDll
);
228 return GETPROCADDRESS_FAILED
;
233 output_write(FALSE
, STRING_UNREGISTER_FAILED
, strDll
);
234 return DLLSERVER_FAILED
;
236 output_write(FALSE
, STRING_UNREGISTER_SUCCESSFUL
, strDll
);
239 FreeLibrary(DllHandle
);
243 static int InstallDll(BOOL install
, const WCHAR
*strDll
, const WCHAR
*command_line
, BOOL firstDll
)
246 DLLINSTALL pfInstall
;
247 HMODULE DllHandle
= NULL
;
249 pfInstall
= LoadProc(strDll
, "DllInstall", &DllHandle
, firstDll
);
251 return GETPROCADDRESS_FAILED
;
253 hr
= pfInstall(install
, command_line
);
257 output_write(FALSE
, STRING_INSTALL_FAILED
, strDll
);
259 output_write(FALSE
, STRING_UNINSTALL_FAILED
, strDll
);
260 return DLLSERVER_FAILED
;
263 output_write(FALSE
, STRING_INSTALL_SUCCESSFUL
, strDll
);
265 output_write(FALSE
, STRING_UNINSTALL_SUCCESSFUL
, strDll
);
268 FreeLibrary(DllHandle
);
272 static WCHAR
*parse_command_line(WCHAR
*command_line
)
274 if (command_line
[0] == ':' && command_line
[1])
276 int len
= lstrlenW(command_line
);
280 /* remove double quotes */
281 if (command_line
[0] == '"')
288 command_line
[len
] = 0;
297 int __cdecl
wmain(int argc
, WCHAR
* argv
[])
300 BOOL CallRegister
= TRUE
;
301 BOOL CallInstall
= FALSE
;
302 BOOL Unregister
= FALSE
;
303 BOOL DllFound
= FALSE
;
304 WCHAR
* wsCommandLine
= NULL
;
305 WCHAR EmptyLine
[] = L
"";
309 /* We mirror the Microsoft version by processing all of the flags before
310 * the files (e.g. regsvr32 file1 /s file2 is silent even for file1).
312 * Note the complication that this version may be passed Unix format filenames
313 * which could be mistaken for flags. The Windows version conveniently
314 * requires each flag to be separate (e.g. no /su), so we will simply
315 * assume that anything longer than /. is a filename.
317 for(i
= 1; i
< argc
; i
++)
319 if (argv
[i
][0] == '/' || argv
[i
][0] == '-')
324 if (argv
[i
][2] && argv
[i
][2] != ':')
327 switch (towlower(argv
[i
][1]))
337 wsCommandLine
= parse_command_line(argv
[i
] + 2); /* argv[i] + strlen("/i") */
339 wsCommandLine
= EmptyLine
;
342 CallRegister
= FALSE
;
345 output_write(TRUE
, STRING_UNRECOGNIZED_SWITCH
, argv
[i
]);
352 if (!CallInstall
&& !CallRegister
) /* flags: /n or /u /n */
355 for (i
= 1; i
< argc
; i
++)
359 WCHAR
*DllName
= argv
[i
];
360 BOOL firstDll
= !DllFound
;
364 if (CallInstall
&& Unregister
)
365 res
= InstallDll(!Unregister
, DllName
, wsCommandLine
, firstDll
);
367 /* The Windows version stops processing the current file on the first error. */
374 if (!CallInstall
|| CallRegister
)
377 res
= UnregisterDll(DllName
, firstDll
);
379 res
= RegisterDll(DllName
, firstDll
);
388 if (CallInstall
&& !Unregister
)
389 res
= InstallDll(!Unregister
, DllName
, wsCommandLine
, firstDll
);
401 output_write(TRUE
, STRING_HEADER
);
407 /* return the most recent error code, even if later DLLs succeed */