Mercurial > vim
annotate src/if_ole.cpp @ 2469:336d6df580e7 vim73
Workaround for missing RegDeleteKeyEx() method.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Mon, 02 Aug 2010 20:26:43 +0200 |
parents | 7a537d53b3ed |
children | a29301e7c05f |
rev | line source |
---|---|
7 | 1 /* vi:set ts=8 sts=4 sw=4: |
2 * | |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * | |
5 * Do ":help uganda" in Vim to read copying and usage conditions. | |
6 * Do ":help credits" in Vim to see a list of people who contributed. | |
7 */ | |
8 | |
9 #if defined(FEAT_OLE) && defined(FEAT_GUI_W32) | |
10 /* | |
11 * OLE server implementation. | |
12 * | |
13 * See os_mswin.c for the client side. | |
14 */ | |
15 | |
931 | 16 /* |
17 * We have some trouble with order of includes here. For Borland it needs to | |
18 * be different from MSVC... | |
19 */ | |
20 #ifndef __BORLANDC__ | |
919 | 21 extern "C" { |
931 | 22 # include "vim.h" |
919 | 23 } |
931 | 24 #endif |
919 | 25 |
7 | 26 #include <windows.h> |
27 #include <oleauto.h> | |
28 | |
29 extern "C" { | |
931 | 30 #ifdef __BORLANDC__ |
31 # include "vim.h" | |
32 #endif | |
7 | 33 extern HWND s_hwnd; |
34 extern HWND vim_parent_hwnd; | |
35 } | |
36 | |
2224
a0cce15dd2a9
Fix definition of UINT_PTR for 64 bit systems.
Bram Moolenaar <bram@vim.org>
parents:
1791
diff
changeset
|
37 #if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR) |
1569 | 38 /* Work around old versions of basetsd.h which wrongly declares |
39 * UINT_PTR as unsigned long */ | |
2224
a0cce15dd2a9
Fix definition of UINT_PTR for 64 bit systems.
Bram Moolenaar <bram@vim.org>
parents:
1791
diff
changeset
|
40 # undef UINT_PTR |
1569 | 41 # define UINT_PTR UINT |
42 #endif | |
43 | |
7 | 44 #include "if_ole.h" // Interface definitions |
45 #include "iid_ole.c" // UUID definitions (compile here) | |
46 | |
47 /* Supply function prototype to work around bug in Mingw oleauto.h header */ | |
48 #ifdef __MINGW32__ | |
49 WINOLEAUTAPI UnRegisterTypeLib(REFGUID libID, WORD wVerMajor, | |
50 WORD wVerMinor, LCID lcid, SYSKIND syskind); | |
51 #endif | |
52 | |
53 /***************************************************************************** | |
54 1. Internal definitions for this file | |
55 *****************************************************************************/ | |
56 | |
57 class CVim; | |
58 class CVimCF; | |
59 | |
60 /* Internal data */ | |
61 // The identifier of the registered class factory | |
62 static unsigned long cf_id = 0; | |
63 | |
64 // The identifier of the running application object | |
65 static unsigned long app_id = 0; | |
66 | |
67 // The single global instance of the class factory | |
68 static CVimCF *cf = 0; | |
69 | |
70 // The single global instance of the application object | |
71 static CVim *app = 0; | |
72 | |
73 /* GUIDs, versions and type library information */ | |
74 #define MYCLSID CLSID_Vim | |
75 #define MYLIBID LIBID_Vim | |
76 #define MYIID IID_IVim | |
77 | |
78 #define MAJORVER 1 | |
79 #define MINORVER 0 | |
80 #define LOCALE 0x0409 | |
81 | |
82 #define MYNAME "Vim" | |
83 #define MYPROGID "Vim.Application.1" | |
84 #define MYVIPROGID "Vim.Application" | |
85 | |
86 #define MAX_CLSID_LEN 100 | |
87 | |
2463
7a537d53b3ed
Define the WOW64 key when needed.
Bram Moolenaar <bram@vim.org>
parents:
2450
diff
changeset
|
88 /* |
7a537d53b3ed
Define the WOW64 key when needed.
Bram Moolenaar <bram@vim.org>
parents:
2450
diff
changeset
|
89 * Modern way of creating registry entries, also works on 64 bit windows when |
7a537d53b3ed
Define the WOW64 key when needed.
Bram Moolenaar <bram@vim.org>
parents:
2450
diff
changeset
|
90 * compiled as a 32 bit program. |
7a537d53b3ed
Define the WOW64 key when needed.
Bram Moolenaar <bram@vim.org>
parents:
2450
diff
changeset
|
91 */ |
7a537d53b3ed
Define the WOW64 key when needed.
Bram Moolenaar <bram@vim.org>
parents:
2450
diff
changeset
|
92 # ifndef KEY_WOW64_64KEY |
7a537d53b3ed
Define the WOW64 key when needed.
Bram Moolenaar <bram@vim.org>
parents:
2450
diff
changeset
|
93 # define KEY_WOW64_64KEY 0x0100 |
2469
336d6df580e7
Workaround for missing RegDeleteKeyEx() method.
Bram Moolenaar <bram@vim.org>
parents:
2463
diff
changeset
|
94 # define RegDeleteKeyEx(a, b, c, d) RegDeleteKey(a, b) |
2463
7a537d53b3ed
Define the WOW64 key when needed.
Bram Moolenaar <bram@vim.org>
parents:
2450
diff
changeset
|
95 # endif |
7a537d53b3ed
Define the WOW64 key when needed.
Bram Moolenaar <bram@vim.org>
parents:
2450
diff
changeset
|
96 |
7 | 97 /***************************************************************************** |
98 2. The application object | |
99 *****************************************************************************/ | |
100 | |
101 /* Definition | |
102 * ---------- | |
103 */ | |
104 | |
105 class CVim : public IVim | |
106 { | |
107 public: | |
108 ~CVim(); | |
380 | 109 static CVim *Create(int *pbDoRestart); |
7 | 110 |
111 // IUnknown members | |
112 STDMETHOD(QueryInterface)(REFIID riid, void ** ppv); | |
113 STDMETHOD_(unsigned long, AddRef)(void); | |
114 STDMETHOD_(unsigned long, Release)(void); | |
115 | |
116 // IDispatch members | |
117 STDMETHOD(GetTypeInfoCount)(UINT *pCount); | |
118 STDMETHOD(GetTypeInfo)(UINT iTypeInfo, LCID, ITypeInfo **ppITypeInfo); | |
380 | 119 STDMETHOD(GetIDsOfNames)(const IID &iid, OLECHAR **names, UINT n, LCID, DISPID *dispids); |
120 STDMETHOD(Invoke)(DISPID member, const IID &iid, LCID, WORD flags, DISPPARAMS *dispparams, VARIANT *result, EXCEPINFO *excepinfo, UINT *argerr); | |
7 | 121 |
122 // IVim members | |
123 STDMETHOD(SendKeys)(BSTR keys); | |
124 STDMETHOD(Eval)(BSTR expr, BSTR *result); | |
125 STDMETHOD(SetForeground)(void); | |
1569 | 126 STDMETHOD(GetHwnd)(UINT_PTR *result); |
7 | 127 |
128 private: | |
129 // Constructor is private - create using CVim::Create() | |
130 CVim() : ref(0), typeinfo(0) {}; | |
131 | |
132 // Reference count | |
133 unsigned long ref; | |
134 | |
135 // The object's TypeInfo | |
136 ITypeInfo *typeinfo; | |
137 }; | |
138 | |
139 /* Implementation | |
140 * -------------- | |
141 */ | |
142 | |
380 | 143 CVim *CVim::Create(int *pbDoRestart) |
7 | 144 { |
145 HRESULT hr; | |
146 CVim *me = 0; | |
147 ITypeLib *typelib = 0; | |
148 ITypeInfo *typeinfo = 0; | |
149 | |
150 *pbDoRestart = FALSE; | |
151 | |
152 // Create the object | |
153 me = new CVim(); | |
154 if (me == NULL) | |
155 { | |
156 MessageBox(0, "Cannot create application object", "Vim Initialisation", 0); | |
157 return NULL; | |
158 } | |
159 | |
160 // Load the type library from the registry | |
161 hr = LoadRegTypeLib(MYLIBID, 1, 0, 0x00, &typelib); | |
162 if (FAILED(hr)) | |
163 { | |
164 HKEY hKey; | |
165 | |
166 // Check we can write to the registry. | |
167 // RegCreateKeyEx succeeds even if key exists. W.Briscoe W2K 20021011 | |
168 if (RegCreateKeyEx(HKEY_CLASSES_ROOT, MYVIPROGID, 0, NULL, | |
2443
499bff609a86
Possibly make OLE work on Windows 64 bit. (untested)
Bram Moolenaar <bram@vim.org>
parents:
2224
diff
changeset
|
169 REG_OPTION_NON_VOLATILE, |
2450
7e9da593951d
Add the WOW64 flag back to OLE registration. (untested)
Bram Moolenaar <bram@vim.org>
parents:
2449
diff
changeset
|
170 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &hKey, NULL)) |
7 | 171 { |
172 delete me; | |
173 return NULL; // Unable to write to registry. Quietly fail. | |
174 } | |
175 RegCloseKey(hKey); | |
176 | |
177 if (MessageBox(0, "Cannot load registered type library.\nDo you want to register Vim now?", | |
178 "Vim Initialisation", MB_YESNO | MB_ICONQUESTION) != IDYES) | |
179 { | |
180 delete me; | |
181 return NULL; | |
182 } | |
183 | |
184 RegisterMe(FALSE); | |
185 | |
186 // Load the type library from the registry | |
187 hr = LoadRegTypeLib(MYLIBID, 1, 0, 0x00, &typelib); | |
188 if (FAILED(hr)) | |
189 { | |
190 MessageBox(0, "You must restart Vim in order for the registration to take effect.", | |
191 "Vim Initialisation", 0); | |
192 *pbDoRestart = TRUE; | |
193 delete me; | |
194 return NULL; | |
195 } | |
196 } | |
197 | |
198 // Get the type info of the vtable interface | |
199 hr = typelib->GetTypeInfoOfGuid(MYIID, &typeinfo); | |
200 typelib->Release(); | |
201 | |
202 if (FAILED(hr)) | |
203 { | |
204 MessageBox(0, "Cannot get interface type information", | |
205 "Vim Initialisation", 0); | |
206 delete me; | |
207 return NULL; | |
208 } | |
209 | |
210 // Save the type information | |
211 me->typeinfo = typeinfo; | |
212 return me; | |
213 } | |
214 | |
215 CVim::~CVim() | |
216 { | |
217 if (typeinfo && vim_parent_hwnd == NULL) | |
218 typeinfo->Release(); | |
380 | 219 typeinfo = 0; |
7 | 220 } |
221 | |
222 STDMETHODIMP | |
223 CVim::QueryInterface(REFIID riid, void **ppv) | |
224 { | |
225 if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IDispatch) || IsEqualIID(riid, MYIID)) | |
226 { | |
227 AddRef(); | |
228 *ppv = this; | |
229 return S_OK; | |
230 } | |
231 | |
232 *ppv = 0; | |
233 return E_NOINTERFACE; | |
234 } | |
235 | |
236 STDMETHODIMP_(ULONG) | |
237 CVim::AddRef() | |
238 { | |
239 return ++ref; | |
240 } | |
241 | |
242 STDMETHODIMP_(ULONG) | |
243 CVim::Release() | |
244 { | |
245 // Don't delete the object when the reference count reaches zero, as there | |
246 // is only a single application object, and its lifetime is controlled by | |
247 // the running instance, not by its reference count. | |
248 if (ref > 0) | |
249 --ref; | |
250 return ref; | |
251 } | |
252 | |
253 STDMETHODIMP | |
254 CVim::GetTypeInfoCount(UINT *pCount) | |
255 { | |
256 *pCount = 1; | |
257 return S_OK; | |
258 } | |
259 | |
260 STDMETHODIMP | |
261 CVim::GetTypeInfo(UINT iTypeInfo, LCID, ITypeInfo **ppITypeInfo) | |
262 { | |
263 *ppITypeInfo = 0; | |
264 | |
265 if (iTypeInfo != 0) | |
266 return DISP_E_BADINDEX; | |
267 | |
268 typeinfo->AddRef(); | |
269 *ppITypeInfo = typeinfo; | |
270 return S_OK; | |
271 } | |
272 | |
273 STDMETHODIMP | |
274 CVim::GetIDsOfNames( | |
380 | 275 const IID &iid, |
276 OLECHAR **names, | |
7 | 277 UINT n, |
278 LCID, | |
279 DISPID *dispids) | |
280 { | |
281 if (iid != IID_NULL) | |
282 return DISP_E_UNKNOWNINTERFACE; | |
283 | |
284 return typeinfo->GetIDsOfNames(names, n, dispids); | |
285 } | |
286 | |
287 STDMETHODIMP | |
288 CVim::Invoke( | |
289 DISPID member, | |
380 | 290 const IID &iid, |
7 | 291 LCID, |
292 WORD flags, | |
293 DISPPARAMS *dispparams, | |
294 VARIANT *result, | |
295 EXCEPINFO *excepinfo, | |
296 UINT *argerr) | |
297 { | |
298 if (iid != IID_NULL) | |
299 return DISP_E_UNKNOWNINTERFACE; | |
300 | |
301 ::SetErrorInfo(0, NULL); | |
302 return typeinfo->Invoke(static_cast<IDispatch*>(this), | |
303 member, flags, dispparams, | |
304 result, excepinfo, argerr); | |
305 } | |
306 | |
307 STDMETHODIMP | |
1569 | 308 CVim::GetHwnd(UINT_PTR *result) |
7 | 309 { |
1569 | 310 *result = (UINT_PTR)s_hwnd; |
7 | 311 return S_OK; |
312 } | |
313 | |
314 STDMETHODIMP | |
315 CVim::SetForeground(void) | |
316 { | |
317 /* Make the Vim window come to the foreground */ | |
318 gui_mch_set_foreground(); | |
319 return S_OK; | |
320 } | |
321 | |
322 STDMETHODIMP | |
323 CVim::SendKeys(BSTR keys) | |
324 { | |
325 int len; | |
326 char *buffer; | |
327 char_u *str; | |
328 char_u *ptr; | |
329 | |
330 /* Get a suitable buffer */ | |
331 len = WideCharToMultiByte(CP_ACP, 0, keys, -1, 0, 0, 0, 0); | |
332 buffer = (char *)alloc(len+1); | |
333 | |
334 if (buffer == NULL) | |
335 return E_OUTOFMEMORY; | |
336 | |
337 len = WideCharToMultiByte(CP_ACP, 0, keys, -1, buffer, len, 0, 0); | |
338 | |
339 if (len == 0) | |
340 { | |
341 vim_free(buffer); | |
342 return E_INVALIDARG; | |
343 } | |
344 | |
345 /* Translate key codes like <Esc> */ | |
859 | 346 str = replace_termcodes((char_u *)buffer, &ptr, FALSE, TRUE, FALSE); |
7 | 347 |
348 /* If ptr was set, then a new buffer was allocated, | |
349 * so we can free the old one. | |
350 */ | |
351 if (ptr) | |
352 vim_free((char_u *)(buffer)); | |
353 | |
354 /* Reject strings too long to fit in the input buffer. Allow 10 bytes | |
355 * space to cover for the (remote) possibility that characters may enter | |
356 * the input buffer between now and when the WM_OLE message is actually | |
357 * processed. If more that 10 characters enter the input buffer in that | |
358 * time, the WM_OLE processing will simply fail to insert the characters. | |
359 */ | |
360 if ((int)(STRLEN(str)) > (vim_free_in_input_buf() - 10)) | |
361 { | |
362 vim_free(str); | |
363 return E_INVALIDARG; | |
364 } | |
365 | |
366 /* Pass the string to the main input loop. The memory will be freed when | |
1791 | 367 * the message is processed. Except for an empty message, we don't need |
368 * to post it then. | |
7 | 369 */ |
1791 | 370 if (*str == NUL) |
371 vim_free(str); | |
372 else | |
373 PostMessage(NULL, WM_OLE, 0, (LPARAM)str); | |
7 | 374 |
375 return S_OK; | |
376 } | |
377 | |
378 STDMETHODIMP | |
379 CVim::Eval(BSTR expr, BSTR *result) | |
380 { | |
381 #ifdef FEAT_EVAL | |
382 int len; | |
383 char *buffer; | |
384 char *str; | |
385 wchar_t *w_buffer; | |
386 | |
387 /* Get a suitable buffer */ | |
388 len = WideCharToMultiByte(CP_ACP, 0, expr, -1, 0, 0, 0, 0); | |
389 if (len == 0) | |
390 return E_INVALIDARG; | |
391 | |
392 buffer = (char *)alloc((unsigned)len); | |
393 | |
394 if (buffer == NULL) | |
395 return E_OUTOFMEMORY; | |
396 | |
397 /* Convert the (wide character) expression to an ASCII string */ | |
398 len = WideCharToMultiByte(CP_ACP, 0, expr, -1, buffer, len, 0, 0); | |
399 if (len == 0) | |
400 return E_INVALIDARG; | |
401 | |
402 /* Evaluate the expression */ | |
403 ++emsg_skip; | |
714 | 404 str = (char *)eval_to_string((char_u *)buffer, NULL, TRUE); |
7 | 405 --emsg_skip; |
406 vim_free(buffer); | |
407 if (str == NULL) | |
408 return E_FAIL; | |
409 | |
410 /* Convert the result to wide characters */ | |
411 MultiByteToWideChar_alloc(CP_ACP, 0, str, -1, &w_buffer, &len); | |
412 vim_free(str); | |
413 if (w_buffer == NULL) | |
414 return E_OUTOFMEMORY; | |
415 | |
416 if (len == 0) | |
417 { | |
418 vim_free(w_buffer); | |
419 return E_FAIL; | |
420 } | |
421 | |
422 /* Store the result */ | |
423 *result = SysAllocString(w_buffer); | |
424 vim_free(w_buffer); | |
425 | |
426 return S_OK; | |
427 #else | |
428 return E_NOTIMPL; | |
429 #endif | |
430 } | |
431 | |
432 /***************************************************************************** | |
433 3. The class factory | |
434 *****************************************************************************/ | |
435 | |
436 /* Definition | |
437 * ---------- | |
438 */ | |
439 | |
440 class CVimCF : public IClassFactory | |
441 { | |
442 public: | |
443 static CVimCF *Create(); | |
444 | |
445 STDMETHOD(QueryInterface)(REFIID riid, void ** ppv); | |
446 STDMETHOD_(unsigned long, AddRef)(void); | |
447 STDMETHOD_(unsigned long, Release)(void); | |
448 STDMETHOD(CreateInstance)(IUnknown *punkOuter, REFIID riid, void ** ppv); | |
449 STDMETHOD(LockServer)(BOOL lock); | |
450 | |
451 private: | |
452 // Constructor is private - create via Create() | |
453 CVimCF() : ref(0) {}; | |
454 | |
455 // Reference count | |
456 unsigned long ref; | |
457 }; | |
458 | |
459 /* Implementation | |
460 * -------------- | |
461 */ | |
462 | |
463 CVimCF *CVimCF::Create() | |
464 { | |
465 CVimCF *me = new CVimCF(); | |
466 | |
467 if (me == NULL) | |
468 MessageBox(0, "Cannot create class factory", "Vim Initialisation", 0); | |
469 | |
470 return me; | |
471 } | |
472 | |
473 STDMETHODIMP | |
474 CVimCF::QueryInterface(REFIID riid, void **ppv) | |
475 { | |
476 if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory)) | |
477 { | |
478 AddRef(); | |
479 *ppv = this; | |
480 return S_OK; | |
481 } | |
482 | |
483 *ppv = 0; | |
484 return E_NOINTERFACE; | |
485 } | |
486 | |
487 STDMETHODIMP_(ULONG) | |
488 CVimCF::AddRef() | |
489 { | |
490 return ++ref; | |
491 } | |
492 | |
493 STDMETHODIMP_(ULONG) | |
494 CVimCF::Release() | |
495 { | |
496 // Don't delete the object when the reference count reaches zero, as there | |
497 // is only a single application object, and its lifetime is controlled by | |
498 // the running instance, not by its reference count. | |
499 if (ref > 0) | |
500 --ref; | |
501 return ref; | |
502 } | |
503 | |
322 | 504 /*ARGSUSED*/ |
7 | 505 STDMETHODIMP |
506 CVimCF::CreateInstance(IUnknown *punkOuter, REFIID riid, void **ppv) | |
507 { | |
508 return app->QueryInterface(riid, ppv); | |
509 } | |
510 | |
322 | 511 /*ARGSUSED*/ |
7 | 512 STDMETHODIMP |
513 CVimCF::LockServer(BOOL lock) | |
514 { | |
515 return S_OK; | |
516 } | |
517 | |
518 /***************************************************************************** | |
519 4. Registry manipulation code | |
520 *****************************************************************************/ | |
521 | |
522 // Internal use only | |
380 | 523 static void SetKeyAndValue(const char *path, const char *subkey, const char *value); |
524 static void GUIDtochar(const GUID &guid, char *GUID, int length); | |
525 static void RecursiveDeleteKey(HKEY hKeyParent, const char *child); | |
7 | 526 static const int GUID_STRING_SIZE = 39; |
527 | |
528 // Register the component in the registry | |
529 // When "silent" is TRUE don't give any messages. | |
530 | |
531 extern "C" void RegisterMe(int silent) | |
532 { | |
533 BOOL ok = TRUE; | |
534 | |
535 // Get the application startup command | |
536 char module[MAX_PATH]; | |
537 | |
538 ::GetModuleFileName(NULL, module, MAX_PATH); | |
539 | |
540 // Unregister first (quietly) | |
541 UnregisterMe(FALSE); | |
542 | |
543 // Convert the CLSID into a char | |
544 char clsid[GUID_STRING_SIZE]; | |
545 GUIDtochar(MYCLSID, clsid, sizeof(clsid)); | |
546 | |
547 // Convert the LIBID into a char | |
548 char libid[GUID_STRING_SIZE]; | |
549 GUIDtochar(MYLIBID, libid, sizeof(libid)); | |
550 | |
551 // Build the key CLSID\\{...} | |
552 char Key[MAX_CLSID_LEN]; | |
553 strcpy(Key, "CLSID\\"); | |
554 strcat(Key, clsid); | |
555 | |
556 // Add the CLSID to the registry | |
557 SetKeyAndValue(Key, NULL, MYNAME); | |
558 SetKeyAndValue(Key, "LocalServer32", module); | |
559 SetKeyAndValue(Key, "ProgID", MYPROGID); | |
560 SetKeyAndValue(Key, "VersionIndependentProgID", MYVIPROGID); | |
561 SetKeyAndValue(Key, "TypeLib", libid); | |
562 | |
563 // Add the version-independent ProgID subkey under HKEY_CLASSES_ROOT | |
564 SetKeyAndValue(MYVIPROGID, NULL, MYNAME); | |
565 SetKeyAndValue(MYVIPROGID, "CLSID", clsid); | |
566 SetKeyAndValue(MYVIPROGID, "CurVer", MYPROGID); | |
567 | |
568 // Add the versioned ProgID subkey under HKEY_CLASSES_ROOT | |
569 SetKeyAndValue(MYPROGID, NULL, MYNAME); | |
570 SetKeyAndValue(MYPROGID, "CLSID", clsid); | |
571 | |
572 wchar_t w_module[MAX_PATH]; | |
573 MultiByteToWideChar(CP_ACP, 0, module, -1, w_module, MAX_PATH); | |
574 | |
575 ITypeLib *typelib = NULL; | |
576 if (LoadTypeLib(w_module, &typelib) != S_OK) | |
577 { | |
578 if (!silent) | |
579 MessageBox(0, "Cannot load type library to register", | |
580 "Vim Registration", 0); | |
581 ok = FALSE; | |
582 } | |
583 else | |
584 { | |
585 if (RegisterTypeLib(typelib, w_module, NULL) != S_OK) | |
586 { | |
587 if (!silent) | |
588 MessageBox(0, "Cannot register type library", | |
589 "Vim Registration", 0); | |
590 ok = FALSE; | |
591 } | |
592 typelib->Release(); | |
593 } | |
594 | |
595 if (ok && !silent) | |
596 MessageBox(0, "Registered successfully", "Vim", 0); | |
597 } | |
598 | |
599 // Remove the component from the registry | |
600 // | |
601 // Note: There is little error checking in this code, to allow incomplete | |
602 // or failed registrations to be undone. | |
603 extern "C" void UnregisterMe(int bNotifyUser) | |
604 { | |
605 // Unregister the type library | |
606 ITypeLib *typelib; | |
607 if (SUCCEEDED(LoadRegTypeLib(MYLIBID, MAJORVER, MINORVER, LOCALE, &typelib))) | |
608 { | |
609 TLIBATTR *tla; | |
610 if (SUCCEEDED(typelib->GetLibAttr(&tla))) | |
611 { | |
612 UnRegisterTypeLib(tla->guid, tla->wMajorVerNum, tla->wMinorVerNum, | |
613 tla->lcid, tla->syskind); | |
614 typelib->ReleaseTLibAttr(tla); | |
615 } | |
616 typelib->Release(); | |
617 } | |
618 | |
619 // Convert the CLSID into a char | |
620 char clsid[GUID_STRING_SIZE]; | |
621 GUIDtochar(MYCLSID, clsid, sizeof(clsid)); | |
622 | |
623 // Build the key CLSID\\{...} | |
624 char Key[MAX_CLSID_LEN]; | |
625 strcpy(Key, "CLSID\\"); | |
626 strcat(Key, clsid); | |
627 | |
628 // Delete the CLSID Key - CLSID\{...} | |
629 RecursiveDeleteKey(HKEY_CLASSES_ROOT, Key); | |
630 | |
631 // Delete the version-independent ProgID Key | |
632 RecursiveDeleteKey(HKEY_CLASSES_ROOT, MYVIPROGID); | |
633 | |
634 // Delete the ProgID key | |
635 RecursiveDeleteKey(HKEY_CLASSES_ROOT, MYPROGID); | |
636 | |
637 if (bNotifyUser) | |
638 MessageBox(0, "Unregistered successfully", "Vim", 0); | |
639 } | |
640 | |
641 /****************************************************************************/ | |
642 | |
643 // Convert a GUID to a char string | |
380 | 644 static void GUIDtochar(const GUID &guid, char *GUID, int length) |
7 | 645 { |
646 // Get wide string version | |
647 LPOLESTR wGUID = NULL; | |
648 StringFromCLSID(guid, &wGUID); | |
649 | |
650 // Covert from wide characters to non-wide | |
651 wcstombs(GUID, wGUID, length); | |
652 | |
653 // Free memory | |
654 CoTaskMemFree(wGUID); | |
655 } | |
656 | |
657 // Delete a key and all of its descendents | |
380 | 658 static void RecursiveDeleteKey(HKEY hKeyParent, const char *child) |
7 | 659 { |
660 // Open the child | |
661 HKEY hKeyChild; | |
2449
943280505f72
Fix that uninstaller isn't found on 64-bit Windows.
Bram Moolenaar <bram@vim.org>
parents:
2443
diff
changeset
|
662 LONG result = RegOpenKeyEx(hKeyParent, child, 0, |
2450
7e9da593951d
Add the WOW64 flag back to OLE registration. (untested)
Bram Moolenaar <bram@vim.org>
parents:
2449
diff
changeset
|
663 KEY_WOW64_64KEY | KEY_ALL_ACCESS, &hKeyChild); |
7 | 664 if (result != ERROR_SUCCESS) |
665 return; | |
666 | |
667 // Enumerate all of the decendents of this child | |
668 FILETIME time; | |
669 char buffer[1024]; | |
670 DWORD size = 1024; | |
671 | |
672 while (RegEnumKeyEx(hKeyChild, 0, buffer, &size, NULL, | |
673 NULL, NULL, &time) == S_OK) | |
674 { | |
675 // Delete the decendents of this child | |
676 RecursiveDeleteKey(hKeyChild, buffer); | |
677 size = 256; | |
678 } | |
679 | |
680 // Close the child | |
681 RegCloseKey(hKeyChild); | |
682 | |
683 // Delete this child | |
684 RegDeleteKey(hKeyParent, child); | |
685 } | |
686 | |
687 // Create a key and set its value | |
388 | 688 static void SetKeyAndValue(const char *key, const char *subkey, const char *value) |
7 | 689 { |
690 HKEY hKey; | |
691 char buffer[1024]; | |
692 | |
693 strcpy(buffer, key); | |
694 | |
695 // Add subkey name to buffer. | |
696 if (subkey) | |
697 { | |
698 strcat(buffer, "\\"); | |
699 strcat(buffer, subkey); | |
700 } | |
701 | |
702 // Create and open key and subkey. | |
703 long result = RegCreateKeyEx(HKEY_CLASSES_ROOT, | |
704 buffer, | |
705 0, NULL, REG_OPTION_NON_VOLATILE, | |
2450
7e9da593951d
Add the WOW64 flag back to OLE registration. (untested)
Bram Moolenaar <bram@vim.org>
parents:
2449
diff
changeset
|
706 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, |
7 | 707 &hKey, NULL); |
708 if (result != ERROR_SUCCESS) | |
709 return; | |
710 | |
711 // Set the value | |
712 if (value) | |
713 RegSetValueEx(hKey, NULL, 0, REG_SZ, (BYTE *)value, | |
714 (DWORD)STRLEN(value)+1); | |
715 | |
716 RegCloseKey(hKey); | |
717 } | |
718 | |
719 /***************************************************************************** | |
720 5. OLE Initialisation and shutdown processing | |
721 *****************************************************************************/ | |
380 | 722 extern "C" void InitOLE(int *pbDoRestart) |
7 | 723 { |
724 HRESULT hr; | |
725 | |
726 *pbDoRestart = FALSE; | |
727 | |
728 // Initialize the OLE libraries | |
729 hr = OleInitialize(NULL); | |
730 if (FAILED(hr)) | |
731 { | |
732 MessageBox(0, "Cannot initialise OLE", "Vim Initialisation", 0); | |
733 goto error0; | |
734 } | |
735 | |
736 // Create the application object | |
737 app = CVim::Create(pbDoRestart); | |
738 if (app == NULL) | |
739 goto error1; | |
740 | |
741 // Create the class factory | |
742 cf = CVimCF::Create(); | |
743 if (cf == NULL) | |
744 goto error1; | |
745 | |
746 // Register the class factory | |
747 hr = CoRegisterClassObject( | |
748 MYCLSID, | |
749 cf, | |
750 CLSCTX_LOCAL_SERVER, | |
751 REGCLS_MULTIPLEUSE, | |
752 &cf_id); | |
753 | |
754 if (FAILED(hr)) | |
755 { | |
756 MessageBox(0, "Cannot register class factory", "Vim Initialisation", 0); | |
757 goto error1; | |
758 } | |
759 | |
760 // Register the application object as active | |
761 hr = RegisterActiveObject( | |
762 app, | |
763 MYCLSID, | |
764 NULL, | |
765 &app_id); | |
766 | |
767 if (FAILED(hr)) | |
768 { | |
769 MessageBox(0, "Cannot register application object", "Vim Initialisation", 0); | |
770 goto error1; | |
771 } | |
772 | |
773 return; | |
774 | |
775 // Errors: tidy up as much as needed and return | |
776 error1: | |
777 UninitOLE(); | |
778 error0: | |
779 return; | |
780 } | |
781 | |
782 extern "C" void UninitOLE() | |
783 { | |
784 // Unregister the application object | |
785 if (app_id) | |
786 { | |
787 RevokeActiveObject(app_id, NULL); | |
788 app_id = 0; | |
789 } | |
790 | |
791 // Unregister the class factory | |
792 if (cf_id) | |
793 { | |
794 CoRevokeClassObject(cf_id); | |
795 cf_id = 0; | |
796 } | |
797 | |
798 // Shut down the OLE libraries | |
799 OleUninitialize(); | |
800 | |
801 // Delete the application object | |
802 if (app) | |
803 { | |
804 delete app; | |
805 app = NULL; | |
806 } | |
807 | |
808 // Delete the class factory | |
809 if (cf) | |
810 { | |
811 delete cf; | |
812 cf = NULL; | |
813 } | |
814 } | |
815 #endif /* FEAT_OLE */ |