blob: ff9e371a70edef9a36ce4f4a42b1d1ceecd82661 [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
Bram Moolenaar3f97ebf2010-08-02 20:26:43 +020094# define RegDeleteKeyEx(a, b, c, d) RegDeleteKey(a, b)
Bram Moolenaar92096d52010-08-01 19:50:25 +020095# endif
96
Bram Moolenaar071d4272004-06-13 20:20:40 +000097/*****************************************************************************
98 2. The application object
99*****************************************************************************/
100
101/* Definition
102 * ----------
103 */
104
105class CVim : public IVim
106{
107public:
108 ~CVim();
Bram Moolenaar505e8282005-07-01 22:31:55 +0000109 static CVim *Create(int *pbDoRestart);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
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);
Bram Moolenaar505e8282005-07-01 22:31:55 +0000119 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);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122 // IVim members
123 STDMETHOD(SendKeys)(BSTR keys);
124 STDMETHOD(Eval)(BSTR expr, BSTR *result);
125 STDMETHOD(SetForeground)(void);
Bram Moolenaar0fde2902008-03-16 13:54:13 +0000126 STDMETHOD(GetHwnd)(UINT_PTR *result);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
128private:
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
Bram Moolenaar505e8282005-07-01 22:31:55 +0000143CVim *CVim::Create(int *pbDoRestart)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144{
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,
Bram Moolenaar460fbac2010-07-31 14:45:05 +0200169 REG_OPTION_NON_VOLATILE,
Bram Moolenaara621a032010-08-01 13:25:05 +0200170 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &hKey, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 {
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
215CVim::~CVim()
216{
217 if (typeinfo && vim_parent_hwnd == NULL)
218 typeinfo->Release();
Bram Moolenaar505e8282005-07-01 22:31:55 +0000219 typeinfo = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220}
221
222STDMETHODIMP
223CVim::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
236STDMETHODIMP_(ULONG)
237CVim::AddRef()
238{
239 return ++ref;
240}
241
242STDMETHODIMP_(ULONG)
243CVim::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
253STDMETHODIMP
254CVim::GetTypeInfoCount(UINT *pCount)
255{
256 *pCount = 1;
257 return S_OK;
258}
259
260STDMETHODIMP
261CVim::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
273STDMETHODIMP
274CVim::GetIDsOfNames(
Bram Moolenaar505e8282005-07-01 22:31:55 +0000275 const IID &iid,
276 OLECHAR **names,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 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
287STDMETHODIMP
288CVim::Invoke(
289 DISPID member,
Bram Moolenaar505e8282005-07-01 22:31:55 +0000290 const IID &iid,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 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
307STDMETHODIMP
Bram Moolenaar0fde2902008-03-16 13:54:13 +0000308CVim::GetHwnd(UINT_PTR *result)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309{
Bram Moolenaar0fde2902008-03-16 13:54:13 +0000310 *result = (UINT_PTR)s_hwnd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 return S_OK;
312}
313
314STDMETHODIMP
315CVim::SetForeground(void)
316{
317 /* Make the Vim window come to the foreground */
318 gui_mch_set_foreground();
319 return S_OK;
320}
321
322STDMETHODIMP
323CVim::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> */
Bram Moolenaar9c102382006-05-03 21:26:49 +0000346 str = replace_termcodes((char_u *)buffer, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
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
Bram Moolenaar370feaf2009-01-28 13:18:26 +0000367 * the message is processed. Except for an empty message, we don't need
368 * to post it then.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 */
Bram Moolenaar370feaf2009-01-28 13:18:26 +0000370 if (*str == NUL)
371 vim_free(str);
372 else
373 PostMessage(NULL, WM_OLE, 0, (LPARAM)str);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375 return S_OK;
376}
377
378STDMETHODIMP
379CVim::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;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000404 str = (char *)eval_to_string((char_u *)buffer, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 --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
440class CVimCF : public IClassFactory
441{
442public:
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
451private:
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
463CVimCF *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
473STDMETHODIMP
474CVimCF::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
487STDMETHODIMP_(ULONG)
488CVimCF::AddRef()
489{
490 return ++ref;
491}
492
493STDMETHODIMP_(ULONG)
494CVimCF::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
Bram Moolenaarbac97eb2005-06-13 22:12:09 +0000504/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505STDMETHODIMP
506CVimCF::CreateInstance(IUnknown *punkOuter, REFIID riid, void **ppv)
507{
508 return app->QueryInterface(riid, ppv);
509}
510
Bram Moolenaarbac97eb2005-06-13 22:12:09 +0000511/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512STDMETHODIMP
513CVimCF::LockServer(BOOL lock)
514{
515 return S_OK;
516}
517
518/*****************************************************************************
519 4. Registry manipulation code
520*****************************************************************************/
521
522// Internal use only
Bram Moolenaar505e8282005-07-01 22:31:55 +0000523static void SetKeyAndValue(const char *path, const char *subkey, const char *value);
524static void GUIDtochar(const GUID &guid, char *GUID, int length);
525static void RecursiveDeleteKey(HKEY hKeyParent, const char *child);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526static 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
531extern "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.
603extern "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
Bram Moolenaar505e8282005-07-01 22:31:55 +0000644static void GUIDtochar(const GUID &guid, char *GUID, int length)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645{
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
Bram Moolenaar505e8282005-07-01 22:31:55 +0000658static void RecursiveDeleteKey(HKEY hKeyParent, const char *child)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659{
660 // Open the child
661 HKEY hKeyChild;
Bram Moolenaar760d14a2010-07-31 22:03:44 +0200662 LONG result = RegOpenKeyEx(hKeyParent, child, 0,
Bram Moolenaara621a032010-08-01 13:25:05 +0200663 KEY_WOW64_64KEY | KEY_ALL_ACCESS, &hKeyChild);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 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
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000688static void SetKeyAndValue(const char *key, const char *subkey, const char *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689{
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,
Bram Moolenaara621a032010-08-01 13:25:05 +0200706 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 &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*****************************************************************************/
Bram Moolenaar505e8282005-07-01 22:31:55 +0000722extern "C" void InitOLE(int *pbDoRestart)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723{
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
776error1:
777 UninitOLE();
778error0:
779 return;
780}
781
782extern "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 */