Release 1.5.29.
[wine] / programs / regsvr32 / regsvr32.c
1 /*
2  * PURPOSE: Register OLE components in the registry
3  *
4  * Copyright 2001 ReactOS project
5  * Copyright 2001 Jurgen Van Gael [jurgen.vangael@student.kuleuven.ac.be]
6  * Copyright 2002 Andriy Palamarchuk
7  *
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.
12  *
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.
17  *
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
21  *
22  * This version deliberately differs in error handling compared to the
23  * windows version.
24  */
25
26 /*
27  *
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)
35  *
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.
40  */
41
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.
47  */
48
49 #define WIN32_LEAN_AND_MEAN
50
51 #include "config.h"
52 #include "wine/port.h"
53
54 #include <stdio.h>
55 #include <string.h>
56 #include <windows.h>
57 #include <ole2.h>
58
59 typedef HRESULT (*DLLREGISTER)          (void);
60 typedef HRESULT (*DLLUNREGISTER)        (void);
61 typedef HRESULT (*DLLINSTALL)           (BOOL,LPCWSTR);
62
63 int Silent = 0;
64
65 static int Usage(void)
66 {
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;
75 }
76
77 /**
78  * Loads procedure.
79  *
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.
84  */
85 static VOID *LoadProc(const char* strDll, const char* procName, HMODULE* DllHandle)
86 {
87     VOID* (*proc)(void);
88
89     *DllHandle = LoadLibraryExA(strDll, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
90     if(!*DllHandle)
91     {
92         if(!Silent)
93             printf("Failed to load DLL %s\n", strDll);
94
95         ExitProcess(1);
96     }
97     proc = (VOID *) GetProcAddress(*DllHandle, procName);
98     if(!proc)
99     {
100         if(!Silent)
101             printf("%s not implemented in DLL %s\n", procName, strDll);
102         FreeLibrary(*DllHandle);
103         return NULL;
104     }
105     return proc;
106 }
107
108 static int RegisterDll(const char* strDll)
109 {
110     HRESULT hr;
111     DLLREGISTER pfRegister;
112     HMODULE DllHandle = NULL;
113
114     pfRegister = LoadProc(strDll, "DllRegisterServer", &DllHandle);
115     if (!pfRegister)
116         return 0;
117
118     hr = pfRegister();
119     if(FAILED(hr))
120     {
121         if(!Silent)
122             printf("Failed to register DLL %s\n", strDll);
123
124         return -1;
125     }
126     if(!Silent)
127         printf("Successfully registered DLL %s\n", strDll);
128
129     if(DllHandle)
130         FreeLibrary(DllHandle);
131     return 0;
132 }
133
134 static int UnregisterDll(char* strDll)
135 {
136     HRESULT hr;
137     DLLUNREGISTER pfUnregister;
138     HMODULE DllHandle = NULL;
139
140     pfUnregister = LoadProc(strDll, "DllUnregisterServer", &DllHandle);
141     if (!pfUnregister)
142         return 0;
143
144     hr = pfUnregister();
145     if(FAILED(hr))
146     {
147         if(!Silent)
148             printf("Failed to unregister DLL %s\n", strDll);
149
150         return -1;
151     }
152     if(!Silent)
153         printf("Successfully unregistered DLL %s\n", strDll);
154
155     if(DllHandle)
156         FreeLibrary(DllHandle);
157     return 0;
158 }
159
160 static int InstallDll(BOOL install, char *strDll, WCHAR *command_line)
161 {
162     HRESULT hr;
163     DLLINSTALL pfInstall;
164     HMODULE DllHandle = NULL;
165
166     pfInstall = LoadProc(strDll, "DllInstall", &DllHandle);
167     if (!pfInstall)
168         return 0;
169
170     hr = pfInstall(install, command_line);
171     if(FAILED(hr))
172     {
173         if(!Silent)
174             printf("Failed to %s DLL %s\n", install ? "install" : "uninstall",
175                    strDll);
176         return -1;
177     }
178     if(!Silent)
179         printf("Successfully %s DLL %s\n",  install ? "installed" : "uninstalled",
180                strDll);
181
182     if(DllHandle)
183         FreeLibrary(DllHandle);
184     return 0;
185 }
186
187 int main(int argc, char* argv[])
188 {
189     int             i;
190     BOOL            CallRegister = TRUE;
191     BOOL            CallInstall = FALSE;
192     BOOL            Unregister = FALSE;
193     BOOL            DllFound = FALSE;
194     WCHAR*          wsCommandLine = NULL;
195     WCHAR           EmptyLine[1] = {0};
196
197     OleInitialize(NULL);
198
199     /* Strictly, the Microsoft version processes all the flags before
200      * the files (e.g. regsvr32 file1 /s file2 is silent even for file1.
201      * For ease, we will not replicate that and will process the arguments
202      * in order.
203      */
204     for(i = 1; i < argc; i++)
205     {
206         if ((!strcasecmp(argv[i], "/u")) ||(!strcasecmp(argv[i], "-u")))
207                 Unregister = TRUE;
208         else if ((!strcasecmp(argv[i], "/s"))||(!strcasecmp(argv[i], "-s")))
209                 Silent = 1;
210         else if ((!strncasecmp(argv[i], "/i", strlen("/i")))||(!strncasecmp(argv[i], "-i", strlen("-i"))))
211         {
212             CHAR* command_line = argv[i] + strlen("/i");
213
214             CallInstall = TRUE;
215             if (command_line[0] == ':' && command_line[1])
216             {
217                 int len = strlen(command_line);
218
219                 command_line++;
220                 len--;
221                 /* remove double quotes */
222                 if (command_line[0] == '"')
223                 {
224                     command_line++;
225                     len--;
226                     if (command_line[0])
227                     {
228                         len--;
229                         command_line[len] = 0;
230                     }
231                 }
232                 if (command_line[0])
233                 {
234                     len = MultiByteToWideChar(CP_ACP, 0, command_line, -1,
235                                               NULL, 0);
236                     wsCommandLine = HeapAlloc(GetProcessHeap(), 0,
237                                               len * sizeof(WCHAR));
238                     if (wsCommandLine)
239                         MultiByteToWideChar(CP_ACP, 0, command_line, -1,
240                                             wsCommandLine, len);
241                 }
242                 else
243                 {
244                     wsCommandLine = EmptyLine;
245                 }
246             }
247             else
248             {
249                 wsCommandLine = EmptyLine;
250             }
251         }
252         else if((!strcasecmp(argv[i], "/n"))||(!strcasecmp(argv[i], "-n")))
253             CallRegister = FALSE;
254         else if((!strcasecmp(argv[i], "/c"))||(!strcasecmp(argv[i], "-c")))
255             /* console output */;
256         else if (argv[i][0] == '/' && (!argv[i][2] || argv[i][2] == ':'))
257             printf("Unrecognized switch %s\n", argv[i]);
258         else
259         {
260             char *DllName = argv[i];
261             int res = 0;
262
263             DllFound = TRUE;
264             if (!CallInstall || (CallInstall && CallRegister))
265             {
266                 if(Unregister)
267                     res = UnregisterDll(DllName);
268                 else
269                     res = RegisterDll(DllName);
270             }
271
272             if (res)
273                 return res;
274             /* Confirmed.  The windows version does stop on the first error.*/
275
276             if (CallInstall)
277             {
278                 res = InstallDll(!Unregister, DllName, wsCommandLine);
279             }
280
281             if (res)
282                 return res;
283         }
284     }
285
286     if (!DllFound)
287     {
288         if(!Silent)
289             return Usage();
290         else
291             return -1;
292     }
293
294     OleUninitialize();
295
296     return 0;
297 }