Preface
This article mainly describes how to use message queue in MFC and capture the USB flash drive insertion and uninstallation events in the message.
Body
1. Use message queue in MFC
1.1 statement
Virtual LRESULT WindowProc (UINT message, WPARAM wParam, LPARAM lParam );
1.2 Implementation
LRESULT CTestDlg: WindowProc (UINT message, WPARAM wParam, LPARAM lParam)
{
Return CDialog: WindowProc (message, wParam, lParam );
}
This example inherits CDialog.
Ii. Capture USB flash drive insertion and uninstallation events
LRESULT CTestDlg: WindowProc (UINT message, WPARAM wParam, LPARAM lParam)
{
Switch (message)
{
// WM_DEVICECHANGE: system message sent from system hardware change
Case WM_DEVICECHANGE:
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR) lParam;
Switch (wParam)
{
Case WM_DEVICECHANGE:
Break;
Case DBT_DEVICEARRIVAL: // DBT_DEVICEARRIVAL. The device detection is complete and can be used.
{
If (lpdb-> dbch_devicetype = DBT_DEVTYP_VOLUME) // logical volume
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME) lpdb;
Switch (lpdbv-> dbcv_flags)
{
Case 0: // U Disk
{
CString decDriver;
DecDriver = FirstDriveFromMask (lpdbv-> dbcv_unitmask );
TRACE1 ("detected USB flash drive: [% s] inserted! \ N ", decDriver. GetBuffer (0 ));
}
Break;
Case DBTF_MEDIA: // CD
TRACE1 ("detected disc: [% c] inserted! \ N ", FirstDriveFromMask (lpdbv-> dbcv_unitmask ));
Break;
}
}
}
Break;
Case DBT_DEVICEREMOVECOMPLETE: // DBT_DEVICEREMOVECOMPLETE. Uninstall or unplug the device.
{
If (lpdb-> dbch_devicetype = DBT_DEVTYP_VOLUME) // logical volume
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME) lpdb;
Switch (lpdbv-> dbcv_flags)
{
Case 0: // U Disk
{
CString decDriver;
DecDriver = FirstDriveFromMask (lpdbv-> dbcv_unitmask );
TRACE1 ("detected USB flash drive: [% s] unplugging! \ N ", decDriver. GetBuffer (0 ));
}
Break;
Case DBTF_MEDIA: // CD
Break;
}
}
}
Break;
}
}
Break;
}
Return CDialog: WindowProc (message, wParam, lParam );
}
III,
IV. Article Modification
4.1 do not use SetWindowLong to retrieve message queues in MFC, otherwise the window cannot be closed.
4.2 thank you for reminding us that after CString getBuffer, you should call ReleaseBuffer
Conclusion
Continue anti-spoofing basics...