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