⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 devicecontroller.cpp

📁 简单的COM 实际例子 用法3
💻 CPP
字号:
// 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);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -