Janitorial: C booleans must not be compared against TRUE.
[wine/multimedia.git] / programs / regsvr32 / regsvr32.c
blob83859a53566d5e08c5ffccc3842c4a91f55f4ca4
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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]
35 * Note the complication that this version may be passed unix format file names
36 * which might be mistaken for flags. Conveniently the Windows version
37 * requires each flag to be separate (e.g. no /su ) and so we will simply
38 * assume that anything longer than /. is a filename.
41 /**
42 * FIXME - currently receives command-line parameters in ASCII only and later
43 * converts to Unicode. Ideally the function should have wWinMain entry point
44 * and then work in Unicode only, but it seems Wine does not have necessary
45 * support.
48 #include "config.h"
49 #include "wine/port.h"
51 #include <stdio.h>
52 #include <string.h>
53 #include <windows.h>
55 typedef HRESULT (*DLLREGISTER) (void);
56 typedef HRESULT (*DLLUNREGISTER) (void);
57 typedef HRESULT (*DLLINSTALL) (BOOL,LPCWSTR);
59 int Silent = 0;
61 int Usage()
63 printf("regsvr32 [/u] [/s] [/n] [/i[:cmdline]] dllname ...\n");
64 printf("\t[/u] unregister server\n");
65 printf("\t[/s] silent (no message boxes)\n");
66 printf("\t[/i] Call DllInstall passing it an optional [cmdline];\n");
67 printf("\t when used with /u calls dll uninstall\n");
68 printf("\t[/n] Do not call DllRegisterServer; this option "
69 "must be used with [/i]\n");
70 return 0;
73 /**
74 * Loads procedure.
76 * Parameters:
77 * strDll - name of the dll.
78 * procName - name of the procedure to load from dll
79 * pDllHanlde - output variable receives handle of the loaded dll.
81 VOID *LoadProc(char* strDll, char* procName, HMODULE* DllHandle)
83 VOID* (*proc)(void);
85 *DllHandle = LoadLibraryEx(strDll, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
86 if(!*DllHandle)
88 if(!Silent)
89 printf("Dll %s not found\n", strDll);
91 exit(-1);
93 proc = (VOID *) GetProcAddress(*DllHandle, procName);
94 if(!proc)
96 if(!Silent)
97 printf("%s not implemented in dll %s\n", procName, strDll);
98 FreeLibrary(*DllHandle);
99 exit(-1);
101 return proc;
104 int RegisterDll(char* strDll)
106 HRESULT hr;
107 DLLREGISTER pfRegister;
108 HMODULE DllHandle = NULL;
110 pfRegister = LoadProc(strDll, "DllRegisterServer", &DllHandle);
112 hr = pfRegister();
113 if(FAILED(hr))
115 if(!Silent)
116 printf("Failed to register dll %s\n", strDll);
118 return -1;
120 if(!Silent)
121 printf("Successfully registered dll %s\n", strDll);
123 if(DllHandle)
124 FreeLibrary(DllHandle);
125 return 0;
128 int UnregisterDll(char* strDll)
130 HRESULT hr;
131 DLLUNREGISTER pfUnregister;
132 HMODULE DllHandle = NULL;
134 pfUnregister = LoadProc(strDll, "DllUnregisterServer", &DllHandle);
135 hr = pfUnregister();
136 if(FAILED(hr))
138 if(!Silent)
139 printf("Failed to unregister dll %s\n", strDll);
141 return -1;
143 if(!Silent)
144 printf("Successfully unregistered dll %s\n", strDll);
146 if(DllHandle)
147 FreeLibrary(DllHandle);
148 return 0;
151 int InstallDll(BOOL install, char *strDll, WCHAR *command_line)
153 HRESULT hr;
154 DLLINSTALL pfInstall;
155 HMODULE DllHandle = NULL;
157 pfInstall = LoadProc(strDll, "DllInstall", &DllHandle);
158 hr = pfInstall(install, command_line);
159 if(FAILED(hr))
161 if(!Silent)
162 printf("Failed to %s dll %s\n", install ? "install" : "uninstall",
163 strDll);
164 return -1;
166 if(!Silent)
167 printf("Successfully %s dll %s\n", install ? "installed" : "uninstalled",
168 strDll);
170 if(DllHandle)
171 FreeLibrary(DllHandle);
172 return 0;
175 int main(int argc, char* argv[])
177 int i;
178 BOOL CallRegister = TRUE;
179 BOOL CallInstall = FALSE;
180 BOOL Unregister = FALSE;
181 BOOL DllFound = FALSE;
182 WCHAR* wsCommandLine = NULL;
183 WCHAR EmptyLine[1] = {0};
186 /* Strictly, the Microsoft version processes all the flags before
187 * the files (e.g. regsvr32 file1 /s file2 is silent even for file1.
188 * For ease, we will not replicate that and will process the arguments
189 * in order.
191 for(i = 1; i < argc; i++)
193 if (!strcasecmp(argv[i], "/u"))
194 Unregister = TRUE;
195 else if (!strcasecmp(argv[i], "/s"))
196 Silent = 1;
197 else if (!strncasecmp(argv[i], "/i", strlen("/i")))
199 CHAR* command_line = argv[i] + strlen("/i");
201 CallInstall = TRUE;
202 if (command_line[0] == ':' && command_line[1])
204 int len = strlen(command_line);
206 command_line++;
207 len--;
208 /* remove double quotes */
209 if (command_line[0] == '"')
211 command_line++;
212 len--;
213 if (command_line[0])
215 len--;
216 command_line[len] = 0;
219 if (command_line[0])
221 len = MultiByteToWideChar(CP_ACP, 0, command_line, -1,
222 NULL, 0);
223 wsCommandLine = HeapAlloc(GetProcessHeap(), 0,
224 len * sizeof(WCHAR));
225 if (wsCommandLine)
226 MultiByteToWideChar(CP_ACP, 0, command_line, -1,
227 wsCommandLine, len);
229 else
231 wsCommandLine = EmptyLine;
234 else
236 wsCommandLine = EmptyLine;
239 else if(!strcasecmp(argv[i], "/n"))
240 CallRegister = FALSE;
241 else if (argv[i][0] == '/' && (!argv[i][2] || argv[i][2] == ':'))
242 printf("Unrecognized switch %s\n", argv[i]);
243 else
245 char *DllName = argv[i];
246 int res = 0;
248 DllFound = TRUE;
249 if (!CallInstall || (CallInstall && CallRegister))
251 if(Unregister)
252 res = UnregisterDll(DllName);
253 else
254 res = RegisterDll(DllName);
257 if (res)
258 return res;
259 /* Confirmed. The windows version does stop on the first error.*/
261 if (CallInstall)
263 res = InstallDll(!Unregister, DllName, wsCommandLine);
266 if (res)
267 return res;
271 if (!DllFound)
273 if(!Silent)
274 return Usage();
275 else
276 return -1;
279 return 0;