DeviceController.cpp
上传用户:hhs829
上传日期:2022-06-17
资源大小:586k
文件大小:3k
- // DeviceController.cpp : implementation file
- //
- #include "stdafx.h"
- #include "DsDemo.h"
- #include "DeviceController.h"
- #include <streams.h>
- #include "Dbt.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CDeviceController
- IMPLEMENT_DYNCREATE(CDeviceController, CFrameWnd)
- CDeviceController::CDeviceController()
- {
- }
- CDeviceController::~CDeviceController()
- {
- }
- BEGIN_MESSAGE_MAP(CDeviceController, CFrameWnd)
- //{{AFX_MSG_MAP(CDeviceController)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- ON_WM_CREATE()
- ON_WM_CLOSE()
- ON_WM_DEVICECHANGE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDeviceController message handlers
- void CDeviceController::Register(void)
- {
- DEV_BROADCAST_DEVICEINTERFACE filterData;
- ZeroMemory(&filterData, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
-
- filterData.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
- filterData.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
- filterData.dbcc_classguid = AM_KSCATEGORY_CAPTURE;
- mUnregisterDeviceNotification = NULL;
- mRegisterDeviceNotification = NULL;
- // dynload device removal APIs
- {
- HMODULE hmodUser = GetModuleHandle(TEXT("user32.dll"));
- ASSERT(hmodUser); // we link to user32
- mUnregisterDeviceNotification = (PUnregisterDeviceNotification)
- GetProcAddress(hmodUser, "UnregisterDeviceNotification");
- // m_pRegisterDeviceNotification is prototyped differently in unicode
- mRegisterDeviceNotification = (PRegisterDeviceNotification)
- GetProcAddress(hmodUser,
- #ifdef UNICODE
- "RegisterDeviceNotificationW"
- #else
- "RegisterDeviceNotificationA"
- #endif
- );
- // failures expected on older platforms.
- ASSERT(mRegisterDeviceNotification && mUnregisterDeviceNotification ||
- !mRegisterDeviceNotification && !mUnregisterDeviceNotification);
- }
- mNotifyHandle = NULL;
- if (mRegisterDeviceNotification)
- {
- mNotifyHandle = mRegisterDeviceNotification(this->GetSafeHwnd(), &filterData, DEVICE_NOTIFY_WINDOW_HANDLE);
- ASSERT(mNotifyHandle != NULL);
- }
- }
- void CDeviceController::Unregister(void)
- {
- if (mNotifyHandle != NULL)
- {
- ASSERT(mUnregisterDeviceNotification);
- mUnregisterDeviceNotification(mNotifyHandle);
- mNotifyHandle = NULL;
- }
- }
- int CDeviceController::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- int createResult = CFrameWnd::OnCreate(lpCreateStruct);
- if (createResult != -1)
- {
- Register();
- }
- return createResult;
- }
- void CDeviceController::OnClose()
- {
- Unregister();
- CFrameWnd::OnClose();
- }
- BOOL CDeviceController::OnDeviceChange( UINT nEventType, DWORD dwData )
- {
- // We are interested in only device arrival & removal events
- if (DBT_DEVICEARRIVAL != nEventType && DBT_DEVICEREMOVECOMPLETE != nEventType)
- return CWnd::OnDeviceChange (nEventType, dwData);
- // ...
- return CWnd::OnDeviceChange (nEventType, dwData);
- }