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