Implemented regsvr32 tool. Based on ReactOS implementation.
[wine/multimedia.git] / programs / regsvr32 / regsvr32.c
blob587e6fbacfffdf727a60525175a04ca4dcc2babd
1 /*
2 * PURPOSE: Register an OLE component 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
25 * regsvr32 [/u] [/s] [/n] [/i[:cmdline]] dllname
26 * [/u] unregister server
27 * [/s] silent (no message boxes)
28 * [/i] Call DllInstall passing it an optional [cmdline];
29 * when used with /u calls dll uninstall.
30 * [/n] Do not call DllRegisterServer; this option must be used with [/i]
33 /**
34 * FIXME - currently receives command-line parameters in ASCII only and later
35 * converts to Unicode. Ideally the function should have wWinMain etry point
36 * and then work in Unicode only, but it seems Wine does not have necessary
37 * support.
40 #include <stdio.h>
41 #include <windows.h>
43 typedef HRESULT (*DLLREGISTER) (void);
44 typedef HRESULT (*DLLUNREGISTER) (void);
45 typedef HRESULT (*DLLINSTALL) (BOOL,LPCWSTR);
47 int Silent = 0;
49 int Usage()
51 printf("regsvr32 [/u] [/s] [/n] [/i[:cmdline]] dllname\n");
52 printf("\t[/u] unregister server\n");
53 printf("\t[/s] silent (no message boxes)\n");
54 printf("\t[/i] Call DllInstall passing it an optional [cmdline];\n");
55 printf("\t when used with /u calls dll uninstall\n");
56 printf("\t[/n] Do not call DllRegisterServer; this option "
57 "must be used with [/i]\n");
58 return 0;
61 /**
62 * Loads procedure.
64 * Parameters:
65 * strDll - name of the dll.
66 * procName - name of the procedure to load from dll
67 * pDllHanlde - output variable receives handle of the loaded dll.
69 VOID *LoadProc(char* strDll, char* procName, HMODULE* DllHandle)
71 VOID* (*proc)(void);
73 *DllHandle = LoadLibrary(strDll);
74 if(!*DllHandle)
76 if(!Silent)
77 printf("Dll %s not found\n", strDll);
79 exit(-1);
81 proc = (VOID *) GetProcAddress(*DllHandle, procName);
82 if(!proc)
84 if(!Silent)
85 printf("%s not implemented in dll %s\n", procName, strDll);
86 FreeLibrary(*DllHandle);
87 exit(-1);
89 return proc;
92 int RegisterDll(char* strDll)
94 HRESULT hr;
95 DLLREGISTER pfRegister;
96 HMODULE DllHandle = NULL;
98 pfRegister = LoadProc(strDll, "DllRegisterServer", &DllHandle);
100 hr = pfRegister();
101 if(FAILED(hr))
103 if(!Silent)
104 printf("Failed to register dll %s\n", strDll);
106 return -1;
108 if(!Silent)
109 printf("Succesfully registered dll %s\n", strDll);
111 if(DllHandle)
112 FreeLibrary(DllHandle);
113 return 0;
116 int UnregisterDll(char* strDll)
118 HRESULT hr;
119 DLLUNREGISTER pfUnregister;
120 HMODULE DllHandle = NULL;
122 pfUnregister = LoadProc(strDll, "DllUnregisterServer", &DllHandle);
123 hr = pfUnregister();
124 if(FAILED(hr))
126 if(!Silent)
127 printf("Failed to unregister dll %s\n", strDll);
129 return -1;
131 if(!Silent)
132 printf("Succesfully unregistered dll %s\n", strDll);
134 if(DllHandle)
135 FreeLibrary(DllHandle);
136 return 0;
139 int InstallDll(BOOL install, char *strDll, WCHAR *command_line)
141 HRESULT hr;
142 DLLINSTALL pfInstall;
143 HMODULE DllHandle = NULL;
145 pfInstall = LoadProc(strDll, "DllInstall", &DllHandle);
146 hr = pfInstall(install, command_line);
147 if(FAILED(hr))
149 if(!Silent)
150 printf("Failed to %s dll %s\n", install ? "install" : "uninstall",
151 strDll);
152 return -1;
154 if(!Silent)
155 printf("Succesfully %s dll %s\n", install ? "installed" : "uninstalled",
156 strDll);
158 if(DllHandle)
159 FreeLibrary(DllHandle);
160 return 0;
163 int main(int argc, char* argv[])
165 int i;
166 BOOL CallRegister = TRUE;
167 BOOL CallInstall = FALSE;
168 BOOL Unregister = FALSE;
169 BOOL DllFound = FALSE;
170 WCHAR* wsCommandLine = NULL;
171 WCHAR EmptyLine[1] = {0};
174 for(i = 1; i < argc; i++)
176 if (!stricmp(argv[i], "/u"))
177 Unregister = TRUE;
178 else if (!stricmp(argv[i], "/s"))
179 Silent = 1;
180 else if (!strnicmp(argv[i], "/i", strlen("/i")))
182 CHAR* command_line = argv[i] + strlen("/i");
184 CallInstall = TRUE;
185 if (command_line[0] == ':' && command_line[1])
187 int len = strlen(command_line);
189 command_line++;
190 len--;
191 /* remove double quotes */
192 if (command_line[0] == '"')
194 command_line++;
195 len--;
196 if (command_line[0])
198 len--;
199 command_line[len] = 0;
202 if (command_line[0])
204 len = MultiByteToWideChar(CP_ACP, 0, command_line, -1,
205 NULL, 0);
206 wsCommandLine = HeapAlloc(GetProcessHeap(), 0,
207 len * sizeof(WCHAR));
208 if (wsCommandLine)
209 MultiByteToWideChar(CP_ACP, 0, command_line, -1,
210 wsCommandLine, len);
212 else
214 wsCommandLine = EmptyLine;
217 else
219 wsCommandLine = EmptyLine;
222 else if(!stricmp(argv[i], "/n"))
223 CallRegister = FALSE;
224 else if (argv[i][0] == '/' && (!argv[i][2] || argv[i][2] == ':'))
225 printf("Unrecognized switch %s\n", argv[i]);
226 else
228 char *DllName = argv[i];
229 int res = 0;
231 DllFound = TRUE;
232 if (!CallInstall || (CallInstall && CallRegister))
234 if(Unregister)
235 res = UnregisterDll(DllName);
236 else
237 res = RegisterDll(DllName);
240 if (res)
241 return res;
243 if (CallInstall)
245 res = InstallDll(!Unregister, DllName, wsCommandLine);
248 return res;
252 if (!DllFound)
254 if(!Silent)
255 return Usage();
256 else
257 return -1;
260 return 0;