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
27 #include "wine/port.h"
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(regsvr32
);
37 typedef HRESULT (WINAPI
*DLLREGISTER
) (void);
38 typedef HRESULT (WINAPI
*DLLUNREGISTER
) (void);
39 typedef HRESULT (WINAPI
*DLLINSTALL
) (BOOL
,LPCWSTR
);
41 static BOOL Silent
= FALSE
;
43 static void __cdecl
output_write(UINT id
, ...)
52 if (!LoadStringW(GetModuleHandleW(NULL
), id
, fmt
, sizeof(fmt
)/sizeof(fmt
[0])))
54 WINE_FIXME("LoadString failed with %d\n", GetLastError());
58 __ms_va_start(va_args
, id
);
59 SetLastError(NO_ERROR
);
60 len
= FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ALLOCATE_BUFFER
,
61 fmt
, 0, 0, (LPWSTR
)&str
, 0, &va_args
);
63 if (len
== 0 && GetLastError() != NO_ERROR
)
65 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt
));
69 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), str
, len
, &nOut
, NULL
);
71 /* WriteConsole fails if its output is redirected to a file.
72 * If this occurs, we should use an OEM codepage and call WriteFile.
79 lenA
= WideCharToMultiByte(GetConsoleOutputCP(), 0, str
, len
, NULL
, 0, NULL
, NULL
);
80 strA
= HeapAlloc(GetProcessHeap(), 0, lenA
);
83 WideCharToMultiByte(GetConsoleOutputCP(), 0, str
, len
, strA
, lenA
, NULL
, NULL
);
84 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), strA
, lenA
, &nOut
, FALSE
);
85 HeapFree(GetProcessHeap(), 0, strA
);
95 * strDll - name of the dll.
96 * procName - name of the procedure to load from the dll.
97 * DllHandle - a variable that receives the handle of the loaded dll.
99 static VOID
*LoadProc(const WCHAR
* strDll
, const char* procName
, HMODULE
* DllHandle
)
103 *DllHandle
= LoadLibraryExW(strDll
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
);
106 output_write(STRING_DLL_LOAD_FAILED
, strDll
);
107 ExitProcess(LOADLIBRARY_FAILED
);
109 proc
= (VOID
*) GetProcAddress(*DllHandle
, procName
);
112 output_write(STRING_PROC_NOT_IMPLEMENTED
, procName
, strDll
);
113 FreeLibrary(*DllHandle
);
119 static int RegisterDll(const WCHAR
* strDll
)
122 DLLREGISTER pfRegister
;
123 HMODULE DllHandle
= NULL
;
125 pfRegister
= LoadProc(strDll
, "DllRegisterServer", &DllHandle
);
127 return GETPROCADDRESS_FAILED
;
132 output_write(STRING_REGISTER_FAILED
, strDll
);
133 return DLLSERVER_FAILED
;
135 output_write(STRING_REGISTER_SUCCESSFUL
, strDll
);
138 FreeLibrary(DllHandle
);
142 static int UnregisterDll(const WCHAR
* strDll
)
145 DLLUNREGISTER pfUnregister
;
146 HMODULE DllHandle
= NULL
;
148 pfUnregister
= LoadProc(strDll
, "DllUnregisterServer", &DllHandle
);
150 return GETPROCADDRESS_FAILED
;
155 output_write(STRING_UNREGISTER_FAILED
, strDll
);
156 return DLLSERVER_FAILED
;
158 output_write(STRING_UNREGISTER_SUCCESSFUL
, strDll
);
161 FreeLibrary(DllHandle
);
165 static int InstallDll(BOOL install
, const WCHAR
*strDll
, const WCHAR
*command_line
)
168 DLLINSTALL pfInstall
;
169 HMODULE DllHandle
= NULL
;
171 pfInstall
= LoadProc(strDll
, "DllInstall", &DllHandle
);
173 return GETPROCADDRESS_FAILED
;
175 hr
= pfInstall(install
, command_line
);
179 output_write(STRING_INSTALL_FAILED
, strDll
);
181 output_write(STRING_UNINSTALL_FAILED
, strDll
);
182 return DLLSERVER_FAILED
;
185 output_write(STRING_INSTALL_SUCCESSFUL
, strDll
);
187 output_write(STRING_UNINSTALL_SUCCESSFUL
, strDll
);
190 FreeLibrary(DllHandle
);
194 static WCHAR
*parse_command_line(WCHAR
*command_line
)
196 if (command_line
[0] == ':' && command_line
[1])
198 int len
= strlenW(command_line
);
202 /* remove double quotes */
203 if (command_line
[0] == '"')
210 command_line
[len
] = 0;
219 int wmain(int argc
, WCHAR
* argv
[])
222 BOOL CallRegister
= TRUE
;
223 BOOL CallInstall
= FALSE
;
224 BOOL Unregister
= FALSE
;
225 BOOL DllFound
= FALSE
;
226 WCHAR
* wsCommandLine
= NULL
;
227 WCHAR EmptyLine
[1] = {0};
231 /* We mirror the Microsoft version by processing all of the flags before
232 * the files (e.g. regsvr32 file1 /s file2 is silent even for file1).
234 * Note the complication that this version may be passed Unix format filenames
235 * which could be mistaken for flags. The Windows version conveniently
236 * requires each flag to be separate (e.g. no /su), so we will simply
237 * assume that anything longer than /. is a filename.
239 for(i
= 1; i
< argc
; i
++)
241 if (argv
[i
][0] == '/' || argv
[i
][0] == '-')
246 if (argv
[i
][2] && argv
[i
][2] != ':')
249 switch (tolowerW(argv
[i
][1]))
259 wsCommandLine
= parse_command_line(argv
[i
] + 2); /* argv[i] + strlen("/i") */
261 wsCommandLine
= EmptyLine
;
264 CallRegister
= FALSE
;
267 /* console output */;
270 output_write(STRING_UNRECOGNIZED_SWITCH
, argv
[i
]);
271 output_write(STRING_USAGE
);
278 if (!CallInstall
&& !CallRegister
) /* flags: /n or /u /n */
281 for (i
= 1; i
< argc
; i
++)
285 WCHAR
*DllName
= argv
[i
];
289 if (CallInstall
&& Unregister
)
290 res
= InstallDll(!Unregister
, DllName
, wsCommandLine
);
292 /* The Windows version stops processing the current file on the first error. */
299 if (!CallInstall
|| CallRegister
)
302 res
= UnregisterDll(DllName
);
304 res
= RegisterDll(DllName
);
313 if (CallInstall
&& !Unregister
)
314 res
= InstallDll(!Unregister
, DllName
, wsCommandLine
);
326 output_write(STRING_HEADER
);
327 output_write(STRING_USAGE
);
333 /* return the most recent error code, even if later DLLs succeed */