Memproses Permintaan untuk Menghapus Perangkat

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 handel terbuka, hFile, ke file atau perangkat yang diwakili oleh FileName. Aplikasi mendaftar untuk pemberitahuan peristiwa perangkat pada perangkat yang mendasar dengan memanggil fungsi RegisterDeviceNotification , menggunakan filter pemberitahuan jenis DBT_DEVTYP_HANDLE dan menentukan variabel hFile di anggota dbch_handle filter.

Aplikasi memproses peristiwa perangkat DBT_DEVICEQUERYREMOVE dengan menutup handel 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 handel pemberitahuannya untuk perangkat dan menutup handel apa pun 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;
}

Jenis Peristiwa Perangkat