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