Build all the programs with winegcc.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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  *
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.
39  */
40
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.
46  */
47
48 #include "config.h"
49 #include "wine/port.h"
50
51 #include <stdio.h>
52 #include <string.h>
53 #include <windows.h>
54
55 typedef HRESULT (*DLLREGISTER)          (void);
56 typedef HRESULT (*DLLUNREGISTER)        (void);
57 typedef HRESULT (*DLLINSTALL)           (BOOL,LPCWSTR);
58
59 int Silent = 0;
60
61 int Usage()
62 {
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;
71 }
72
73 /**
74  * Loads procedure.
75  *
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.
80  */
81 VOID *LoadProc(char* strDll, char* procName, HMODULE* DllHandle)
82 {
83     VOID* (*proc)(void);
84
85     *DllHandle = LoadLibraryEx(strDll, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
86     if(!*DllHandle)
87     {
88         if(!Silent)
89             printf("Dll %s not found\n", strDll);
90
91         exit(-1);
92     }
93     proc = (VOID *) GetProcAddress(*DllHandle, procName);
94     if(!proc)
95     {
96         if(!Silent)
97             printf("%s not implemented in dll %s\n", procName, strDll);
98         FreeLibrary(*DllHandle);
99         exit(-1);
100     }
101     return proc;
102 }
103
104 int RegisterDll(char* strDll)
105 {
106     HRESULT hr;
107     DLLREGISTER pfRegister;
108     HMODULE DllHandle = NULL;
109
110     pfRegister = LoadProc(strDll, "DllRegisterServer", &DllHandle);
111
112     hr = pfRegister();
113     if(FAILED(hr))
114     {
115         if(!Silent)
116             printf("Failed to register dll %s\n", strDll);
117
118         return -1;
119     }
120     if(!Silent)
121         printf("Successfully registered dll %s\n", strDll);
122
123     if(DllHandle)
124         FreeLibrary(DllHandle);
125     return 0;
126 }
127
128 int UnregisterDll(char* strDll)
129 {
130     HRESULT hr;
131     DLLUNREGISTER pfUnregister;
132     HMODULE DllHandle = NULL;
133
134     pfUnregister = LoadProc(strDll, "DllUnregisterServer", &DllHandle);
135     hr = pfUnregister();
136     if(FAILED(hr))
137     {
138         if(!Silent)
139             printf("Failed to unregister dll %s\n", strDll);
140
141         return -1;
142     }
143     if(!Silent)
144         printf("Successfully unregistered dll %s\n", strDll);
145
146     if(DllHandle)
147         FreeLibrary(DllHandle);
148     return 0;
149 }
150
151 int InstallDll(BOOL install, char *strDll, WCHAR *command_line)
152 {
153     HRESULT hr;
154     DLLINSTALL pfInstall;
155     HMODULE DllHandle = NULL;
156
157     pfInstall = LoadProc(strDll, "DllInstall", &DllHandle);
158     hr = pfInstall(install, command_line);
159     if(FAILED(hr))
160     {
161         if(!Silent)
162             printf("Failed to %s dll %s\n", install ? "install" : "uninstall",
163                    strDll);
164         return -1;
165     }
166     if(!Silent)
167         printf("Successfully %s dll %s\n",  install ? "installed" : "uninstalled",
168                strDll);
169
170     if(DllHandle)
171         FreeLibrary(DllHandle);
172     return 0;
173 }
174
175 int main(int argc, char* argv[])
176 {
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};
184
185
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.
190      */
191     for(i = 1; i < argc; i++)
192     {
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")))
198         {
199             CHAR* command_line = argv[i] + strlen("/i");
200
201             CallInstall = TRUE;
202             if (command_line[0] == ':' && command_line[1])
203             {
204                 int len = strlen(command_line);
205
206                 command_line++;
207                 len--;
208                 /* remove double quotes */
209                 if (command_line[0] == '"')
210                 {
211                     command_line++;
212                     len--;
213                     if (command_line[0])
214                     {
215                         len--;
216                         command_line[len] = 0;
217                     }
218                 }
219                 if (command_line[0])
220                 {
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);
228                 }
229                 else
230                 {
231                     wsCommandLine = EmptyLine;
232                 }
233             }
234             else
235             {
236                 wsCommandLine = EmptyLine;
237             }
238         }
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
244         {
245             char *DllName = argv[i];
246             int res = 0;
247
248             DllFound = TRUE;
249             if (!CallInstall || (CallInstall && CallRegister))
250             {
251                 if(Unregister)
252                     res = UnregisterDll(DllName);
253                 else
254                     res = RegisterDll(DllName);
255             }
256
257             if (res)
258                 return res;
259             /* Confirmed.  The windows version does stop on the first error.*/
260
261             if (CallInstall)
262             {
263                 res = InstallDll(!Unregister, DllName, wsCommandLine);
264             }
265
266             if (res)
267                 return res;
268         }
269     }
270
271     if (!DllFound)
272     {
273         if(!Silent)
274             return Usage();
275         else
276             return -1;
277     }
278
279     return 0;
280 }