Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Системное программирование и WinAPI > Как перехватить вставку/извлечение флешки?


Автор: Alca 19.3.2009, 11:24
Как перехватить вставку/извлечение флешки? (тестил на VC++ 2008)
Может кому сгодиться...
http://msdn.microsoft.com/en-us/library/aa363215(VS.85).aspx
http://msdn.microsoft.com/en-us/library/aa363427(VS.85).aspx
https://rootkits.ru/viewtopic.php?id=499&words=
http://forum.sources.ru/index.php?showtopic=249011&st=0&#entry2062579
Код

#include <windows.h>
#include <dbt.h>
#include <strsafe.h>
#include "resource.h"

LRESULT CALLBACK MainProc(HWND, UINT, UINT, LONG);


HINSTANCE g_hInst; 
void Main_OnDeviceChange (HWND hwnd, WPARAM wParam, LPARAM lParam);
char FirstDriveFromMask (ULONG unitmask);  //prototype

/*------------------------------------------------------------------
   Main_OnDeviceChange (hwnd, wParam, lParam)

   Description
      Handles WM_DEVICECHANGE messages sent to the application's
      top-level window.
--------------------------------------------------------------------*/
void Main_OnDeviceChange (HWND hwnd, WPARAM wParam, LPARAM lParam) {
   PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
   char szMsg[80];

   switch(wParam)
   {
      case DBT_DEVICEARRIVAL:
         // Check whether a CD or DVD was inserted into a drive.
         if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
         {
            PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;

            if (lpdbv -> dbcv_flags & DBTF_MEDIA)
            {
               StringCchPrintf (szMsg, 80, "Drive %c: Media has arrived.\n",
                         FirstDriveFromMask(lpdbv ->dbcv_unitmask));

               MessageBox (hwnd, szMsg, "WM_DEVICECHANGE", MB_OK);
            }
         }
         break;

      case DBT_DEVICEREMOVECOMPLETE:
         // Check whether a CD or DVD was removed from a drive.
         if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
         {
            PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;

            if (lpdbv -> dbcv_flags & DBTF_MEDIA)
            {
               StringCchPrintf (szMsg, 80, "Drive %c: Media was removed.\n",
                         FirstDriveFromMask(lpdbv ->dbcv_unitmask));

               MessageBox (hwnd, szMsg, "WM_DEVICECHANGE", MB_OK);
            }
         }
         break;

      default:
           /*
              Process other WM_DEVICECHANGE notifications for other 
              devices or reasons.
           */ 
            ;
   }
}

/*------------------------------------------------------------------
   FirstDriveFromMask (unitmask)

   Description
     Finds the first valid drive letter from a mask of drive letters.
     The mask must be in the format bit 0 = A, bit 1 = B, bit 2 = C, 
     etc. A valid drive letter is defined when the corresponding bit 
     is set to 1.

   Returns the first drive letter that was found.
--------------------------------------------------------------------*/
char FirstDriveFromMask (ULONG unitmask) {
   char i;

   for (i = 0; i < 26; ++i)
   {
      if (unitmask & 0x1)
         break;
      unitmask = unitmask >> 1;
   }

   return (i + 'A');
}
//---------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance,    HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    MSG msg;

    g_hInst = hPrevInstance;
    ::CreateDialog(g_hInst, MAKEINTRESOURCE(IDD_dlgMain), NULL, (DLGPROC)MainProc );

    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK MainProc(HWND hDlg, UINT message, UINT wParam, LONG lParam) {
    switch(message)    {
        case WM_DEVICECHANGE:
            Beep(100, 100);    //должно бибикнуть
            break;

        case WM_COMMAND:
            {
                switch (wParam) {
                    case IDCANCEL: 
                        PostQuitMessage( 0 );
                        break;                        
                }
            }
            break;

        case WM_DESTROY:
            PostQuitMessage (0);
            break;
    }

    return 0;
}
//---------------------------------------------------------------------------


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)