Browse Source

【软件版本】
V
【模块名称】

【问题原因】
拨插摄像头时,崩溃;
【修改描述】
未解决。
【测试结果】

sat23 3 years ago
parent
commit
c7612013f7

+ 84 - 0
FieldTestTool/FieldTestTool/CritSection.h

@@ -0,0 +1,84 @@
+#ifndef __CRITSECTION_20160221__
+#define __CRITSECTION_20160221__
+
+// ÁÙ½çÖµ;
+class ThreadSection
+{
+public:
+	ThreadSection(){
+		HRESULT hr = Init();
+		(hr);
+	}
+
+	~ThreadSection(){
+		DeleteCriticalSection(&_CriticalSection);
+	}
+
+	bool Lock()
+	{
+		bool result = false;
+		__try
+		{
+			EnterCriticalSection(&_CriticalSection);
+			result = true;
+		}
+		__except (STATUS_NO_MEMORY == GetExceptionCode())
+		{
+		}
+		return result;
+	}
+
+	bool Unlock()
+	{
+		bool result = false;
+		__try
+		{
+			LeaveCriticalSection(&_CriticalSection);
+			result = true;
+		}
+		__except (STATUS_NO_MEMORY == GetExceptionCode())
+		{
+		}
+		return result;
+	}
+
+private:
+	HRESULT Init() throw()
+	{
+		HRESULT hRes = E_FAIL;
+		__try
+		{
+			InitializeCriticalSection(&_CriticalSection);
+			hRes = S_OK;
+		}
+		__except (STATUS_NO_MEMORY == GetExceptionCode())
+		{
+			hRes = E_OUTOFMEMORY;
+		}
+		return hRes;
+	}
+
+	ThreadSection(const ThreadSection & tSection);
+	ThreadSection &operator=(const ThreadSection & tSection);
+	CRITICAL_SECTION _CriticalSection;
+};
+
+
+class AutoThreadSection
+{
+public:
+	AutoThreadSection(IN ThreadSection* pSection){
+		_pSection = pSection;
+		_pSection->Lock();
+	}
+
+	~AutoThreadSection(){
+		_pSection->Unlock();
+	}
+private:
+	AutoThreadSection(const AutoThreadSection & tSection);
+	AutoThreadSection &operator=(const AutoThreadSection & tSection);
+	ThreadSection * _pSection;
+};
+
+#endif //__CRITSECTION_20160221__

+ 4 - 0
FieldTestTool/FieldTestTool/FieldTestTool.vcproj

@@ -262,6 +262,10 @@
 				RelativePath=".\ChildFrm.h"
 				>
 			</File>
+			<File
+				RelativePath=".\CritSection.h"
+				>
+			</File>
 			<File
 				RelativePath=".\FieldTestTool.h"
 				>

+ 109 - 75
FieldTestTool/FieldTestTool/MainFrm.cpp

@@ -19,6 +19,8 @@
 #include "ChildFrm.h"
 #include "FieldTestToolView.h"
 
+ThreadSection g_csCamera;
+
 #ifdef _DEBUG
 #define new DEBUG_NEW
 #endif
@@ -849,10 +851,9 @@ bool CMainFrame::RegisterDeviceChange()
         { 0xad498944, 0x762f, 0x11d0,{ 0x8d, 0xcb, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x8c } },  // 网卡;
         { 0x4d36e96c, 0xe325, 0x11ce,{ 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } }
     }; 
-
+#if 0
     // 注册HID事件
     DEV_BROADCAST_DEVICEINTERFACE DevBroadcastDeviceInterface;
-
     memset(&DevBroadcastDeviceInterface, 0, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
     DevBroadcastDeviceInterface.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
     DevBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
@@ -861,10 +862,30 @@ bool CMainFrame::RegisterDeviceChange()
     HDEVNOTIFY hDevNotify = RegisterDeviceNotification(this->GetSafeHwnd(), &DevBroadcastDeviceInterface, DEVICE_NOTIFY_WINDOW_HANDLE);
 
     return hDevNotify ? true : false;
-}
+#else
+    HDEVNOTIFY hDevNotify;
+    for ( int i = 0; i < 6; i++ )
+    {
+        // 注册HID事件
+        DEV_BROADCAST_DEVICEINTERFACE DevBroadcastDeviceInterface;
+        memset(&DevBroadcastDeviceInterface, 0, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
+        DevBroadcastDeviceInterface.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
+        DevBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
+        DevBroadcastDeviceInterface.dbcc_classguid = GUID_DEVINTERFACE_LIST[i];
+        HDEVNOTIFY hDevNotify = RegisterDeviceNotification(this->GetSafeHwnd(), &DevBroadcastDeviceInterface, DEVICE_NOTIFY_WINDOW_HANDLE);
+        if ( !hDevNotify )
+            return false;
+    }
 
+    return true;
+#endif    
+}
+#include <string>
 BOOL CMainFrame::OnDeviceChange(UINT nEventType, DWORD_PTR dwData)
 {
+    if ( dwData == 0 )
+        return FALSE;
+
     CMFCRibbonBar* pRibbon = GetRibbonBar();
     ASSERT_VALID(pRibbon);
 
@@ -872,97 +893,110 @@ BOOL CMainFrame::OnDeviceChange(UINT nEventType, DWORD_PTR dwData)
     CMFCRibbonComboBox* pSwitcherCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_RIBBON_SWITCHER_COMBOBOX));
     ASSERT_VALID(pInfradeCombo);
     ASSERT_VALID(pSwitcherCombo);
