regsvr32: Replace printf calls with WriteConsole function.
[wine.git] / programs / regsvr32 / regsvr32.c
blobc795f427d96f847afc4327e082ec31f6ff3c9e9d
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
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * This version deliberately differs in error handling compared to the
23 * windows version.
28 * regsvr32 [/u] [/s] [/n] [/i[:cmdline]] dllname ...
29 * [/u] unregister server
30 * [/s] silent (no message boxes)
31 * [/i] Call DllInstall passing it an optional [cmdline];
32 * when used with /u calls dll uninstall.
33 * [/n] Do not call DllRegisterServer; this option must be used with [/i]
34 * [/c] Console output (seems to be deprecated and ignored)
36 * Note the complication that this version may be passed unix format file names
37 * which might be mistaken for flags. Conveniently the Windows version
38 * requires each flag to be separate (e.g. no /su ) and so we will simply
39 * assume that anything longer than /. is a filename.
42 /**
43 * FIXME - currently receives command-line parameters in ASCII only and later
44 * converts to Unicode. Ideally the function should have wWinMain entry point
45 * and then work in Unicode only, but it seems Wine does not have necessary
46 * support.
49 #define WIN32_LEAN_AND_MEAN
51 #include "config.h"
52 #include "wine/port.h"
54 #include <string.h>
55 #include <windows.h>
56 #include <ole2.h>
57 #include "regsvr32.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(regsvr32);
62 typedef HRESULT (*DLLREGISTER) (void);
63 typedef HRESULT (*DLLUNREGISTER) (void);
64 typedef HRESULT (*DLLINSTALL) (BOOL,LPCWSTR);
66 static BOOL Silent = FALSE;
68 static void __cdecl output_write(UINT id, ...)
70 char fmt[1024];
71 __ms_va_list va_args;
72 char *str;
73 DWORD len, nOut, ret;
75 if (!LoadStringA(GetModuleHandleA(NULL), id, fmt, sizeof(fmt)/sizeof(fmt[0])))
77 WINE_FIXME("LoadString failed with %d\n", GetLastError());
78 return;
81 __ms_va_start(va_args, id);
82 SetLastError(NO_ERROR);
83 len = FormatMessageA(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
84 fmt, 0, 0, (LPSTR)&str, 0, &va_args);
85 __ms_va_end(va_args);
86 if (len == 0 && GetLastError() != NO_ERROR)
88 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_a(fmt));
89 return;
92 ret = WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &nOut, NULL);
94 if (!ret)
95 WINE_WARN("regsvr32: WriteConsoleA() failed.\n");
97 LocalFree(str);
101 * Loads procedure.
103 * Parameters:
104 * strDll - name of the dll.
105 * procName - name of the procedure to load from dll
106 * pDllHanlde - output variable receives handle of the loaded dll.
108 static VOID *LoadProc(const char* strDll, const char* procName, HMODULE* DllHandle)
110 VOID* (*proc)(void);
112 *DllHandle = LoadLibraryExA(strDll, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
113 if(!*DllHandle)
115 if(!Silent)
116 output_write(STRING_DLL_LOAD_FAILED, strDll);
118 ExitProcess(1);
120 proc = (VOID *) GetProcAddress(*DllHandle, procName);
121 if(!proc)
123 if(!Silent)
124 output_write(STRING_PROC_NOT_IMPLEMENTED, procName, strDll);
125 FreeLibrary(*DllHandle);
126 return NULL;
128 return proc;
131 static int RegisterDll(const char* strDll)
133 HRESULT hr;
134 DLLREGISTER pfRegister;
135 HMODULE DllHandle = NULL;
137 pfRegister = LoadProc(strDll, "DllRegisterServer", &DllHandle);
138 if (!pfRegister)
139 return 0;
141 hr = pfRegister();
142 if(FAILED(hr))
144 if(!Silent)
145 output_write(STRING_REGISTER_FAILED, strDll);
147 return -1;
149 if(!Silent)
150 output_write(STRING_REGISTER_SUCCESSFUL, strDll);
152 if(DllHandle)
153 FreeLibrary(DllHandle);
154 return 0;
157 static int UnregisterDll(char* strDll)
159 HRESULT hr;
160 DLLUNREGISTER pfUnregister;
161 HMODULE DllHandle = NULL;
163 pfUnregister = LoadProc(strDll, "DllUnregisterServer", &DllHandle);
164 if (!pfUnregister)
165 return 0;
167 hr = pfUnregister();
168 if(FAILED(hr))
170 if(!Silent)
171 output_write(STRING_UNREGISTER_FAILED, strDll);
173 return -1;
175 if(!Silent)
176 output_write(STRING_UNREGISTER_SUCCESSFUL, strDll);
178 if(DllHandle)
179 FreeLibrary(DllHandle);
180 return 0;
183 static int InstallDll(BOOL install, char *strDll, WCHAR *command_line)
185 HRESULT hr;
186 DLLINSTALL pfInstall;
187 HMODULE DllHandle = NULL;
189 pfInstall = LoadProc(strDll, "DllInstall", &DllHandle);
190 if (!pfInstall)
191 return 0;
193 hr = pfInstall(install, command_line);
194 if(FAILED(hr))
196 if(!Silent)
198 if (install)
199 output_write(STRING_INSTALL_FAILED, strDll);
200 else
201 output_write(STRING_UNINSTALL_FAILED, strDll);
203 return -1;
205 if(!Silent)
207 if (install)
208 output_write(STRING_INSTALL_SUCCESSFUL, strDll);
209 else
210 output_write(STRING_UNINSTALL_SUCCESSFUL, strDll);
213 if(DllHandle)
214 FreeLibrary(DllHandle);
215 return 0;
218 int main(int argc, char* argv[])
220 int i;
221 BOOL CallRegister = TRUE;
222 BOOL CallInstall = FALSE;
223 BOOL Unregister = FALSE;
224 BOOL DllFound = FALSE;
225 WCHAR* wsCommandLine = NULL;
226 WCHAR EmptyLine[1] = {0};
228 OleInitialize(NULL);
230 /* Strictly, the Microsoft version processes all the flags before
231 * the files (e.g. regsvr32 file1 /s file2 is silent even for file1).
232 * For ease, we will not replicate that and will process the arguments
233 * in order.
235 for(i = 1; i < argc; i++)
237 if ((!strcasecmp(argv[i], "/u")) ||(!strcasecmp(argv[i], "-u")))
238 Unregister = TRUE;
239 else if ((!strcasecmp(argv[i], "/s"))||(!strcasecmp(argv[i], "-s")))
240 Silent = TRUE;
241 else if ((!strncasecmp(argv[i], "/i", strlen("/i")))||(!strncasecmp(argv[i], "-i", strlen("-i"))))
243 CHAR* command_line = argv[i] + strlen("/i");
245 CallInstall = TRUE;
246 if (command_line[0] == ':' && command_line[1])
248 int len = strlen(command_line);
250 command_line++;
251 len--;
252 /* remove double quotes */
253 if (command_line[0] == '"')
255 command_line++;
256 len--;
257 if (command_line[0])
259 len--;
260 command_line[len] = 0;
263 if (command_line[0])
265 len = MultiByteToWideChar(CP_ACP, 0, command_line, -1,
266 NULL, 0);
267 wsCommandLine = HeapAlloc(GetProcessHeap(), 0,
268 len * sizeof(WCHAR));
269 if (wsCommandLine)
270 MultiByteToWideChar(CP_ACP, 0, command_line, -1,
271 wsCommandLine, len);
273 else
275 wsCommandLine = EmptyLine;
278 else
280 wsCommandLine = EmptyLine;
283 else if((!strcasecmp(argv[i], "/n"))||(!strcasecmp(argv[i], "-n")))
284 CallRegister = FALSE;
285 else if((!strcasecmp(argv[i], "/c"))||(!strcasecmp(argv[i], "-c")))
286 /* console output */;
287 else if (argv[i][0] == '/' && (!argv[i][2] || argv[i][2] == ':'))
289 output_write(STRING_UNRECOGNIZED_SWITCH, argv[i]);
290 output_write(STRING_USAGE);
291 return 1;
293 else
295 char *DllName = argv[i];
296 int res = 0;
298 DllFound = TRUE;
299 if (!CallInstall || (CallInstall && CallRegister))
301 if(Unregister)
302 res = UnregisterDll(DllName);
303 else
304 res = RegisterDll(DllName);
307 if (res)
308 return res;
309 /* Confirmed. The windows version does stop on the first error.*/
311 if (CallInstall)
313 res = InstallDll(!Unregister, DllName, wsCommandLine);
316 if (res)
317 return res;
321 if (!DllFound)
323 if(!Silent)
325 output_write(STRING_HEADER);
326 output_write(STRING_USAGE);
328 return 1;
331 OleUninitialize();
333 return 0;