psapi: Implement semi-stub for K32EnumProcessModulesEx.
[wine.git] / programs / regsvr32 / regsvr32.c
blob624acd697c67c93025e118f5696f6371ebb55832
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 (Silent) return;
77 if (!LoadStringA(GetModuleHandleA(NULL), id, fmt, sizeof(fmt)/sizeof(fmt[0])))
79 WINE_FIXME("LoadString failed with %d\n", GetLastError());
80 return;
83 __ms_va_start(va_args, id);
84 SetLastError(NO_ERROR);
85 len = FormatMessageA(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
86 fmt, 0, 0, (LPSTR)&str, 0, &va_args);
87 __ms_va_end(va_args);
88 if (len == 0 && GetLastError() != NO_ERROR)
90 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_a(fmt));
91 return;
94 ret = WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &nOut, NULL);
96 if (!ret)
97 WINE_WARN("regsvr32: WriteConsoleA() failed.\n");
99 LocalFree(str);
103 * Loads procedure.
105 * Parameters:
106 * strDll - name of the dll.
107 * procName - name of the procedure to load from dll
108 * pDllHanlde - output variable receives handle of the loaded dll.
110 static VOID *LoadProc(const char* strDll, const char* procName, HMODULE* DllHandle)
112 VOID* (*proc)(void);
114 *DllHandle = LoadLibraryExA(strDll, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
115 if(!*DllHandle)
117 output_write(STRING_DLL_LOAD_FAILED, strDll);
118 ExitProcess(1);
120 proc = (VOID *) GetProcAddress(*DllHandle, procName);
121 if(!proc)
123 output_write(STRING_PROC_NOT_IMPLEMENTED, procName, strDll);
124 FreeLibrary(*DllHandle);
125 return NULL;
127 return proc;
130 static int RegisterDll(const char* strDll)
132 HRESULT hr;
133 DLLREGISTER pfRegister;
134 HMODULE DllHandle = NULL;
136 pfRegister = LoadProc(strDll, "DllRegisterServer", &DllHandle);
137 if (!pfRegister)
138 return 0;
140 hr = pfRegister();
141 if(FAILED(hr))
143 output_write(STRING_REGISTER_FAILED, strDll);
144 return -1;
146 output_write(STRING_REGISTER_SUCCESSFUL, strDll);
148 if(DllHandle)
149 FreeLibrary(DllHandle);
150 return 0;
153 static int UnregisterDll(char* strDll)
155 HRESULT hr;
156 DLLUNREGISTER pfUnregister;
157 HMODULE DllHandle = NULL;
159 pfUnregister = LoadProc(strDll, "DllUnregisterServer", &DllHandle);
160 if (!pfUnregister)
161 return 0;
163 hr = pfUnregister();
164 if(FAILED(hr))
166 output_write(STRING_UNREGISTER_FAILED, strDll);
167 return -1;
169 output_write(STRING_UNREGISTER_SUCCESSFUL, strDll);
171 if(DllHandle)
172 FreeLibrary(DllHandle);
173 return 0;
176 static int InstallDll(BOOL install, char *strDll, WCHAR *command_line)
178 HRESULT hr;
179 DLLINSTALL pfInstall;
180 HMODULE DllHandle = NULL;
182 pfInstall = LoadProc(strDll, "DllInstall", &DllHandle);
183 if (!pfInstall)
184 return 0;
186 hr = pfInstall(install, command_line);
187 if(FAILED(hr))
189 if (install)
190 output_write(STRING_INSTALL_FAILED, strDll);
191 else
192 output_write(STRING_UNINSTALL_FAILED, strDll);
193 return -1;
195 if (install)
196 output_write(STRING_INSTALL_SUCCESSFUL, strDll);
197 else
198 output_write(STRING_UNINSTALL_SUCCESSFUL, strDll);
200 if(DllHandle)
201 FreeLibrary(DllHandle);
202 return 0;
205 int main(int argc, char* argv[])
207 int i;
208 BOOL CallRegister = TRUE;
209 BOOL CallInstall = FALSE;
210 BOOL Unregister = FALSE;
211 BOOL DllFound = FALSE;
212 WCHAR* wsCommandLine = NULL;
213 WCHAR EmptyLine[1] = {0};
215 OleInitialize(NULL);
217 /* Strictly, the Microsoft version processes all the flags before
218 * the files (e.g. regsvr32 file1 /s file2 is silent even for file1).
219 * For ease, we will not replicate that and will process the arguments
220 * in order.
222 for(i = 1; i < argc; i++)
224 if ((!strcasecmp(argv[i], "/u")) ||(!strcasecmp(argv[i], "-u")))
225 Unregister = TRUE;
226 else if ((!strcasecmp(argv[i], "/s"))||(!strcasecmp(argv[i], "-s")))
227 Silent = TRUE;
228 else if ((!strncasecmp(argv[i], "/i", strlen("/i")))||(!strncasecmp(argv[i], "-i", strlen("-i"))))
230 CHAR* command_line = argv[i] + strlen("/i");
232 CallInstall = TRUE;
233 if (command_line[0] == ':' && command_line[1])
235 int len = strlen(command_line);
237 command_line++;
238 len--;
239 /* remove double quotes */
240 if (command_line[0] == '"')
242 command_line++;
243 len--;
244 if (command_line[0])
246 len--;
247 command_line[len] = 0;
250 if (command_line[0])
252 len = MultiByteToWideChar(CP_ACP, 0, command_line, -1,
253 NULL, 0);
254 wsCommandLine = HeapAlloc(GetProcessHeap(), 0,
255 len * sizeof(WCHAR));
256 if (wsCommandLine)
257 MultiByteToWideChar(CP_ACP, 0, command_line, -1,
258 wsCommandLine, len);
260 else
262 wsCommandLine = EmptyLine;
265 else
267 wsCommandLine = EmptyLine;
270 else if((!strcasecmp(argv[i], "/n"))||(!strcasecmp(argv[i], "-n")))
271 CallRegister = FALSE;
272 else if((!strcasecmp(argv[i], "/c"))||(!strcasecmp(argv[i], "-c")))
273 /* console output */;
274 else if (argv[i][0] == '/' && (!argv[i][2] || argv[i][2] == ':'))
276 output_write(STRING_UNRECOGNIZED_SWITCH, argv[i]);
277 output_write(STRING_USAGE);
278 return 1;
280 else
282 char *DllName = argv[i];
283 int res = 0;
285 DllFound = TRUE;
286 if (!CallInstall || (CallInstall && CallRegister))
288 if(Unregister)
289 res = UnregisterDll(DllName);
290 else
291 res = RegisterDll(DllName);
294 if (res)
295 return res;
296 /* Confirmed. The windows version does stop on the first error.*/
298 if (CallInstall)
300 res = InstallDll(!Unregister, DllName, wsCommandLine);
303 if (res)
304 return res;
308 if (!DllFound)
310 output_write(STRING_HEADER);
311 output_write(STRING_USAGE);
312 return 1;
315 OleUninitialize();
317 return 0;