[Ttssh2-commit] [8428] pGetConsoleWindow() をcompat_winに追加

scmno****@osdn***** scmno****@osdn*****
2019年 12月 2日 (月) 00:07:57 JST


Revision: 8428
          https://osdn.net/projects/ttssh2/scm/svn/commits/8428
Author:   zmatsuo
Date:     2019-12-02 00:07:57 +0900 (Mon, 02 Dec 2019)
Log Message:
-----------
pGetConsoleWindow() をcompat_winに追加

- GetConsoleWindow() コンソールウィンドウのハンドルを取得

Modified Paths:
--------------
    branches/unicode_buf/teraterm/common/compat_win.cpp
    branches/unicode_buf/teraterm/common/compat_win.h
    branches/unicode_buf/teraterm/teraterm/debug_pp.cpp

-------------- next part --------------
Modified: branches/unicode_buf/teraterm/common/compat_win.cpp
===================================================================
--- branches/unicode_buf/teraterm/common/compat_win.cpp	2019-11-30 15:31:58 UTC (rev 8427)
+++ branches/unicode_buf/teraterm/common/compat_win.cpp	2019-12-01 15:07:57 UTC (rev 8428)
@@ -63,7 +63,47 @@
 HRESULT (WINAPI *pGetDpiForMonitor)(HMONITOR hmonitor, MONITOR_DPI_TYPE dpiType, UINT *dpiX, UINT *dpiY);
 HMONITOR (WINAPI *pMonitorFromRect)(LPCRECT lprc, DWORD dwFlags);
 BOOL (WINAPI *pAdjustWindowRectExForDpi)(LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle, UINT dpi);
+HWND (WINAPI *pGetConsoleWindow)(void);
 
+/**
+ *	GetConsoleWindow() \x82Ɠ\xAF\x82\xB6\x93\xAE\x8D\xEC\x82\xF0\x82\xB7\x82\xE9
+ *	 https://support.microsoft.com/ja-jp/help/124103/how-to-obtain-a-console-window-handle-hwnd
+ */
+static HWND WINAPI GetConsoleWindowLocal(void)
+{
+#define MY_BUFSIZE 1024					 // Buffer size for console window titles.
+	HWND hwndFound;						 // This is what is returned to the caller.
+	char pszNewWindowTitle[MY_BUFSIZE];  // Contains fabricated WindowTitle.
+	char pszOldWindowTitle[MY_BUFSIZE];  // Contains original WindowTitle.
+
+	// Fetch current window title.
+	DWORD size = GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
+	if (size == 0) {
+		DWORD err = GetLastError();
+		if (err == ERROR_INVALID_HANDLE) {
+			// \x83R\x83\x93\x83\\x81[\x83\x8B\x82\xAA\x8AJ\x82\xA2\x82Ă\xA2\x82Ȃ\xA2
+			return NULL;
+		}
+	}
+
+	// Format a "unique" NewWindowTitle.
+	wsprintf(pszNewWindowTitle, "%d/%d", GetTickCount(), GetCurrentProcessId());
+
+	// Change current window title.
+	SetConsoleTitle(pszNewWindowTitle);
+
+	// Ensure window title has been updated.
+	Sleep(40);
+
+	// Look for NewWindowTitle.
+	hwndFound = FindWindow(NULL, pszNewWindowTitle);
+
+	// Restore original window title.
+	SetConsoleTitle(pszOldWindowTitle);
+
+	return hwndFound;
+}
+
 static const APIInfo Lists_user32[] = {
 	{ "SetLayeredWindowAttributes", (void **)&pSetLayeredWindowAttributes },
 	{ "SetThreadDpiAwarenessContext", (void **)&pSetThreadDpiAwarenessContext },
@@ -101,6 +141,7 @@
 static const APIInfo Lists_kernel32[] = {
 	{ "GetFileAttributesW", (void **)&pGetFileAttributesW },
 	{ "GetPrivateProfileStringW", (void **)&pGetPrivateProfileStringW },
+	{ "GetConsoleWindow", (void **)&pGetConsoleWindow },
 	{},
 };
 
@@ -143,5 +184,9 @@
 		pSetDlgItemTextW = NULL;
 		pGetDlgItemTextW = NULL;
 	}
