ユーザー モード アプリケーションがメッセージ WM_DEVICECHANGE 処理する方法は、アプリケーションが Win32 API に純粋に基づいているか、Microsoft Foundation Class (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_DEVICEINTERFACE)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 Foundation Class (MFC) アプリケーション
MFC フレームワークは、MFC ベースのアプリケーションのウィンドウに送信されるメッセージを処理します。 MFC ベースのアプリケーションでは、 メッセージを受信するアプリケーションのウィンドウ クラスの WM_DEVICECHANGE メンバー関数を実装する必要があります。
次のコード例は、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_DEVICEINTERFACE)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;
}