VC 检测U盘 C++

#include <windows.h>
 #include <Shlwapi.h>
 #include <fstream.h>
 #include <TlHelp32.h>
 #include <Dbt.h>
 
  #pragma comment(lib,"shlwapi.lib")
 
 #define TIMER 1//计时器
 
 //function 
 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//窗口过程
 //获取盘符
 TCHAR FirstDriveFromMask (ULONG unitmask);
 
 //global variable
 TCHAR szExePath[MAX_PATH];//the virus's path
 TCHAR U[2];//保存U盘的盘符
 TCHAR szSysPath[MAX_PATH];//system path
 
 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                     PSTR szCmdLine, int iCmdShow)
 {
     static TCHAR szAppName[]=TEXT ("UUUUUU");
     HWND               hwnd;
     MSG                msg;
     WNDCLASS           wndclass;
     
     wndclass.style            =0;
     wndclass.lpfnWndProc      =WndProc;
     wndclass.cbClsExtra       =0;
     wndclass.cbWndExtra       =0;
     wndclass.hInstance        =hInstance;
     wndclass.hIcon            =0;
     wndclass.hCursor          =0;
     wndclass.hbrBackground    =0;
     wndclass.lpszMenuName     =NULL;
     wndclass.lpszClassName    =szAppName;
     if (!RegisterClass (&wndclass))
     {
         MessageBox (NULL,TEXT("Program requires Windows NT!"),
             szAppName, MB_ICONERROR);
         return 0;
     }
     hwnd = CreateWindow (szAppName, NULL,
         WS_DISABLED,
         0, 0,
         0, 0,
         NULL, NULL, hInstance, NULL);
     while (GetMessage(&msg, NULL, 0, 0))
     {
         TranslateMessage (&msg);
         DispatchMessage (&msg);
     }
     return msg.wParam;
 }
 LRESULT OnDeviceChange(HWND hwnd,WPARAM wParam, LPARAM lParam)
 {
     PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
     switch(wParam)
     {
     case DBT_DEVICEARRIVAL: //插入
         if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
         {
             PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
             U[0]=FirstDriveFromMask(lpdbv ->dbcv_unitmask);//得到u盘盘符
             MessageBox(0,U,"Notice!",MB_OK);
         
         }
         break;
     case DBT_DEVICEREMOVECOMPLETE: //设备删除
         break;
     }
     return LRESULT();
 }
 
 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,LPARAM 
                           lParam)
 {
     switch(message)
     {
     case WM_CREATE:           //处理一些要下面要用到的全局变量
         U[1]=':';
     
         SetTimer(hwnd,TIMER,5000,0);//启动计时器
 
         return 0;
     case WM_TIMER:             //timer message 
     
         SendMessage(hwnd,WM_DEVICECHANGE,0,0);//检测有没有插入设备消息
         
         return 0;
     case WM_DEVICECHANGE:
         OnDeviceChange(hwnd,wParam,lParam);
         return 0;
     case WM_DESTROY:
         KillTimer(hwnd,TIMER);
         PostQuitMessage(0);
         return 0;
     }
     return DefWindowProc(hwnd, message, wParam, lParam);
 }
 
 TCHAR FirstDriveFromMask(ULONG unitmask)
 {
     char i;
     for (i = 0; i < 26; ++i)
     {
         if (unitmask & 0x1)//看该驱动器的状态是否发生了变化
             break;
         unitmask = unitmask >> 1;
     }
     return (i + 'A');
  }

你可能感兴趣的:(VC 检测U盘 C++)