Skip to content

Instantly share code, notes, and snippets.

@mattn
Created September 29, 2015 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattn/26f7aeb8b888df92ddbd to your computer and use it in GitHub Desktop.
Save mattn/26f7aeb8b888df92ddbd to your computer and use it in GitHub Desktop.
diff --git a/src/os_win32.c b/src/os_win32.c
index 6981d2f..57feae6 100644
--- a/src/os_win32.c
+++ b/src/os_win32.c
@@ -4614,18 +4614,31 @@ mch_system(char *cmd, int options)
int ret;
/*
- * Restore non-termcap screen buffer before execute external program, and
- * revert it after. Because msys and msys2's programs will cause freeze
- * or crash conhost.exe (Windows's console window provider) and vim.exe,
- * if active screen buffer is vim's one on Windows7.
+ * Call DuplicateHandle before execute external program. Because msys and
+ * msys2's programs will call CreateConsoleScreenBuffer and CloseHandle.
+ * CreateConsoleScreenBuffer return same handle which created by vim.
+ * So it occurs crash. This workaround is required on Windows7.
*/
+ HANDLE hTemp = INVALID_HANDLE_VALUE;
if (is_win7 && g_fTermcapMode)
- SetConsoleActiveScreenBuffer(g_cbNonTermcap.handle);
+ {
+ if (DuplicateHandle(
+ GetCurrentProcess(),
+ g_hConOut,
+ GetCurrentProcess(),
+ &hTemp,
+ 0,
+ TRUE,DUPLICATE_SAME_ACCESS)) {
+ SetConsoleActiveScreenBuffer(hTemp);
+ }
+ }
ret = mch_system1(cmd, options);
- if (is_win7 && g_fTermcapMode)
- SetConsoleActiveScreenBuffer(g_cbTermcap.handle);
+ if (hTemp != INVALID_HANDLE_VALUE) {
+ SetConsoleActiveScreenBuffer(g_hConOut);
+ CloseHandle(hTemp);
+ }
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment