[Ttssh2-commit] [6100] USBシリアル検出処理の修正

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2015年 11月 9日 (月) 22:55:50 JST


Revision: 6100
          http://sourceforge.jp/projects/ttssh2/scm/svn/commits/6100
Author:   salarm
Date:     2015-11-09 22:55:50 +0900 (Mon, 09 Nov 2015)
Log Message:
-----------
USBシリアル検出処理の修正
・デバイスによってはWM_DEVICECHANGEが通知されないのでRegisterDeviceNotificationを呼び出す
・デバイスによっては切断が検出できないのでレジストリも見るように修正

Modified Paths:
--------------
    trunk/teraterm/teraterm/vtwin.cpp
    trunk/teraterm/ttpcmn/ttcmn.c

-------------- next part --------------
Modified: trunk/teraterm/teraterm/vtwin.cpp
===================================================================
--- trunk/teraterm/teraterm/vtwin.cpp	2015-11-09 13:39:01 UTC (rev 6099)
+++ trunk/teraterm/teraterm/vtwin.cpp	2015-11-09 13:55:50 UTC (rev 6100)
@@ -56,6 +56,9 @@
 #include "addsetting.h"
 #include "winjump.h"
 
+#include "initguid.h"
+#include "Usbiodef.h"
+
 #define VTClassName "VTWin32"
 
 #ifdef _DEBUG
@@ -81,6 +84,8 @@
 
 static BOOL IgnoreRelease = FALSE;
 
+static HDEVNOTIFY hDevNotify = NULL;
+
 static int AutoDisconnectedPort = -1;
 
 // \x96{\x91̂\xCD addsetting.cpp
@@ -310,7 +315,39 @@
 	}
 }
 
+void RegDeviceNotify(HWND hWnd)
+{
+	typedef HDEVNOTIFY (WINAPI *PRegisterDeviceNotification)(HANDLE hRecipient, LPVOID NotificationFilter, DWORD Flags);
+	HMODULE h;
+	PRegisterDeviceNotification pRegisterDeviceNotification;
+	DEV_BROADCAST_DEVICEINTERFACE filter;
 
+	if (((h = GetModuleHandle("user32.dll")) == NULL) ||
+			((pRegisterDeviceNotification = (PRegisterDeviceNotification)GetProcAddress(h, "RegisterDeviceNotificationA")) == NULL)) {
+		return;
+	}
+
+	ZeroMemory(&filter, sizeof(filter));
+	filter.dbcc_size = sizeof(filter);
+	filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
+	filter.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
+	hDevNotify = pRegisterDeviceNotification(hWnd, &filter, DEVICE_NOTIFY_WINDOW_HANDLE);
+}
+
+void UnRegDeviceNotify(HWND hWnd)
+{
+	typedef BOOL (WINAPI *PUnregisterDeviceNotification)(HDEVNOTIFY Handle);
+	HMODULE h;
+	PUnregisterDeviceNotification pUnregisterDeviceNotification;
+
+	if (((h = GetModuleHandle("user32.dll")) == NULL) ||
+			((pUnregisterDeviceNotification = (PUnregisterDeviceNotification)GetProcAddress(h, "UnregisterDeviceNotification")) == NULL)) {
+		return;
+	}
+
+	pUnregisterDeviceNotification(hDevNotify);
+}
+
 //
 // \x97\xE1\x8AO\x83n\x83\x93\x83h\x83\x89\x82̃t\x83b\x83N\x81i\x83X\x83^\x83b\x83N\x83g\x83\x8C\x81[\x83X\x82̃_\x83\x93\x83v\x81j
 //
@@ -698,6 +735,9 @@
 //-->
 #endif
 
+	// USB\x83f\x83o\x83C\x83X\x95ω\xBB\x92ʒm\x93o\x98^
+	RegDeviceNotify(HVTWin);
+
 	if (is_NT4()) {
 		fuLoad = LR_VGACOLOR;
 	}
@@ -1822,6 +1862,9 @@
 	// remove this window from the window list
 	UnregWin(HVTWin);
 
+	// USB\x83f\x83o\x83C\x83X\x95ω\xBB\x92ʒm\x89\xF0\x8F\x9C
+	UnRegDeviceNotify(HVTWin);
+
 	EndKeyboard();
 
 	/* Disable drag-drop */
@@ -2980,7 +3023,7 @@
 
 BOOL CVTWindow::OnDeviceChange(UINT nEventType, DWORD_PTR dwData)
 {
-	if (nEventType == DBT_DEVICEARRIVAL || nEventType ==DBT_DEVICEREMOVECOMPLETE) {
+	if (nEventType == DBT_DEVICEARRIVAL || nEventType == DBT_DEVICEREMOVECOMPLETE) {
 		if (ts.PortType == IdSerial) {
 			if (!ts.AutoComDisReConnect) {
 				return CFrameWnd::OnDeviceChange(nEventType, dwData);
@@ -2988,20 +3031,18 @@
 
 			if (cv.Open != 0) {
 				/* \x90ڑ\xB1\x92\x86 */
-				if (CheckComPort(cv.ComPort) == 0) {
+				if (AutoDisconnectedPort == -1 && CheckComPort(cv.ComPort) == 0) {
 					AutoDisconnectedPort = cv.ComPort;
 					Disconnect(TRUE);
-					return 0;
 				}
 			}
 			else {
 				/* \x96\xA2\x90ڑ\xB1 */
-				if (AutoDisconnectedPort == cv.ComPort && CheckComPort(cv.ComPort) == 1) {
+				if (AutoDisconnectedPort == ts.ComPort && CheckComPort(ts.ComPort) == 1) {
 					AutoDisconnectedPort = -1;
 					Connecting = TRUE;
 					ChangeTitle();
 					CommOpen(HVTWin, &ts, &cv);
-					return 0;
 				}
 			}
 		}

Modified: trunk/teraterm/ttpcmn/ttcmn.c
===================================================================
--- trunk/teraterm/ttpcmn/ttcmn.c	2015-11-09 13:39:01 UTC (rev 6099)
+++ trunk/teraterm/ttpcmn/ttcmn.c	2015-11-09 13:55:50 UTC (rev 6100)
@@ -2312,6 +2312,12 @@
 	HMODULE h;
 	TCHAR   devicesBuff[65535];
 	char    com_str[64];
+	BOOL bRet;
+	GUID ClassGuid[1];
+	DWORD dwRequiredSize;
+	HDEVINFO DeviceInfoSet = NULL;
+	SP_DEVINFO_DATA DeviceInfoData;
+	int found = 0;
 
 	_snprintf_s(com_str, sizeof(com_str), _TRUNCATE, "COM%d", ComPort);
 
@@ -2319,10 +2325,8 @@
 		/* ERROR */
 		return -1;
 	}
-	if (QueryDosDevice(com_str, devicesBuff, 65535) != 0) {
-		return 1;
-	}
-	else {
+
+	if (QueryDosDevice(com_str, devicesBuff, 65535) == 0) {
 		DWORD err = GetLastError();
 		if (err == ERROR_FILE_NOT_FOUND) {
 			/* NOT FOUND */
@@ -2331,6 +2335,45 @@
 		/* ERROR */
 		return -1;
 	}
+
+	/* QueryDosDevice\x82Őؒf\x82\xF0\x8C\x9F\x92m\x82ł\xAB\x82Ȃ\xA2\x8A‹\xAB\x82\xAA\x82\xA0\x82\xE9\x82ł\xB3\x82\xE7\x82Ƀ`\x83F\x83b\x83N */
+	bRet = SetupDiClassGuidsFromName(_T("PORTS"), (LPGUID) & ClassGuid, 1, &dwRequiredSize);
+	if (bRet == FALSE) {
+		return -1;
+	}
+
+	DeviceInfoSet = SetupDiGetClassDevs(&ClassGuid[0], NULL, NULL, DIGCF_PRESENT | DIGCF_PROFILE);
+	if (DeviceInfoSet == NULL) {
+		return -1;
+	}
+
+	if (DeviceInfoSet) {
+		DWORD dwMemberIndex = 0;
+		HKEY hKey = NULL;
+		TCHAR szPortName[MAX_PATH];
+		DWORD dwReqSize;
+		DWORD dwType;
+
+		DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
+		while (SetupDiEnumDeviceInfo(DeviceInfoSet, dwMemberIndex, &DeviceInfoData)) {
+			hKey = SetupDiOpenDevRegKey(DeviceInfoSet, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ);
+			if (hKey) {
+				long lRet;
+				dwReqSize = sizeof(szPortName);
+				lRet = RegQueryValueEx(hKey, _T("PortName"), 0, &dwType, (LPBYTE)& szPortName, &dwReqSize);
+				RegCloseKey(hKey);
+				if (_stricmp(szPortName, com_str) == 0) {
+					found = TRUE;
+					break;
+				}
+			}
+			dwMemberIndex++;
+		}
+	}
+
+	SetupDiDestroyDeviceInfoList(DeviceInfoSet);
+
+	return found;
 }
 
 BOOL WINAPI DllMain(HANDLE hInstance,



Ttssh2-commit メーリングリストの案内
Back to archive index