blob: e58b142555c57153105d0ac1adc846fc0136874d [file] [log] [blame]
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved gvimext by Tianmiao Hu
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/*
10 * gvimext is a DLL which is used for the "Edit with Vim" context menu
11 * extension. It implements a MS defined interface with the Shell.
12 *
13 * If you have any questions or any suggestions concerning gvimext, please
14 * contact Tianmiao Hu: tianmiao@acm.org.
15 */
16
17#include "gvimext.h"
18
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000019static char *searchpath(char *name);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000020
21// Always get an error while putting the following stuff to the
22// gvimext.h file as class protected variables, give up and
23// declare them as global stuff
24FORMATETC fmte = {CF_HDROP,
25 (DVTARGETDEVICE FAR *)NULL,
26 DVASPECT_CONTENT,
27 -1,
28 TYMED_HGLOBAL
29 };
30STGMEDIUM medium;
31HRESULT hres = 0;
32UINT cbFiles = 0;
33
Bram Moolenaar6199d432017-10-14 19:05:44 +020034/* The buffers size used to be MAX_PATH (260 bytes), but that's not always
Bram Moolenaare759a7a2005-07-12 22:50:18 +000035 * enough */
36#define BUFSIZE 1100
37
msoyka-of-wharton83cd0152021-07-29 19:18:33 +020038// The "Edit with Vim" shell extension provides these choices when
39// a new instance of Gvim is selected:
40// - use tabpages
41// - enable diff mode
42// - none of the above
43#define EDIT_WITH_VIM_USE_TABPAGES (2)
44#define EDIT_WITH_VIM_IN_DIFF_MODE (1)
45#define EDIT_WITH_VIM_NO_OPTIONS (0)
46
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000047//
48// Get the name of the Gvim executable to use, with the path.
49// When "runtime" is non-zero, we were called to find the runtime directory.
Bram Moolenaare759a7a2005-07-12 22:50:18 +000050// Returns the path in name[BUFSIZE]. It's empty when it fails.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000051//
52 static void
53getGvimName(char *name, int runtime)
54{
55 HKEY keyhandle;
56 DWORD hlen;
57
58 // Get the location of gvim from the registry.
59 name[0] = 0;
60 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Vim\\Gvim", 0,
61 KEY_READ, &keyhandle) == ERROR_SUCCESS)
62 {
Bram Moolenaare759a7a2005-07-12 22:50:18 +000063 hlen = BUFSIZE;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000064 if (RegQueryValueEx(keyhandle, "path", 0, NULL, (BYTE *)name, &hlen)
65 != ERROR_SUCCESS)
66 name[0] = 0;
67 else
68 name[hlen] = 0;
69 RegCloseKey(keyhandle);
70 }
71
72 // Registry didn't work, use the search path.
73 if (name[0] == 0)
Bram Moolenaar7baa45d2007-08-18 15:00:42 +000074 strcpy(name, searchpath((char *)"gvim.exe"));
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000075
76 if (!runtime)
77 {
78 // Only when looking for the executable, not the runtime dir, we can
79 // search for the batch file or a name without a path.
80 if (name[0] == 0)
Bram Moolenaar7baa45d2007-08-18 15:00:42 +000081 strcpy(name, searchpath((char *)"gvim.bat"));
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000082 if (name[0] == 0)
83 strcpy(name, "gvim"); // finds gvim.bat or gvim.exe
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000084 }
85}
86
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +020087 static void
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +020088getGvimInvocation(char *name, int runtime)
89{
90 getGvimName(name, runtime);
91 // avoid that Vim tries to expand wildcards in the file names
92 strcat(name, " --literal");
93}
94
95 static void
96getGvimInvocationW(wchar_t *nameW)
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +020097{
98 char *name;
99
100 name = (char *)malloc(BUFSIZE);
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200101 getGvimInvocation(name, 0);
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200102 mbstowcs(nameW, name, BUFSIZE);
103 free(name);
104}
105
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000106//
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000107// Get the Vim runtime directory into buf[BUFSIZE].
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000108// The result is empty when it failed.
109// When it works, the path ends in a slash or backslash.
110//
111 static void
112getRuntimeDir(char *buf)
113{
114 int idx;
115
116 getGvimName(buf, 1);
117 if (buf[0] != 0)
118 {
119 // When no path found, use the search path to expand it.
120 if (strchr(buf, '/') == NULL && strchr(buf, '\\') == NULL)
121 strcpy(buf, searchpath(buf));
122
123 // remove "gvim.exe" from the end
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000124 for (idx = (int)strlen(buf) - 1; idx >= 0; idx--)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000125 if (buf[idx] == '\\' || buf[idx] == '/')
126 {
127 buf[idx + 1] = 0;
128 break;
129 }
130 }
131}
132
K.Takata12715722023-05-25 16:43:27 +0100133 WCHAR *
134utf8_to_utf16(const char *s)
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200135{
K.Takata12715722023-05-25 16:43:27 +0100136 int size = MultiByteToWideChar(CP_UTF8, 0, s, -1, NULL, 0);
137 WCHAR *buf = (WCHAR *)malloc(size * sizeof(WCHAR));
138 MultiByteToWideChar(CP_UTF8, 0, s, -1, buf, size);
139 return buf;
140}
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200141
K.Takata12715722023-05-25 16:43:27 +0100142 HBITMAP
143IconToBitmap(HICON hIcon, HBRUSH hBackground, int width, int height)
144{
145 HDC hDC = GetDC(NULL);
146 HDC hMemDC = CreateCompatibleDC(hDC);
147 HBITMAP hMemBmp = CreateCompatibleBitmap(hDC, width, height);
148 HBITMAP hResultBmp = NULL;
149 HGDIOBJ hOrgBMP = SelectObject(hMemDC, hMemBmp);
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200150
K.Takata12715722023-05-25 16:43:27 +0100151 DrawIconEx(hMemDC, 0, 0, hIcon, width, height, 0, hBackground, DI_NORMAL);
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200152
K.Takata12715722023-05-25 16:43:27 +0100153 hResultBmp = hMemBmp;
154 hMemBmp = NULL;
155
156 SelectObject(hMemDC, hOrgBMP);
157 DeleteDC(hMemDC);
158 ReleaseDC(NULL, hDC);
159 DestroyIcon(hIcon);
160 return hResultBmp;
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200161}
162
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000163//
164// GETTEXT: translated messages and menu entries
165//
166#ifndef FEAT_GETTEXT
K.Takata12715722023-05-25 16:43:27 +0100167# define _(x) x
168# define W_impl(x) _wcsdup(L##x)
169# define W(x) W_impl(x)
170# define set_gettext_codeset() NULL
171# define restore_gettext_codeset(x)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000172#else
K.Takata12715722023-05-25 16:43:27 +0100173# define _(x) (*dyn_libintl_gettext)(x)
174# define W(x) utf8_to_utf16(x)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000175# define VIMPACKAGE "vim"
176# ifndef GETTEXT_DLL
177# define GETTEXT_DLL "libintl.dll"
Bram Moolenaar271273c2016-02-21 20:30:22 +0100178# define GETTEXT_DLL_ALT "libintl-8.dll"
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000179# endif
180
181// Dummy functions
182static char *null_libintl_gettext(const char *);
183static char *null_libintl_textdomain(const char *);
184static char *null_libintl_bindtextdomain(const char *, const char *);
K.Takata12715722023-05-25 16:43:27 +0100185static char *null_libintl_bind_textdomain_codeset(const char *, const char *);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000186static int dyn_libintl_init(char *dir);
187static void dyn_libintl_end(void);
188
189static HINSTANCE hLibintlDLL = 0;
190static char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext;
191static char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain;
192static char *(*dyn_libintl_bindtextdomain)(const char *, const char *)
193 = null_libintl_bindtextdomain;
K.Takata12715722023-05-25 16:43:27 +0100194static char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *)
195 = null_libintl_bind_textdomain_codeset;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000196
197//
198// Attempt to load libintl.dll. If it doesn't work, use dummy functions.
199// "dir" is the directory where the libintl.dll might be.
200// Return 1 for success, 0 for failure.
201//
202 static int
203dyn_libintl_init(char *dir)
204{
205 int i;
206 static struct
207 {
208 char *name;
209 FARPROC *ptr;
210 } libintl_entry[] =
211 {
Bram Moolenaar7baa45d2007-08-18 15:00:42 +0000212 {(char *)"gettext", (FARPROC*)&dyn_libintl_gettext},
213 {(char *)"textdomain", (FARPROC*)&dyn_libintl_textdomain},
214 {(char *)"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain},
K.Takata12715722023-05-25 16:43:27 +0100215 {(char *)"bind_textdomain_codeset", (FARPROC*)&dyn_libintl_bind_textdomain_codeset},
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000216 {NULL, NULL}
217 };
Bram Moolenaar271273c2016-02-21 20:30:22 +0100218 DWORD len, len2;
219 LPWSTR buf = NULL;
220 LPWSTR buf2 = NULL;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000221
222 // No need to initialize twice.
223 if (hLibintlDLL)
224 return 1;
225
Bram Moolenaar6199d432017-10-14 19:05:44 +0200226 // Load gettext library from $VIMRUNTIME\GvimExt{64,32} directory.
Bram Moolenaar271273c2016-02-21 20:30:22 +0100227 // Add the directory to $PATH temporarily.
228 len = GetEnvironmentVariableW(L"PATH", NULL, 0);
229 len2 = MAX_PATH + 1 + len;
230 buf = (LPWSTR)malloc(len * sizeof(WCHAR));
231 buf2 = (LPWSTR)malloc(len2 * sizeof(WCHAR));
232 if (buf != NULL && buf2 != NULL)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000233 {
Bram Moolenaar271273c2016-02-21 20:30:22 +0100234 GetEnvironmentVariableW(L"PATH", buf, len);
Bram Moolenaar38231922020-11-18 15:30:09 +0100235# ifdef _WIN64
Bram Moolenaar6199d432017-10-14 19:05:44 +0200236 _snwprintf(buf2, len2, L"%S\\GvimExt64;%s", dir, buf);
Bram Moolenaar38231922020-11-18 15:30:09 +0100237# else
Bram Moolenaar6199d432017-10-14 19:05:44 +0200238 _snwprintf(buf2, len2, L"%S\\GvimExt32;%s", dir, buf);
Bram Moolenaar38231922020-11-18 15:30:09 +0100239# endif
Bram Moolenaar271273c2016-02-21 20:30:22 +0100240 SetEnvironmentVariableW(L"PATH", buf2);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000241 hLibintlDLL = LoadLibrary(GETTEXT_DLL);
Bram Moolenaar38231922020-11-18 15:30:09 +0100242# ifdef GETTEXT_DLL_ALT
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000243 if (!hLibintlDLL)
Bram Moolenaar271273c2016-02-21 20:30:22 +0100244 hLibintlDLL = LoadLibrary(GETTEXT_DLL_ALT);
Bram Moolenaar38231922020-11-18 15:30:09 +0100245# endif
Bram Moolenaar271273c2016-02-21 20:30:22 +0100246 SetEnvironmentVariableW(L"PATH", buf);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000247 }
Bram Moolenaar271273c2016-02-21 20:30:22 +0100248 free(buf);
249 free(buf2);
250 if (!hLibintlDLL)
251 return 0;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000252
253 // Get the addresses of the functions we need.
254 for (i = 0; libintl_entry[i].name != NULL
255 && libintl_entry[i].ptr != NULL; ++i)
256 {
257 if ((*libintl_entry[i].ptr = GetProcAddress(hLibintlDLL,
258 libintl_entry[i].name)) == NULL)
259 {
260 dyn_libintl_end();
261 return 0;
262 }
263 }
264 return 1;
265}
266
267 static void
268dyn_libintl_end(void)
269{
270 if (hLibintlDLL)
271 FreeLibrary(hLibintlDLL);
272 hLibintlDLL = NULL;
273 dyn_libintl_gettext = null_libintl_gettext;
274 dyn_libintl_textdomain = null_libintl_textdomain;
275 dyn_libintl_bindtextdomain = null_libintl_bindtextdomain;
K.Takata12715722023-05-25 16:43:27 +0100276 dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000277}
278
279 static char *
280null_libintl_gettext(const char *msgid)
281{
282 return (char *)msgid;
283}
284
285 static char *
K.Takata12715722023-05-25 16:43:27 +0100286null_libintl_textdomain(const char * /* domainname */)
287{
288 return NULL;
289}
290
291 static char *
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000292null_libintl_bindtextdomain(const char * /* domainname */, const char * /* dirname */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000293{
294 return NULL;
295}
296
297 static char *
K.Takata12715722023-05-25 16:43:27 +0100298null_libintl_bind_textdomain_codeset(const char * /* domainname */, const char * /* codeset */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000299{
300 return NULL;
301}
302
303//
304// Setup for translating strings.
305//
306 static void
307dyn_gettext_load(void)
308{
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000309 char szBuff[BUFSIZE];
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000310 DWORD len;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000311
312 // Try to locate the runtime files. The path is used to find libintl.dll
313 // and the vim.mo files.
314 getRuntimeDir(szBuff);
315 if (szBuff[0] != 0)
316 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000317 len = (DWORD)strlen(szBuff);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000318 if (dyn_libintl_init(szBuff))
319 {
320 strcpy(szBuff + len, "lang");
321
322 (*dyn_libintl_bindtextdomain)(VIMPACKAGE, szBuff);
323 (*dyn_libintl_textdomain)(VIMPACKAGE);
324 }
325 }
326}
327
328 static void
329dyn_gettext_free(void)
330{
331 dyn_libintl_end();
332}
K.Takata12715722023-05-25 16:43:27 +0100333
334//
335// Use UTF-8 for gettext. Returns previous codeset.
336//
337 static char *
338set_gettext_codeset(void)
339{
340 char *prev = dyn_libintl_bind_textdomain_codeset(VIMPACKAGE, NULL);
341 prev = _strdup((prev != NULL) ? prev : "char");
342 dyn_libintl_bind_textdomain_codeset(VIMPACKAGE, "utf-8");
343
344 return prev;
345}
346
347//
348// Restore previous codeset for gettext.
349//
350 static void
351restore_gettext_codeset(char *prev)
352{
353 dyn_libintl_bind_textdomain_codeset(VIMPACKAGE, prev);
354 free(prev);
355}
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000356#endif // FEAT_GETTEXT
357
358//
359// Global variables
360//
361UINT g_cRefThisDll = 0; // Reference count of this DLL.
362HINSTANCE g_hmodThisDll = NULL; // Handle to this DLL itself.
363
364
365//---------------------------------------------------------------------------
366// DllMain
367//---------------------------------------------------------------------------
368extern "C" int APIENTRY
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000369DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /* lpReserved */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000370{
371 switch (dwReason)
372 {
373 case DLL_PROCESS_ATTACH:
374 // Extension DLL one-time initialization
375 g_hmodThisDll = hInstance;
376 break;
377
378 case DLL_PROCESS_DETACH:
379 break;
380 }
381
382 return 1; // ok
383}
384
385 static void
386inc_cRefThisDLL()
387{
388#ifdef FEAT_GETTEXT
Bram Moolenaar38231922020-11-18 15:30:09 +0100389 if (g_cRefThisDll == 0)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000390 dyn_gettext_load();
391#endif
392 InterlockedIncrement((LPLONG)&g_cRefThisDll);
393}
394
395 static void
396dec_cRefThisDLL()
397{
398#ifdef FEAT_GETTEXT
Bram Moolenaar38231922020-11-18 15:30:09 +0100399 if (InterlockedDecrement((LPLONG)&g_cRefThisDll) == 0)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000400 dyn_gettext_free();
401#else
402 InterlockedDecrement((LPLONG)&g_cRefThisDll);
403#endif
404}
405
406//---------------------------------------------------------------------------
407// DllCanUnloadNow
408//---------------------------------------------------------------------------
409
410STDAPI DllCanUnloadNow(void)
411{
412 return (g_cRefThisDll == 0 ? S_OK : S_FALSE);
413}
414
415STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppvOut)
416{
417 *ppvOut = NULL;
418
419 if (IsEqualIID(rclsid, CLSID_ShellExtension))
420 {
421 CShellExtClassFactory *pcf = new CShellExtClassFactory;
422
423 return pcf->QueryInterface(riid, ppvOut);
424 }
425
426 return CLASS_E_CLASSNOTAVAILABLE;
427}
428
429CShellExtClassFactory::CShellExtClassFactory()
430{
431 m_cRef = 0L;
432
433 inc_cRefThisDLL();
434}
435
436CShellExtClassFactory::~CShellExtClassFactory()
437{
438 dec_cRefThisDLL();
439}
440
441STDMETHODIMP CShellExtClassFactory::QueryInterface(REFIID riid,
442 LPVOID FAR *ppv)
443{
444 *ppv = NULL;
445
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200446 // any interface on this object is the object pointer
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000447
448 if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory))
449 {
450 *ppv = (LPCLASSFACTORY)this;
451
452 AddRef();
453
454 return NOERROR;
455 }
456
457 return E_NOINTERFACE;
458}
459
460STDMETHODIMP_(ULONG) CShellExtClassFactory::AddRef()
461{
462 return InterlockedIncrement((LPLONG)&m_cRef);
463}
464
465STDMETHODIMP_(ULONG) CShellExtClassFactory::Release()
466{
467 if (InterlockedDecrement((LPLONG)&m_cRef))
468 return m_cRef;
469
470 delete this;
471
472 return 0L;
473}
474
475STDMETHODIMP CShellExtClassFactory::CreateInstance(LPUNKNOWN pUnkOuter,
476 REFIID riid,
477 LPVOID *ppvObj)
478{
479 *ppvObj = NULL;
480
481 // Shell extensions typically don't support aggregation (inheritance)
482
483 if (pUnkOuter)
484 return CLASS_E_NOAGGREGATION;
485
486 // Create the main shell extension object. The shell will then call
487 // QueryInterface with IID_IShellExtInit--this is how shell extensions are
488 // initialized.
489
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200490 LPCSHELLEXT pShellExt = new CShellExt(); // create the CShellExt object
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000491
492 if (NULL == pShellExt)
493 return E_OUTOFMEMORY;
494
495 return pShellExt->QueryInterface(riid, ppvObj);
496}
497
498
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000499STDMETHODIMP CShellExtClassFactory::LockServer(BOOL /* fLock */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000500{
501 return NOERROR;
502}
503
504// *********************** CShellExt *************************
505CShellExt::CShellExt()
506{
507 m_cRef = 0L;
508 m_pDataObj = NULL;
509
510 inc_cRefThisDLL();
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200511
512 LoadMenuIcon();
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000513}
514
515CShellExt::~CShellExt()
516{
517 if (m_pDataObj)
518 m_pDataObj->Release();
519
520 dec_cRefThisDLL();
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200521
522 if (m_hVimIconBitmap)
523 DeleteObject(m_hVimIconBitmap);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000524}
525
526STDMETHODIMP CShellExt::QueryInterface(REFIID riid, LPVOID FAR *ppv)
527{
528 *ppv = NULL;
529
530 if (IsEqualIID(riid, IID_IShellExtInit) || IsEqualIID(riid, IID_IUnknown))
531 {
532 *ppv = (LPSHELLEXTINIT)this;
533 }
534 else if (IsEqualIID(riid, IID_IContextMenu))
535 {
536 *ppv = (LPCONTEXTMENU)this;
537 }
538
539 if (*ppv)
540 {
541 AddRef();
542
543 return NOERROR;
544 }
545
546 return E_NOINTERFACE;
547}
548
549STDMETHODIMP_(ULONG) CShellExt::AddRef()
550{
551 return InterlockedIncrement((LPLONG)&m_cRef);
552}
553
554STDMETHODIMP_(ULONG) CShellExt::Release()
555{
556
557 if (InterlockedDecrement((LPLONG)&m_cRef))
558 return m_cRef;
559
560 delete this;
561
562 return 0L;
563}
564
565
566//
567// FUNCTION: CShellExt::Initialize(LPCITEMIDLIST, LPDATAOBJECT, HKEY)
568//
569// PURPOSE: Called by the shell when initializing a context menu or property
570// sheet extension.
571//
572// PARAMETERS:
573// pIDFolder - Specifies the parent folder
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200574// pDataObj - Specifies the set of items selected in that folder.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000575// hRegKey - Specifies the type of the focused item in the selection.
576//
577// RETURN VALUE:
578//
579// NOERROR in all cases.
580//
581// COMMENTS: Note that at the time this function is called, we don't know
582// (or care) what type of shell extension is being initialized.
583// It could be a context menu or a property sheet.
584//
585
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000586STDMETHODIMP CShellExt::Initialize(LPCITEMIDLIST /* pIDFolder */,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000587 LPDATAOBJECT pDataObj,
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000588 HKEY /* hRegKey */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000589{
590 // Initialize can be called more than once
591 if (m_pDataObj)
592 m_pDataObj->Release();
593
594 // duplicate the object pointer and registry handle
595
596 if (pDataObj)
597 {
598 m_pDataObj = pDataObj;
599 pDataObj->AddRef();
600 }
601
602 return NOERROR;
603}
604
605
606//
607// FUNCTION: CShellExt::QueryContextMenu(HMENU, UINT, UINT, UINT, UINT)
608//
609// PURPOSE: Called by the shell just before the context menu is displayed.
610// This is where you add your specific menu items.
611//
612// PARAMETERS:
613// hMenu - Handle to the context menu
614// indexMenu - Index of where to begin inserting menu items
615// idCmdFirst - Lowest value for new menu ID's
616// idCmtLast - Highest value for new menu ID's
617// uFlags - Specifies the context of the menu event
618//
619// RETURN VALUE:
620//
621//
622// COMMENTS:
623//
624
625STDMETHODIMP CShellExt::QueryContextMenu(HMENU hMenu,
626 UINT indexMenu,
627 UINT idCmdFirst,
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000628 UINT /* idCmdLast */,
629 UINT /* uFlags */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000630{
631 UINT idCmd = idCmdFirst;
632
633 hres = m_pDataObj->GetData(&fmte, &medium);
634 if (medium.hGlobal)
K.Takata12715722023-05-25 16:43:27 +0100635 cbFiles = DragQueryFileW((HDROP)medium.hGlobal, (UINT)-1, 0, 0);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000636
637 // InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL);
638
639 // Initialize m_cntOfHWnd to 0
640 m_cntOfHWnd = 0;
Bram Moolenaarce35c882011-07-20 17:27:25 +0200641
642 HKEY keyhandle;
643 bool showExisting = true;
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200644 bool showIcons = true;
Bram Moolenaarce35c882011-07-20 17:27:25 +0200645
646 // Check whether "Edit with existing Vim" entries are disabled.
647 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Vim\\Gvim", 0,
648 KEY_READ, &keyhandle) == ERROR_SUCCESS)
649 {
650 if (RegQueryValueEx(keyhandle, "DisableEditWithExisting", 0, NULL,
651 NULL, NULL) == ERROR_SUCCESS)
652 showExisting = false;
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200653 if (RegQueryValueEx(keyhandle, "DisableContextMenuIcons", 0, NULL,
654 NULL, NULL) == ERROR_SUCCESS)
655 showIcons = false;
Bram Moolenaarce35c882011-07-20 17:27:25 +0200656 RegCloseKey(keyhandle);
657 }
658
K.Takata12715722023-05-25 16:43:27 +0100659 // Use UTF-8 for gettext.
660 char *prev = set_gettext_codeset();
661
Bram Moolenaarce35c882011-07-20 17:27:25 +0200662 // Retrieve all the vim instances, unless disabled.
663 if (showExisting)
664 EnumWindows(EnumWindowsProc, (LPARAM)this);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000665
K.Takata12715722023-05-25 16:43:27 +0100666 MENUITEMINFOW mii = { sizeof(MENUITEMINFOW) };
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200667 mii.fMask = MIIM_STRING | MIIM_ID;
668 if (showIcons)
669 {
670 mii.fMask |= MIIM_BITMAP;
671 mii.hbmpItem = m_hVimIconBitmap;
672 }
673
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000674 if (cbFiles > 1)
675 {
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200676 mii.wID = idCmd++;
K.Takata12715722023-05-25 16:43:27 +0100677 mii.dwTypeData = W(_("Edit with Vim using &tabpages"));
678 mii.cch = wcslen(mii.dwTypeData);
679 InsertMenuItemW(hMenu, indexMenu++, TRUE, &mii);
680 free(mii.dwTypeData);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000681
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200682 mii.wID = idCmd++;
K.Takata12715722023-05-25 16:43:27 +0100683 mii.dwTypeData = W(_("Edit with single &Vim"));
684 mii.cch = wcslen(mii.dwTypeData);
685 InsertMenuItemW(hMenu, indexMenu++, TRUE, &mii);
686 free(mii.dwTypeData);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000687
688 if (cbFiles <= 4)
689 {
690 // Can edit up to 4 files in diff mode
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200691 mii.wID = idCmd++;
K.Takata12715722023-05-25 16:43:27 +0100692 mii.dwTypeData = W(_("Diff with Vim"));
693 mii.cch = wcslen(mii.dwTypeData);
694 InsertMenuItemW(hMenu, indexMenu++, TRUE, &mii);
695 free(mii.dwTypeData);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000696 m_edit_existing_off = 3;
697 }
698 else
699 m_edit_existing_off = 2;
700
701 }
702 else
703 {
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200704 mii.wID = idCmd++;
K.Takata12715722023-05-25 16:43:27 +0100705 mii.dwTypeData = W(_("Edit with &Vim"));
706 mii.cch = wcslen(mii.dwTypeData);
707 InsertMenuItemW(hMenu, indexMenu++, TRUE, &mii);
708 free(mii.dwTypeData);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000709 m_edit_existing_off = 1;
710 }
711
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200712 HMENU hSubMenu = NULL;
713 if (m_cntOfHWnd > 1)
714 {
715 hSubMenu = CreatePopupMenu();
716 mii.fMask |= MIIM_SUBMENU;
717 mii.wID = idCmd;
K.Takata12715722023-05-25 16:43:27 +0100718 mii.dwTypeData = W(_("Edit with existing Vim"));
719 mii.cch = wcslen(mii.dwTypeData);
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200720 mii.hSubMenu = hSubMenu;
K.Takata12715722023-05-25 16:43:27 +0100721 InsertMenuItemW(hMenu, indexMenu++, TRUE, &mii);
722 free(mii.dwTypeData);
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200723 mii.fMask = mii.fMask & ~MIIM_SUBMENU;
724 mii.hSubMenu = NULL;
725 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000726 // Now display all the vim instances
727 for (int i = 0; i < m_cntOfHWnd; i++)
728 {
K.Takata12715722023-05-25 16:43:27 +0100729 WCHAR title[BUFSIZE];
730 WCHAR temp[BUFSIZE];
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200731 int index;
732 HMENU hmenu;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000733
734 // Obtain window title, continue if can not
K.Takata12715722023-05-25 16:43:27 +0100735 if (GetWindowTextW(m_hWnd[i], title, BUFSIZE - 1) == 0)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000736 continue;
737 // Truncate the title before the path, keep the file name
K.Takata12715722023-05-25 16:43:27 +0100738 WCHAR *pos = wcschr(title, L'(');
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000739 if (pos != NULL)
740 {
K.Takata12715722023-05-25 16:43:27 +0100741 if (pos > title && pos[-1] == L' ')
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000742 --pos;
743 *pos = 0;
744 }
745 // Now concatenate
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200746 if (m_cntOfHWnd > 1)
K.Takata12715722023-05-25 16:43:27 +0100747 temp[0] = L'\0';
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200748 else
749 {
K.Takata12715722023-05-25 16:43:27 +0100750 WCHAR *s = W(_("Edit with existing Vim - "));
751 wcsncpy(temp, s, BUFSIZE - 1);
752 temp[BUFSIZE - 1] = L'\0';
753 free(s);
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200754 }
K.Takata12715722023-05-25 16:43:27 +0100755 wcsncat(temp, title, BUFSIZE - 1 - wcslen(temp));
756 temp[BUFSIZE - 1] = L'\0';
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200757
758 mii.wID = idCmd++;
759 mii.dwTypeData = temp;
K.Takata12715722023-05-25 16:43:27 +0100760 mii.cch = wcslen(mii.dwTypeData);
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200761 if (m_cntOfHWnd > 1)
762 {
763 hmenu = hSubMenu;
764 index = i;
765 }
766 else
767 {
768 hmenu = hMenu;
769 index = indexMenu++;
770 }
K.Takata12715722023-05-25 16:43:27 +0100771 InsertMenuItemW(hmenu, index, TRUE, &mii);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000772 }
773 // InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL);
774
K.Takata12715722023-05-25 16:43:27 +0100775 // Restore previous codeset.
776 restore_gettext_codeset(prev);
777
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000778 // Must return number of menu items we added.
779 return ResultFromShort(idCmd-idCmdFirst);
780}
781
782//
783// FUNCTION: CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO)
784//
785// PURPOSE: Called by the shell after the user has selected on of the
786// menu items that was added in QueryContextMenu().
787//
788// PARAMETERS:
789// lpcmi - Pointer to an CMINVOKECOMMANDINFO structure
790//
791// RETURN VALUE:
792//
793//
794// COMMENTS:
795//
796
797STDMETHODIMP CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)
798{
799 HRESULT hr = E_INVALIDARG;
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200800 int gvimExtraOptions;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000801
802 // If HIWORD(lpcmi->lpVerb) then we have been called programmatically
803 // and lpVerb is a command that should be invoked. Otherwise, the shell
804 // has called us, and LOWORD(lpcmi->lpVerb) is the menu ID the user has
805 // selected. Actually, it's (menu ID - idCmdFirst) from QueryContextMenu().
806 if (!HIWORD(lpcmi->lpVerb))
807 {
808 UINT idCmd = LOWORD(lpcmi->lpVerb);
809
810 if (idCmd >= m_edit_existing_off)
811 {
812 // Existing with vim instance
813 hr = PushToWindow(lpcmi->hwnd,
814 lpcmi->lpDirectory,
815 lpcmi->lpVerb,
816 lpcmi->lpParameters,
817 lpcmi->nShow,
818 idCmd - m_edit_existing_off);
819 }
820 else
821 {
822 switch (idCmd)
823 {
824 case 0:
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200825 gvimExtraOptions = EDIT_WITH_VIM_USE_TABPAGES;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000826 break;
827 case 1:
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200828 gvimExtraOptions = EDIT_WITH_VIM_NO_OPTIONS;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000829 break;
830 case 2:
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200831 gvimExtraOptions = EDIT_WITH_VIM_IN_DIFF_MODE;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000832 break;
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200833 default:
834 // If execution reaches this point we likely have an
835 // inconsistency between the code that setup the menus
836 // and this code that determines what the user
837 // selected. This should be detected and fixed during
838 // development.
839 return E_FAIL;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000840 }
Nir Lichtman1aeccdb2021-12-22 15:21:15 +0000841
842 LPCMINVOKECOMMANDINFOEX lpcmiex = (LPCMINVOKECOMMANDINFOEX)lpcmi;
843 LPCWSTR currentDirectory = lpcmi->cbSize == sizeof(CMINVOKECOMMANDINFOEX) ? lpcmiex->lpDirectoryW : NULL;
844
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200845 hr = InvokeSingleGvim(lpcmi->hwnd,
Nir Lichtman1aeccdb2021-12-22 15:21:15 +0000846 currentDirectory,
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200847 lpcmi->lpVerb,
848 lpcmi->lpParameters,
849 lpcmi->nShow,
850 gvimExtraOptions);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000851 }
852 }
853 return hr;
854}
855
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000856STDMETHODIMP CShellExt::PushToWindow(HWND /* hParent */,
857 LPCSTR /* pszWorkingDir */,
858 LPCSTR /* pszCmd */,
859 LPCSTR /* pszParam */,
860 int /* iShowCmd */,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000861 int idHWnd)
862{
863 HWND hWnd = m_hWnd[idHWnd];
864
865 // Show and bring vim instance to foreground
866 if (IsIconic(hWnd) != 0)
867 ShowWindow(hWnd, SW_RESTORE);
868 else
869 ShowWindow(hWnd, SW_SHOW);
870 SetForegroundWindow(hWnd);
871
872 // Post the selected files to the vim instance
873 PostMessage(hWnd, WM_DROPFILES, (WPARAM)medium.hGlobal, 0);
874
875 return NOERROR;
876}
877
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000878STDMETHODIMP CShellExt::GetCommandString(UINT_PTR /* idCmd */,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000879 UINT uFlags,
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000880 UINT FAR * /* reserved */,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000881 LPSTR pszName,
882 UINT cchMax)
883{
K.Takata12715722023-05-25 16:43:27 +0100884 // Use UTF-8 for gettext.
885 char *prev = set_gettext_codeset();
886
887 WCHAR *s = W(_("Edits the selected file(s) with Vim"));
888 if (uFlags == GCS_HELPTEXTW && cchMax > wcslen(s))
889 wcscpy((WCHAR *)pszName, s);
890 free(s);
891
892 // Restore previous codeset.
893 restore_gettext_codeset(prev);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000894
895 return NOERROR;
896}
897
898BOOL CALLBACK CShellExt::EnumWindowsProc(HWND hWnd, LPARAM lParam)
899{
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000900 char temp[BUFSIZE];
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000901
902 // First do a bunch of check
903 // No invisible window
K.Takata12715722023-05-25 16:43:27 +0100904 if (!IsWindowVisible(hWnd))
905 return TRUE;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000906 // No child window ???
907 // if (GetParent(hWnd)) return TRUE;
908 // Class name should be Vim, if failed to get class name, return
909 if (GetClassName(hWnd, temp, sizeof(temp)) == 0)
910 return TRUE;
911 // Compare class name to that of vim, if not, return
912 if (_strnicmp(temp, "vim", sizeof("vim")) != 0)
913 return TRUE;
914 // First check if the number of vim instance exceeds MAX_HWND
915 CShellExt *cs = (CShellExt*) lParam;
K.Takata12715722023-05-25 16:43:27 +0100916 if (cs->m_cntOfHWnd >= MAX_HWND)
917 return FALSE; // stop enumeration
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000918 // Now we get the vim window, put it into some kind of array
919 cs->m_hWnd[cs->m_cntOfHWnd] = hWnd;
920 cs->m_cntOfHWnd ++;
921
922 return TRUE; // continue enumeration (otherwise this would be false)
923}
924
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200925BOOL CShellExt::LoadMenuIcon()
926{
K.Takata12715722023-05-25 16:43:27 +0100927 char vimExeFile[BUFSIZE];
928 getGvimName(vimExeFile, 1);
929 if (vimExeFile[0] == '\0')
930 return FALSE;
931 HICON hVimIcon;
932 if (ExtractIconEx(vimExeFile, 0, NULL, &hVimIcon, 1) == 0)
933 return FALSE;
934 m_hVimIconBitmap = IconToBitmap(hVimIcon,
935 GetSysColorBrush(COLOR_MENU),
936 GetSystemMetrics(SM_CXSMICON),
937 GetSystemMetrics(SM_CYSMICON));
938 return TRUE;
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200939}
940
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000941 static char *
942searchpath(char *name)
943{
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000944 static char widename[2 * BUFSIZE];
945 static char location[2 * BUFSIZE + 2];
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000946
947 // There appears to be a bug in FindExecutableA() on Windows NT.
948 // Use FindExecutableW() instead...
Bram Moolenaar6199d432017-10-14 19:05:44 +0200949 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)name, -1,
950 (LPWSTR)widename, BUFSIZE);
951 if (FindExecutableW((LPCWSTR)widename, (LPCWSTR)"",
952 (LPWSTR)location) > (HINSTANCE)32)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000953 {
Bram Moolenaar6199d432017-10-14 19:05:44 +0200954 WideCharToMultiByte(CP_ACP, 0, (LPWSTR)location, -1,
955 (LPSTR)widename, 2 * BUFSIZE, NULL, NULL);
956 return widename;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000957 }
Bram Moolenaar7baa45d2007-08-18 15:00:42 +0000958 return (char *)"";
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000959}
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000960
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000961
962STDMETHODIMP CShellExt::InvokeSingleGvim(HWND hParent,
Nir Lichtman1aeccdb2021-12-22 15:21:15 +0000963 LPCWSTR workingDir,
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000964 LPCSTR /* pszCmd */,
965 LPCSTR /* pszParam */,
966 int /* iShowCmd */,
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200967 int gvimExtraOptions)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000968{
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200969 wchar_t m_szFileUserClickedOn[BUFSIZE];
970 wchar_t *cmdStrW;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000971 size_t cmdlen;
972 size_t len;
973 UINT i;
974
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000975 cmdlen = BUFSIZE;
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200976 cmdStrW = (wchar_t *) malloc(cmdlen * sizeof(wchar_t));
Bram Moolenaar06d4c4c2018-12-14 19:20:02 +0100977 if (cmdStrW == NULL)
Bram Moolenaar142a9752018-12-14 19:54:39 +0100978 return E_FAIL;
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200979 getGvimInvocationW(cmdStrW);
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200980
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200981 if (gvimExtraOptions == EDIT_WITH_VIM_IN_DIFF_MODE)
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200982 wcscat(cmdStrW, L" -d");
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200983 else if (gvimExtraOptions == EDIT_WITH_VIM_USE_TABPAGES)
984 wcscat(cmdStrW, L" -p");
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000985 for (i = 0; i < cbFiles; i++)
986 {
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200987 DragQueryFileW((HDROP)medium.hGlobal,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000988 i,
989 m_szFileUserClickedOn,
990 sizeof(m_szFileUserClickedOn));
991
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200992 len = wcslen(cmdStrW) + wcslen(m_szFileUserClickedOn) + 4;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000993 if (len > cmdlen)
994 {
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000995 cmdlen = len + BUFSIZE;
Bram Moolenaar06d4c4c2018-12-14 19:20:02 +0100996 wchar_t *cmdStrW_new = (wchar_t *)realloc(cmdStrW, cmdlen * sizeof(wchar_t));
997 if (cmdStrW_new == NULL)
Bram Moolenaar142a9752018-12-14 19:54:39 +0100998 {
999 free(cmdStrW);
1000 return E_FAIL;
1001 }
Bram Moolenaar06d4c4c2018-12-14 19:20:02 +01001002 cmdStrW = cmdStrW_new;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001003 }
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +02001004 wcscat(cmdStrW, L" \"");
1005 wcscat(cmdStrW, m_szFileUserClickedOn);
1006 wcscat(cmdStrW, L"\"");
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001007 }
1008
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +02001009 STARTUPINFOW si;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001010 PROCESS_INFORMATION pi;
1011
1012 ZeroMemory(&si, sizeof(si));
1013 si.cb = sizeof(si);
1014
1015 // Start the child process.
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +02001016 if (!CreateProcessW(NULL, // No module name (use command line).
1017 cmdStrW, // Command line.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001018 NULL, // Process handle not inheritable.
1019 NULL, // Thread handle not inheritable.
1020 FALSE, // Set handle inheritance to FALSE.
Bram Moolenaar38231922020-11-18 15:30:09 +01001021 0, // No creation flags.
1022 NULL, // Use parent's environment block.
K.Takata12715722023-05-25 16:43:27 +01001023 workingDir, // Use parent's starting directory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001024 &si, // Pointer to STARTUPINFO structure.
1025 &pi) // Pointer to PROCESS_INFORMATION structure.
1026 )
1027 {
K.Takata12715722023-05-25 16:43:27 +01001028 // Use UTF-8 for gettext.
1029 char *prev = set_gettext_codeset();
1030
1031 WCHAR *msg = W(_("Error creating process: Check if gvim is in your path!"));
1032 WCHAR *title = W(_("gvimext.dll error"));
1033
1034 MessageBoxW(hParent, msg, title, MB_OK);
1035
1036 free(msg);
1037 free(title);
1038
1039 // Restore previous codeset.
1040 restore_gettext_codeset(prev);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001041 }
1042 else
1043 {
1044 CloseHandle(pi.hProcess);
1045 CloseHandle(pi.hThread);
1046 }
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +02001047 free(cmdStrW);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001048
1049 return NOERROR;
1050}