用户模式应用程序如何处理 WM_DEVICECHANGE
消息取决于应用程序是否仅基于 Win32 API,还是基于 Microsoft 基础类 (MFC) 库。
Win32 应用程序
基于 Win32 的应用程序通过实现窗口 过程来处理发送到应用程序窗口的消息 () 。 有关窗口过程的详细信息,请参阅Microsoft Windows SDK文档中的窗口过程主题。
以下代码示例演示如何在基于 Win32 的应用程序中处理 WM_DEVICECHANGE
消息:
// Prototype for the function that handles the
// processing of WM_DEVICECHANGE messages.
LRESULT
OnDeviceChange(
WPARAM wParam,
LPARAM lParam
);
// The application's message processing function
// for the window that receives the WM_DEVICECHANGE
// messages.
LRESULT CALLBACK
WindowProc(
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch (uMsg)
{
.
. // Cases for other messages
.
// Device change message
case WM_DEVICECHANGE:
OnDeviceChange(wParam, lParam);
break;
.
. // Cases for other messages
.
// Catchall for all messages that are
// not handled by the application.
default:
return DefWindowProc(
hWnd,
uMsg,
wParam,
lParam
);
}
return 0;
}
// The function that handles the processing
// of WM_DEVICECHANGE messages.
LRESULT
OnDeviceChange(
WPARAM wParam,
LPARAM lParam
)
{
PDEV_BROADCAST_HDR devHdr;
PDEV_BROADCAST_DEVICEINTERFACE devInterface;
HANDLE ProcessHandle;
DWORD_PTR ProcessAffinityMask;
DWORD_PTR SystemAffinityMask;
DWORD_PTR ChangedAffinityMask;
MEMORYSTATUSEX MemoryStatus;
// Check whether the message is a device arrival message
if (wParam == DBT_DEVICEARRIVAL)
{
// Get a pointer to the structure header
devHdr = (PDEV_BROADCAST_HDR)lParam;
// Check whether the message is about a device interface
if (devHdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
{
// Get a pointer to the device interface structure
devInterface = (PDEV_BROADCAST_INTERFACE)devHdr;
// Check whether this is a message about a processor
if (IsEqualGUID(
devInterface->dbcc_classguid,
GUID_DEVICE_PROCESSOR
))
{
// Get a handle to the current process
ProcessHandle =
GetCurrentProcess();
// Get the current process and system affinity masks
GetProcessAffinityMask(
ProcessHandle,
&ProcessAffinityMask,
&SystemAffinityMask
);
// Get a mask of any change to the set of processors
ChangedAffinityMask =
ProcessAffinityMask ^ SystemAffinityMask;
// Perform any per processor memory allocation
// for any new processors
...
// Set the process affinity mask to use all the
// active processors in the hardware partition.
SetProcessAffinityMask(
ProcessHandle,
SystemAffinityMask
);
// Adjust the number of threads in any thread
// pools as needed for optimal performance.
...
}
// Check whether this is a message about memory
else if (IsEqualGUID(
devInterface->dbcc_classguid,
GUID_DEVICE_MEMORY
))
{
// Get the current memory status
GlobalMemoryStatusEx(&MemoryStatus);
// Note: MemoryStatus.ullTotalPhys contains
// the amount of physical memory in the
// hardware partition.
// Adjust the memory buffer allocations
// as needed for optimal performance.
...
}
}
}
return 0;
}
Microsoft 基础类 (MFC) 应用程序
MFC 框架处理发送到基于 MFC 的应用程序窗口的消息 () 。 基于 MFC 的应用程序必须为接收WM_DEVICECHANGE
消息的应用程序的窗口类实现 CWnd::OnDeviceChange 成员函数。
下面的代码示例演示如何在基于 MFC 的应用程序中实现 OnDeviceChange 成员函数:
afx_msg BOOL
CAppWnd::OnDeviceChange(
UINT nEventType,
DWORD_PTR dwData
)
{
PDEV_BROADCAST_HDR devHdr;
PDEV_BROADCAST_DEVICEINTERFACE devInterface;
HANDLE ProcessHandle;
DWORD_PTR ProcessAffinityMask;
DWORD_PTR SystemAffinityMask;
DWORD_PTR ChangedAffinityMask;
MEMORYSTATUSEX MemoryStatus;
if (nEventType == DBT_DEVICEARRIVAL)
{
devHdr = (PDEV_BROADCAST_HDR)dwData;
if (devHdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
{
devInterface = (PDEV_BROADCAST_INTERFACE)devHdr;
if (IsEqualGUID(
devInterface->dbcc_classguid,
GUID_DEVICE_PROCESSOR
))
{
// Get a handle to the current process
ProcessHandle =
GetCurrentProcess();
// Get the current process and system affinity masks
GetProcessAffinityMask(
ProcessHandle,
&ProcessAffinityMask,
&SystemAffinityMask
);
// Get a mask of any change to the set of processors
ChangedAffinityMask =
ProcessAffinityMask ^ SystemAffinityMask;
// Perform any per processor memory allocation
// for the new processors
...
// Set the process affinity mask to use all the
// active processors in the hardware partition.
SetProcessAffinityMask(
ProcessHandle,
SystemAffinityMask
);
// Adjust the number of threads in any thread
// pools as needed for optimal performance.
...
}
else if (IsEqualGUID(
devInterface->dbcc_classguid,
GUID_DEVICE_MEMORY
))
{
// Get the current memory status
GlobalMemoryStatusEx(&MemoryStatus);
// Note: MemoryStatus.ullTotalPhys contains
// the amount of physical memory in the
// hardware partition.
// Adjust the memory buffer allocations
// as needed for optimal performance.
...
}
}
}
return TRUE;
}