Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Aplikasi menerima peristiwa perangkat DBT_DEVICEQUERYREMOVE ketika fitur dalam sistem telah memutuskan untuk menghapus perangkat tertentu. Ketika aplikasi menerima peristiwa ini, aplikasi harus menentukan apakah aplikasi menggunakan perangkat yang ditentukan dan membatalkan atau mempersiapkan penghapusan.
Dalam contoh berikut, aplikasi mempertahankan handle terbuka, hFile, pada file atau perangkat yang direpresentasikan oleh FileName. Aplikasi mendaftar untuk pemberitahuan peristiwa perangkat pada perangkat yang mendasar dengan memanggil fungsi RegisterDeviceNotification, menggunakan filter pemberitahuan dengan jenis DBT_DEVTYP_HANDLE dan menetapkan variabel hFile di anggota filter dbch_handle.
Aplikasi memproses peristiwa perangkat DBT_DEVICEQUERYREMOVE dengan cara menutup handle file terbuka ke perangkat yang akan dihapus. Jika penghapusan perangkat ini dibatalkan, aplikasi memproses peristiwa perangkat DBT_DEVICEQUERYREMOVEFAILED untuk membuka kembali handel ke perangkat. Setelah perangkat dihapus dari sistem, aplikasi memproses peristiwa perangkat DBT_DEVICEREMOVECOMPLETE dan DBT_DEVICEREMOVEPENDING dengan membatalkan pendaftaran handle pemberitahuan untuk perangkat dan menutup handle apapun yang masih terbuka untuk perangkat.
#include <windows.h>
#include <dbt.h>
#include <strsafe.h>
// ...
INT_PTR WINAPI WinProcCallback( HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam )
{
LPCTSTR FileName = NULL; // path to the file or device of interest
HANDLE hFile = INVALID_HANDLE_VALUE; // handle to the file or device
PDEV_BROADCAST_HDR pDBHdr;
PDEV_BROADCAST_HANDLE pDBHandle;
TCHAR szMsg[80];
switch (message)
{
//...
case WM_DEVICECHANGE:
switch (wParam)
{
case DBT_DEVICEQUERYREMOVE:
pDBHdr = (PDEV_BROADCAST_HDR) lParam;
switch (pDBHdr->dbch_devicetype)
{
case DBT_DEVTYP_HANDLE:
// A request has been made to remove the device;
// close any open handles to the file or device
pDBHandle = (PDEV_BROADCAST_HANDLE) pDBHdr;
if (hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
}
}
return TRUE;
case DBT_DEVICEQUERYREMOVEFAILED:
pDBHdr = (PDEV_BROADCAST_HDR) lParam;
switch (pDBHdr->dbch_devicetype)
{
case DBT_DEVTYP_HANDLE:
// Removal of the device has failed;
// reopen a handle to the file or device
pDBHandle = (PDEV_BROADCAST_HANDLE) pDBHdr;
hFile = CreateFile(FileName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
StringCchPrintf( szMsg, sizeof(szMsg)/sizeof(szMsg[0]),
TEXT("CreateFile failed: %lx.\n"),
GetLastError());
MessageBox(hWnd, szMsg, TEXT("CreateFile"), MB_OK);
}
}
return TRUE;
case DBT_DEVICEREMOVEPENDING:
pDBHdr = (PDEV_BROADCAST_HDR) lParam;
switch (pDBHdr->dbch_devicetype)
{
case DBT_DEVTYP_HANDLE:
// The device is being removed;
// close any open handles to the file or device
if (hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
}
}
return TRUE;
case DBT_DEVICEREMOVECOMPLETE:
pDBHdr = (PDEV_BROADCAST_HDR) lParam;
switch (pDBHdr->dbch_devicetype)
{
case DBT_DEVTYP_HANDLE:
pDBHandle = (PDEV_BROADCAST_HANDLE) pDBHdr;
// The device has been removed from the system;
// unregister its notification handle
UnregisterDeviceNotification(
pDBHandle->dbch_hdevnotify);
// The device has been removed;
// close any remaining open handles to the file or device
if (hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
}
}
return TRUE;
default:
return TRUE;
}
}
default:
return TRUE;
}
Topik terkait