-#ifdef _DEBUG
-    if ( dwData != 0 )
-        TRACE("dwData 不为空\r\n");
-#endif
 
     DEV_BROADCAST_DEVICEINTERFACE* pdbd = (DEV_BROADCAST_DEVICEINTERFACE*)dwData;
-    DEV_BROADCAST_PORT *pbdp = (DEV_BROADCAST_PORT*)dwData;
-    if ( pbdp && _tcsstr(pbdp->dbcp_name, _T("COM")) )
+    if ( pdbd->dbcc_devicetype == DBT_DEVTYP_PORT )
     {
+        DEV_BROADCAST_PORT *pbdp = (DEV_BROADCAST_PORT*)dwData;
+        if (_tcsstr(pbdp->dbcp_name, _T("COM")))
         {
-            // 查找是否有添加过该项;
-            int nIndex = pInfradeCombo->FindItem(pbdp->dbcp_name);
-            // 删除和新增前,先获取当前选中的项;
-            CString strCurCOM = _T("");
-            int nCurIndex = pInfradeCombo->GetCurSel();
-            if ( nCurIndex != CB_ERR )
-                strCurCOM = pInfradeCombo->GetItem(nCurIndex);
-
-            if ( nEventType == DBT_DEVICEREMOVECOMPLETE )
-            {// 串口拨掉;
-                TRACE(_T("串口拨掉\n"));
-                if ( nIndex != CB_ERR )
-                {                
-                    pInfradeCombo->DeleteItem(nIndex);
-                    // 删除后, 由于索引变化了,要重新设置选中项;
-                    if ( nCurIndex != CB_ERR )
-                        pInfradeCombo->SelectItem(strCurCOM);
+            // 测试精灵;
+            {
+                // 查找是否有添加过该项;
+                int nIndex = pInfradeCombo->FindItem(pbdp->dbcp_name);
+                // 删除和新增前,先获取当前选中的项;
+                CString strCurCOM = _T("");
+                int nCurIndex = pInfradeCombo->GetCurSel();
+                if ( nCurIndex != CB_ERR )
+                    strCurCOM = pInfradeCombo->GetItem(nCurIndex);
+
+                if ( nEventType == DBT_DEVICEREMOVECOMPLETE )
+                {// 串口拨掉;
+                    TRACE(_T("串口拨掉\n"));
+                    if ( nIndex != CB_ERR )
+                    {                
+                        pInfradeCombo->DeleteItem(nIndex);
+                        // 删除后, 由于索引变化了,要重新设置选中项;
+                        if ( nCurIndex != CB_ERR )
+                            pInfradeCombo->SelectItem(strCurCOM);
+                    }
                 }
-            }
-            else if ( nEventType == DBT_DEVICEARRIVAL )
-            {// 串口插入;
-                TRACE(_T("串口插入\n"));
-                if ( nIndex == CB_ERR )
-                {
-                    pInfradeCombo->AddItem(pbdp->dbcp_name);
-                    // 新增后,由于索引变化了,要重新设置选中项;
-                    if ( nCurIndex != CB_ERR )
-                        pInfradeCombo->SelectItem(strCurCOM);
-                    else
+                else if ( nEventType == DBT_DEVICEARRIVAL )
+                {// 串口插入;
+                    TRACE(_T("串口插入\n"));
+                    if ( nIndex == CB_ERR )
                     {
-                        IsTestWizardPort(pbdp->dbcp_name);
+                        pInfradeCombo->AddItem(pbdp->dbcp_name);
+                        // 新增后,由于索引变化了,要重新设置选中项;
+                        if ( nCurIndex != CB_ERR )
+                            pInfradeCombo->SelectItem(strCurCOM);
+                        else
+                        {
+                            IsTestWizardPort(pbdp->dbcp_name);
+                        }
                     }
                 }
             }
-        }
 
-        {
-            // 查找是否有添加过该项;
-            int nIndex = pSwitcherCombo->FindItem(pbdp->dbcp_name);
-            // 删除和新增前,先获取当前选中的项;
-            CString strCurCOM = _T("");
-            int nCurIndex = pSwitcherCombo->GetCurSel();
-            if ( nCurIndex != CB_ERR )
-                strCurCOM = pSwitcherCombo->GetItem(nCurIndex);
-
-            if ( nEventType == DBT_DEVICEREMOVECOMPLETE )
-            {// 串口拨掉;
-                TRACE(_T("串口拨掉\n"));
-                if ( nIndex != CB_ERR )
-                {                
-                    pSwitcherCombo->DeleteItem(nIndex);
-                    // 删除后, 由于索引变化了,要重新设置选中项;
-                    if ( nCurIndex != CB_ERR )
-                        pSwitcherCombo->SelectItem(strCurCOM);
+            // USB切换器;
+            {
+                // 查找是否有添加过该项;
+                int nIndex = pSwitcherCombo->FindItem(pbdp->dbcp_name);
+                // 删除和新增前,先获取当前选中的项;
+                CString strCurCOM = _T("");
+                int nCurIndex = pSwitcherCombo->GetCurSel();
+                if ( nCurIndex != CB_ERR )
+                    strCurCOM = pSwitcherCombo->GetItem(nCurIndex);
+
+                if ( nEventType == DBT_DEVICEREMOVECOMPLETE )
+                {// 串口拨掉;
+                    TRACE(_T("串口拨掉\n"));
+                    if ( nIndex != CB_ERR )
+                    {                
+                        pSwitcherCombo->DeleteItem(nIndex);
+                        // 删除后, 由于索引变化了,要重新设置选中项;
+                        if ( nCurIndex != CB_ERR )
+                            pSwitcherCombo->SelectItem(strCurCOM);
+                    }
                 }
-            }
-            else if ( nEventType == DBT_DEVICEARRIVAL )
-            {// 串口插入;
-                TRACE(_T("串口插入\n"));
-                if ( nIndex == CB_ERR )
-                {
-                    pSwitcherCombo->AddItem(pbdp->dbcp_name);
-                    // 新增后,由于索引变化了,要重新设置选中项;
-                    if ( nCurIndex != CB_ERR )
-                        pSwitcherCombo->SelectItem(strCurCOM);
-                    else
+                else if ( nEventType == DBT_DEVICEARRIVAL )
+                {// 串口插入;
+                    TRACE(_T("串口插入\n"));
+                    if ( nIndex == CB_ERR )
                     {
-                        IsUSBSwitchPort(pbdp->dbcp_name);
+                        pSwitcherCombo->AddItem(pbdp->dbcp_name);
+                        // 新增后,由于索引变化了,要重新设置选中项;
+                        if ( nCurIndex != CB_ERR )
+                            pSwitcherCombo->SelectItem(strCurCOM);
+                        else
+                        {
+                            IsUSBSwitchPort(pbdp->dbcp_name);
+                        }
                     }
                 }
             }
-        }
+        } 
     }
-    else
+
+    // USB等接口设备;
+    if ( pdbd->dbcc_devicetype == DBT_DEVTYP_DEVICEINTERFACE )
     {
-        //DEV_BROADCAST_HDR* phdr = (DEV_BROADCAST_HDR*)dwData;
-        // 初始化摄像头下拉框;
-        CMFCRibbonBar* pRibbon = GetRibbonBar();
-        ASSERT_VALID(pRibbon);
+#if 0
+        DEV_BROADCAST_HDR* phdr = (DEV_BROADCAST_HDR*)dwData;
         CMFCRibbonComboBox* pCameraCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_RIBBON_CAMERA_COMBOBOX));
+        // 加了线程锁也报错;
+        AutoThreadSection at(&g_csCamera);
         m_cap.EnumDevices(pCameraCombo);
+#else
+        if ( nEventType == DBT_DEVICEREMOVECOMPLETE )
+        {// 设备拨掉;
+          
+        }
+        else if ( nEventType == DBT_DEVICEARRIVAL )
+        {// 设备插入;
+
+        }
+#endif
     }
 
     TRACE(_T("OnDeviceChange\n"));

+ 1 - 0
FieldTestTool/FieldTestTool/stdafx.h

@@ -60,6 +60,7 @@
 #include <Dshow.h>
 #include <strmif.h>
 #pragma comment(lib, "strmiids.lib")
+#include "CritSection.h"
 
 // ½ûÓñêÌâÀ¸;
 #define DISABLE_CAPTIONBAR