+
+	// GetConsoleWindow\x93\xC1\x95ʏ\x88\x97\x9D
+	if (pGetConsoleWindow == NULL) {
+		pGetConsoleWindow = GetConsoleWindowLocal;
+	}
 }
-

Modified: branches/unicode_buf/teraterm/common/compat_win.h
===================================================================
--- branches/unicode_buf/teraterm/common/compat_win.h	2019-11-30 15:31:58 UTC (rev 8427)
+++ branches/unicode_buf/teraterm/common/compat_win.h	2019-12-01 15:07:57 UTC (rev 8428)
@@ -96,6 +96,7 @@
 extern int (WINAPI *pAddFontResourceExW)(LPCWSTR name, DWORD fl, PVOID res);
 extern BOOL (WINAPI *pRemoveFontResourceExA)(LPCSTR name, DWORD fl, PVOID pdv);
 extern BOOL (WINAPI *pRemoveFontResourceExW)(LPCWSTR name, DWORD fl, PVOID pdv);
+extern HWND (WINAPI *pGetConsoleWindow)(void);
 
 #ifdef UNICODE
 #define pAddFontResourceEx		pAddFontResourceExW

Modified: branches/unicode_buf/teraterm/teraterm/debug_pp.cpp
===================================================================
--- branches/unicode_buf/teraterm/teraterm/debug_pp.cpp	2019-11-30 15:31:58 UTC (rev 8427)
+++ branches/unicode_buf/teraterm/teraterm/debug_pp.cpp	2019-12-01 15:07:57 UTC (rev 8428)
@@ -36,6 +36,7 @@
 #include "../common/tt_res.h"
 #include "unicode_test.h"
 #include "dlglib.h"
+#include "compat_win.h"
 
 CDebugPropPage::CDebugPropPage(HINSTANCE inst, TTCPropertySheet *sheet)
 	: TTCPropertyPage(inst, IDD_TABSHEET_DEBUG, sheet)
@@ -59,7 +60,6 @@
 	// popup
 	SetCheck(IDC_DEBUG_POPUP_ENABLE, UnicodeDebugParam.CodePopupEnable);
 	for (int i = 0; i < _countof(key_list); i++) {
-		const char *key_str = key_list[i].key_str;
 		SendDlgItemMessage(IDC_DEBUG_POPUP_KEY1, CB_ADDSTRING, 0, (LPARAM)key_list[i].key_str);
 		SendDlgItemMessage(IDC_DEBUG_POPUP_KEY2, CB_ADDSTRING, 0, (LPARAM)key_list[i].key_str);
 		if (UnicodeDebugParam.CodePopupKey1 == key_list[i].key_code) {
@@ -72,7 +72,7 @@
 
 	// console button
 	const char *caption;
-	HWND hWnd = GetConsoleWindow();
+	HWND hWnd = pGetConsoleWindow();
 	if (hWnd == NULL) {
 		caption = "Open console window";
 	} else {
@@ -85,12 +85,12 @@
 	SetDlgItemTextA(IDC_DEBUG_CONSOLE_BUTTON, caption);
 }
 
-BOOL CDebugPropPage::OnCommand(WPARAM wParam, LPARAM lParam)
+BOOL CDebugPropPage::OnCommand(WPARAM wParam, LPARAM)
 {
 	switch (wParam) {
 		case IDC_DEBUG_CONSOLE_BUTTON | (BN_CLICKED << 16): {
 			const char *caption;
-			HWND hWnd = GetConsoleWindow();
+			HWND hWnd = pGetConsoleWindow();
 			if (hWnd == NULL) {
 				FILE *fp;
 				AllocConsole();
@@ -97,7 +97,7 @@
 				freopen_s(&fp, "CONOUT$", "w", stdout);
 				freopen_s(&fp, "CONOUT$", "w", stderr);
 				caption = "Hide console window";
-				HWND hWnd = GetConsoleWindow();
+				hWnd = pGetConsoleWindow();
 				HMENU hmenu = GetSystemMenu(hWnd, FALSE);
 				RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
 			}


Ttssh2-commit メーリングリストの案内