| 1 | /* $Id: regsvr32.cpp,v 1.8 2002-05-28 07:35:07 sandervl Exp $ */ | 
|---|
| 2 | /* | 
|---|
| 3 | * RegSvr32 for OS/2 | 
|---|
| 4 | * | 
|---|
| 5 | * 1/9/99 | 
|---|
| 6 | * | 
|---|
| 7 | * Copyright 1999 David J. Raison | 
|---|
| 8 | * | 
|---|
| 9 | * | 
|---|
| 10 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 11 | * | 
|---|
| 12 | */ | 
|---|
| 13 |  | 
|---|
| 14 | #include <windows.h> | 
|---|
| 15 | #include <odinlx.h> | 
|---|
| 16 | #include <stdarg.h> | 
|---|
| 17 | #include <stdio.h> | 
|---|
| 18 | #include <string.h> | 
|---|
| 19 | #include <ctype.h> | 
|---|
| 20 | #include <heapstring.h> | 
|---|
| 21 |  | 
|---|
| 22 | // ====================================================================== | 
|---|
| 23 | // Local Data | 
|---|
| 24 | // ====================================================================== | 
|---|
| 25 | static  int     fRegister = 1; | 
|---|
| 26 | static  int     fSilent = 0; | 
|---|
| 27 | static  char    szFile[1024]; | 
|---|
| 28 | static  HANDLE  hdlObject; | 
|---|
| 29 |  | 
|---|
| 30 | // ====================================================================== | 
|---|
| 31 | // Prototypes. | 
|---|
| 32 | // ====================================================================== | 
|---|
| 33 |  | 
|---|
| 34 | static  HRESULT parse_command(int argc, char * argv[]); | 
|---|
| 35 | static  HRESULT load_object(void); | 
|---|
| 36 | static  HRESULT register_object(void); | 
|---|
| 37 | static  HRESULT deregister_object(void); | 
|---|
| 38 | static  HRESULT unload_object(void); | 
|---|
| 39 | static  HRESULT sign_on(void); | 
|---|
| 40 | static  HRESULT sign_off(void); | 
|---|
| 41 | static  HRESULT report_msg(char * fmt, ...); | 
|---|
| 42 | static  HRESULT report_err(HRESULT hr, char * fmt, ...); | 
|---|
| 43 | static  HRESULT usage(char * fmt, ...); | 
|---|
| 44 | static int WIN32API EntryPoint(HINSTANCE hInstance, HINSTANCE hPrevInstance, | 
|---|
| 45 | LPSTR lpCmdLine, int nCmdShow); | 
|---|
| 46 |  | 
|---|
| 47 | // ====================================================================== | 
|---|
| 48 | // Public API's | 
|---|
| 49 | // ====================================================================== | 
|---|
| 50 |  | 
|---|
| 51 | // ---------------------------------------------------------------------- | 
|---|
| 52 | // main | 
|---|
| 53 | // ---------------------------------------------------------------------- | 
|---|
| 54 | int main(int argc, char *argv[]) | 
|---|
| 55 | { | 
|---|
| 56 | HRESULT     hr; | 
|---|
| 57 |  | 
|---|
| 58 | if((hr = parse_command(argc, argv)) != S_OK) | 
|---|
| 59 | return hr; | 
|---|
| 60 |  | 
|---|
| 61 | RegisterLxExe(EntryPoint, NULL); | 
|---|
| 62 |  | 
|---|
| 63 | return 0; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | // ====================================================================== | 
|---|
| 67 | // Private functions. | 
|---|
| 68 | // ====================================================================== | 
|---|
| 69 |  | 
|---|
| 70 | // ---------------------------------------------------------------------- | 
|---|
| 71 | // EntryPoint | 
|---|
| 72 | // ---------------------------------------------------------------------- | 
|---|
| 73 | static int WIN32API EntryPoint | 
|---|
| 74 | (HINSTANCE   hInstance, | 
|---|
| 75 | HINSTANCE   hPrevInstance, | 
|---|
| 76 | LPSTR       lpCmdLine, | 
|---|
| 77 | int         nCmdShow) | 
|---|
| 78 | { | 
|---|
| 79 | HRESULT     hr; | 
|---|
| 80 |  | 
|---|
| 81 | if((hr = sign_on()) != S_OK) | 
|---|
| 82 | return hr; | 
|---|
| 83 |  | 
|---|
| 84 | if((hr = load_object()) != S_OK) | 
|---|
| 85 | return hr; | 
|---|
| 86 |  | 
|---|
| 87 | if (fRegister) | 
|---|
| 88 | hr = register_object(); | 
|---|
| 89 | else | 
|---|
| 90 | hr = deregister_object(); | 
|---|
| 91 |  | 
|---|
| 92 | if (hr != S_OK) | 
|---|
| 93 | return hr; | 
|---|
| 94 |  | 
|---|
| 95 | if((hr = unload_object()) != S_OK) | 
|---|
| 96 | return hr; | 
|---|
| 97 |  | 
|---|
| 98 | return sign_off(); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | // ---------------------------------------------------------------------- | 
|---|
| 102 | // sign_on | 
|---|
| 103 | // ---------------------------------------------------------------------- | 
|---|
| 104 | static  HRESULT sign_on(void) | 
|---|
| 105 | { | 
|---|
| 106 | HRESULT     hr; | 
|---|
| 107 |  | 
|---|
| 108 | hr = OleInitialize(NULL); | 
|---|
| 109 | if (hr != S_OK) | 
|---|
| 110 | return report_err(hr, "Error initialising OLE"); | 
|---|
| 111 |  | 
|---|
| 112 | return S_OK; | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | // ---------------------------------------------------------------------- | 
|---|
| 116 | // sign_off | 
|---|
| 117 | // ---------------------------------------------------------------------- | 
|---|
| 118 | static  HRESULT sign_off(void) | 
|---|
| 119 | { | 
|---|
| 120 | OleUninitialize(); | 
|---|
| 121 | return report_msg("%s initialised OK", szFile); | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | // ---------------------------------------------------------------------- | 
|---|
| 125 | // parse_command | 
|---|
| 126 | // ---------------------------------------------------------------------- | 
|---|
| 127 | static  HRESULT parse_command(int argc, char * argv[]) | 
|---|
| 128 | { | 
|---|
| 129 | int         ii; | 
|---|
| 130 | char *      pArg; | 
|---|
| 131 |  | 
|---|
| 132 | // Initial conditions. | 
|---|
| 133 | fRegister = 1; | 
|---|
| 134 | fSilent = 0; | 
|---|
| 135 | szFile[0] = 0; | 
|---|
| 136 |  | 
|---|
| 137 | // Loop through args... | 
|---|
| 138 | for (ii = 1; ii < argc; ii++) | 
|---|
| 139 | { | 
|---|
| 140 | pArg = argv[ii]; | 
|---|
| 141 |  | 
|---|
| 142 | if ((pArg[0] == '-') || (pArg[0] == '/')) | 
|---|
| 143 | { | 
|---|
| 144 | switch(toupper(pArg[1])) | 
|---|
| 145 | { | 
|---|
| 146 | case 'S': | 
|---|
| 147 | { | 
|---|
| 148 | fSilent = 1; | 
|---|
| 149 | break; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | case 'U': | 
|---|
| 153 | { | 
|---|
| 154 | fRegister = 0; | 
|---|
| 155 | break; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | default: | 
|---|
| 159 | { | 
|---|
| 160 | return usage("Unknown switch '%c'", pArg[1] ); | 
|---|
| 161 | } | 
|---|
| 162 | } | 
|---|
| 163 | } | 
|---|
| 164 | else if (strlen(szFile) == 0) | 
|---|
| 165 | { | 
|---|
| 166 | strcpy(szFile, pArg); | 
|---|
| 167 | } | 
|---|
| 168 | else | 
|---|
| 169 | { | 
|---|
| 170 | return usage("Too many files specified"); | 
|---|
| 171 | } | 
|---|
| 172 | } | 
|---|
| 173 | if (strlen(szFile) == 0) | 
|---|
| 174 | return usage("No file specified"); | 
|---|
| 175 |  | 
|---|
| 176 | return S_OK; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | // ---------------------------------------------------------------------- | 
|---|
| 180 | // load_object | 
|---|
| 181 | // ---------------------------------------------------------------------- | 
|---|
| 182 | static  HRESULT load_object(void) | 
|---|
| 183 | { | 
|---|
| 184 | //SvL: Use LoadLibrary here, not CoLoadLibrary | 
|---|
| 185 | //     Some dlls (divx codec) call CoFreeUnusedLibraries which unloads our dll | 
|---|
| 186 | //     if we use CoLoadLibrary | 
|---|
| 187 | //     NT's regsvr32 doesn't use CoLoadLibrary either. | 
|---|
| 188 | #if 1 | 
|---|
| 189 | hdlObject = LoadLibraryA(szFile); | 
|---|
| 190 | #else | 
|---|
| 191 | LPOLESTR libnameW = (LPOLESTR)HEAP_strdupAtoW(GetProcessHeap(), 0, szFile); | 
|---|
| 192 | hdlObject = LoadLibraryW(libnameW); | 
|---|
| 193 | HeapFree(GetProcessHeap(), 0, libnameW); | 
|---|
| 194 | #endif | 
|---|
| 195 | if (hdlObject == 0) | 
|---|
| 196 | return report_err(S_FALSE, "Failed to load '%s'", szFile); | 
|---|
| 197 |  | 
|---|
| 198 | return S_OK; | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | // ---------------------------------------------------------------------- | 
|---|
| 202 | // register_object | 
|---|
| 203 | // ---------------------------------------------------------------------- | 
|---|
| 204 | static  HRESULT register_object(void) | 
|---|
| 205 | { | 
|---|
| 206 | typedef HRESULT(*LPFNREGISTER)(void); | 
|---|
| 207 |  | 
|---|
| 208 | LPFNREGISTER        pfnRegister; | 
|---|
| 209 | HRESULT             hr; | 
|---|
| 210 |  | 
|---|
| 211 | pfnRegister = (LPFNREGISTER)GetProcAddress(hdlObject, "DllRegisterServer"); | 
|---|
| 212 |  | 
|---|
| 213 | if (pfnRegister == NULL) | 
|---|
| 214 | return report_err(S_FALSE, "Unable to locate registration entry point in '%s'", szFile); | 
|---|
| 215 |  | 
|---|
| 216 | hr = pfnRegister(); | 
|---|
| 217 | if (hr != S_OK) | 
|---|
| 218 | return report_err(hr, "Registration for '%s' returned error 0x%08lx", szFile, hr); | 
|---|
| 219 |  | 
|---|
| 220 | return S_OK; | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | // ---------------------------------------------------------------------- | 
|---|
| 224 | // deregister_object | 
|---|
| 225 | // ---------------------------------------------------------------------- | 
|---|
| 226 | static  HRESULT deregister_object(void) | 
|---|
| 227 | { | 
|---|
| 228 | typedef HRESULT(*LPFNUNREGISTER)(void); | 
|---|
| 229 |  | 
|---|
| 230 | LPFNUNREGISTER      pfnUnregister; | 
|---|
| 231 | HRESULT             hr; | 
|---|
| 232 |  | 
|---|
| 233 | pfnUnregister = (LPFNUNREGISTER)GetProcAddress(hdlObject, "DllUnregisterServer"); | 
|---|
| 234 |  | 
|---|
| 235 | if (pfnUnregister == NULL) | 
|---|
| 236 | return report_err(S_FALSE, "Unable to locate unregistration entry point in '%s'", szFile); | 
|---|
| 237 |  | 
|---|
| 238 | hr = pfnUnregister(); | 
|---|
| 239 | if (hr != S_OK) | 
|---|
| 240 | return report_err(hr, "Unregistration for '%s' returned error 0x%08lx", szFile, hr); | 
|---|
| 241 |  | 
|---|
| 242 | return S_OK; | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | // ---------------------------------------------------------------------- | 
|---|
| 246 | // unload_object | 
|---|
| 247 | // ---------------------------------------------------------------------- | 
|---|
| 248 | static  HRESULT unload_object(void) | 
|---|
| 249 | { | 
|---|
| 250 | #if 1 | 
|---|
| 251 | FreeLibrary(hdlObject); | 
|---|
| 252 | #else | 
|---|
| 253 | CoFreeLibrary(hdlObject); | 
|---|
| 254 | #endif | 
|---|
| 255 | return S_OK; | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | // ---------------------------------------------------------------------- | 
|---|
| 259 | // report_msg | 
|---|
| 260 | // ---------------------------------------------------------------------- | 
|---|
| 261 | static  HRESULT report_msg(char * fmt, ...) | 
|---|
| 262 | { | 
|---|
| 263 | if (!fSilent) | 
|---|
| 264 | { | 
|---|
| 265 | va_list va_ptr; | 
|---|
| 266 | HWND    hwnd = GetDesktopWindow(); | 
|---|
| 267 | char    text[1024]; | 
|---|
| 268 |  | 
|---|
| 269 | va_start(va_ptr, fmt); | 
|---|
| 270 | vsprintf(text, fmt, va_ptr); | 
|---|
| 271 | va_end(va_ptr); | 
|---|
| 272 |  | 
|---|
| 273 | MessageBoxA(hwnd, text, "RegSvr32", MB_OK | MB_ICONINFORMATION); | 
|---|
| 274 | } | 
|---|
| 275 | return S_OK; | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | // ---------------------------------------------------------------------- | 
|---|
| 279 | // report_err | 
|---|
| 280 | // ---------------------------------------------------------------------- | 
|---|
| 281 | static  HRESULT report_err(HRESULT hr, char * fmt, ...) | 
|---|
| 282 | { | 
|---|
| 283 | va_list     va_ptr; | 
|---|
| 284 | HWND        hwnd = GetDesktopWindow(); | 
|---|
| 285 | char        text[1024]; | 
|---|
| 286 |  | 
|---|
| 287 | va_start(va_ptr, fmt); | 
|---|
| 288 | vsprintf(text, fmt, va_ptr); | 
|---|
| 289 | va_end(va_ptr); | 
|---|
| 290 |  | 
|---|
| 291 | MessageBoxA(hwnd, text, "RegSvr32", MB_OK | MB_ICONERROR); | 
|---|
| 292 |  | 
|---|
| 293 | return hr; | 
|---|
| 294 | } | 
|---|
| 295 |  | 
|---|
| 296 | // ---------------------------------------------------------------------- | 
|---|
| 297 | // usage | 
|---|
| 298 | // ---------------------------------------------------------------------- | 
|---|
| 299 | static  HRESULT usage(char * fmt, ...) | 
|---|
| 300 | { | 
|---|
| 301 | va_list     va_ptr; | 
|---|
| 302 | HWND        hwnd = GetDesktopWindow(); | 
|---|
| 303 | char        text[256]; | 
|---|
| 304 | char        msg[1024]; | 
|---|
| 305 |  | 
|---|
| 306 | va_start(va_ptr, fmt); | 
|---|
| 307 | vsprintf(text, fmt, va_ptr); | 
|---|
| 308 | va_end(va_ptr); | 
|---|
| 309 | sprintf(msg, "Error - %s\n\n" | 
|---|
| 310 | "Usage:\n" | 
|---|
| 311 | "regsvr32 [/s] [/u] filename\n" | 
|---|
| 312 | "where\n" | 
|---|
| 313 | "/s - silent completion\n" | 
|---|
| 314 | "/u - unregister", text); | 
|---|
| 315 |  | 
|---|
| 316 | MessageBoxA(hwnd, msg, "RegSvr32", MB_OK | MB_ICONWARNING); | 
|---|
| 317 |  | 
|---|
| 318 | return S_FALSE; | 
|---|
| 319 | } | 
|---|