user32/tests: Set last error if the menu item cannot be found.
[wine/multimedia.git] / programs / regsvr32 / regsvr32.c
blob75cc9c994505779e04a7e4ddc3157a9503dd52e7
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 <stdio.h>
55 #include <string.h>
56 #include <windows.h>
57 #include <ole2.h>
59 typedef HRESULT (*DLLREGISTER) (void);
60 typedef HRESULT (*DLLUNREGISTER) (void);
61 typedef HRESULT (*DLLINSTALL) (BOOL,LPCWSTR);
63 int Silent = 0;
65 static int Usage(void)
67 printf("regsvr32 [/u] [/s] [/n] [/i[:cmdline]] dllname ...\n");
68 printf("\t[/u] unregister server\n");
69 printf("\t[/s] silent (no message boxes)\n");
70 printf("\t[/i] Call DllInstall passing it an optional [cmdline];\n");
71 printf("\t when used with /u calls dll uninstall\n");
72 printf("\t[/n] Do not call DllRegisterServer; this option "
73 "must be used with [/i]\n");
74 return 0;
77 /**
78 * Loads procedure.
80 * Parameters:
81 * strDll - name of the dll.
82 * procName - name of the procedure to load from dll
83 * pDllHanlde - output variable receives handle of the loaded dll.
85 static VOID *LoadProc(const char* strDll, const char* procName, HMODULE* DllHandle)
87 VOID* (*proc)(void);
89 *DllHandle = LoadLibraryExA(strDll, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
90 if(!*DllHandle)
92 if(!Silent)
93 printf("Failed to load DLL %s\n", strDll);
95 ExitProcess(1);
97 proc = (VOID *) GetProcAddress(*DllHandle, procName);
98 if(!proc)
100 if(!Silent)
101 printf("%s not implemented in DLL %s\n", procName, strDll);
102 FreeLibrary(*DllHandle);
103 ExitProcess(1);
105 return proc;
108 static int RegisterDll(const char* strDll)
110 HRESULT hr;
111 DLLREGISTER pfRegister;
112 HMODULE DllHandle = NULL;
114 pfRegister = LoadProc(strDll, "DllRegisterServer", &DllHandle);
116 hr = pfRegister();
117 if(FAILED(hr))
119 if(!Silent)
120 printf("Failed to register DLL %s\n", strDll);
122 return -1;
124 if(!Silent)
125 printf("Successfully registered DLL %s\n", strDll);
127 if(DllHandle)
128 FreeLibrary(DllHandle);
129 return 0;
132 static int UnregisterDll(char* strDll)
134 HRESULT hr;
135 DLLUNREGISTER pfUnregister;
136 HMODULE DllHandle = NULL;
138 pfUnregister = LoadProc(strDll, "DllUnregisterServer", &DllHandle);
139 hr = pfUnregister();
140 if(FAILED(hr))
142 if(!Silent)
143 printf("Failed to unregister DLL %s\n", strDll);
145 return -1;
147 if(!Silent)
148 printf("Successfully unregistered DLL %s\n", strDll);
150 if(DllHandle)
151 FreeLibrary(DllHandle);
152 return 0;
155 static int InstallDll(BOOL install, char *strDll, WCHAR *command_line)
157 HRESULT hr;
158 DLLINSTALL pfInstall;
159 HMODULE DllHandle = NULL;
161 pfInstall = LoadProc(strDll, "DllInstall", &DllHandle);
162 hr = pfInstall(install, command_line);
163 if(FAILED(hr))
165 if(!Silent)
166 printf("Failed to %s DLL %s\n", install ? "install" : "uninstall",
167 strDll);
168 return -1;
170 if(!Silent)
171 printf("Successfully %s DLL %s\n", install ? "installed" : "uninstalled",
172 strDll);
174 if(DllHandle)
175 FreeLibrary(DllHandle);
176 return 0;
179 int main(int argc, char* argv[])
181 int i;
182 BOOL CallRegister = TRUE;
183 BOOL CallInstall = FALSE;
184 BOOL Unregister = FALSE;
185 BOOL DllFound = FALSE;
186 WCHAR* wsCommandLine = NULL;
187 WCHAR EmptyLine[1] = {0};
189 OleInitialize(NULL);
191 /* Strictly, the Microsoft version processes all the flags before
192 * the files (e.g. regsvr32 file1 /s file2 is silent even for file1.
193 * For ease, we will not replicate that and will process the arguments
194 * in order.
196 for(i = 1; i < argc; i++)
198 if ((!strcasecmp(argv[i], "/u")) ||(!strcasecmp(argv[i], "-u")))
199 Unregister = TRUE;
200 else if ((!strcasecmp(argv[i], "/s"))||(!strcasecmp(argv[i], "-s")))
201 Silent = 1;
202 else if ((!strncasecmp(argv[i], "/i", strlen("/i")))||(!strncasecmp(argv[i], "-i", strlen("-i"))))
204 CHAR* command_line = argv[i] + strlen("/i");
206 CallInstall = TRUE;
207 if (command_line[0] == ':' && command_line[1])
209 int len = strlen(command_line);
211 command_line++;
212 len--;
213 /* remove double quotes */
214 if (command_line[0] == '"')
216 command_line++;
217 len--;
218 if (command_line[0])
220 len--;
221 command_line[len] = 0;
224 if (command_line[0])
226 len = MultiByteToWideChar(CP_ACP, 0, command_line, -1,
227 NULL, 0);
228 wsCommandLine = HeapAlloc(GetProcessHeap(), 0,
229 len * sizeof(WCHAR));
230 if (wsCommandLine)
231 MultiByteToWideChar(CP_ACP, 0, command_line, -1,
232 wsCommandLine, len);
234 else
236 wsCommandLine = EmptyLine;
239 else
241 wsCommandLine = EmptyLine;
244 else if((!strcasecmp(argv[i], "/n"))||(!strcasecmp(argv[i], "-n")))
245 CallRegister = FALSE;
246 else if((!strcasecmp(argv[i], "/c"))||(!strcasecmp(argv[i], "-c")))
247 /* console output */;
248 else if (argv[i][0] == '/' && (!argv[i][2] || argv[i][2] == ':'))
249 printf("Unrecognized switch %s\n", argv[i]);
250 else
252 char *DllName = argv[i];
253 int res = 0;
255 DllFound = TRUE;
256 if (!CallInstall || (CallInstall && CallRegister))
258 if(Unregister)
259 res = UnregisterDll(DllName);
260 else
261 res = RegisterDll(DllName);
264 if (res)
265 return res;
266 /* Confirmed. The windows version does stop on the first error.*/
268 if (CallInstall)
270 res = InstallDll(!Unregister, DllName, wsCommandLine);
273 if (res)
274 return res;
278 if (!DllFound)
280 if(!Silent)
281 return Usage();
282 else
283 return -1;
286 OleUninitialize();
288 return 0;