I am having a problem trying to use windows RegisterDeviceNotification function to register notification for TrueCrypt drives. My code is a windows service and the registration part is as follows:
TCHAR volumePath[MAX_PATH];
_stprintf( volumePath, _T("\\\\.\\%c:"), (*lpcDrive));
//Create File and add notification
HANDLE hDevice = CreateFile( volumePath,
GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL );
if (hDevice == INVALID_HANDLE_VALUE)
{
bResult=FALSE;
goto end;
}
DEV_BROADCAST_HANDLE filter = {0};
filter.dbch_size = sizeof(filter);
filter.dbch_devicetype = DBT_DEVTYP_HANDLE;
filter.dbch_handle = hDevice;
filter.dbch_hdevnotify = RegisterDeviceNotification(m_hStatus, &filter, DEVICE_NOTIFY_SERVICE_HANDLE);
The volume path to the truecrypt drive is: "\\.\m:" the m_hStatus handle is the handle I get when I register my control handler using RegisterServiceCtrlHandlerEx. and its a valid handle.
The file handle is also valid however when calling RegisterDeviceNotification it returns NULL. Now I'm not sure why its not registering correctly since all other disks/devices register successfully. I've been looking all over the web for a solution to this but couldn't find anything. I am not sure if the problem is with the device type but I have browsed through TrueCrypt's code and it seems that they register their mounted device as a DBT_DEVTYP_VOLUME.
All the necessary devices types are registered at service start using the following code:
HANDLE hDevNotify = new HDEVNOTIFY[sizeof(GUID_DEVINTERFACE_LIST)/sizeof(GUID)];
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
for(int i=0; i<sizeof(GUID_DEVINTERFACE_LIST)/sizeof(GUID); i++)
{
NotificationFilter.dbcc_classguid = GUID_DEVINTERFACE_LIST[i];
hDevNotify[i] = RegisterDeviceNotification(h,&NotificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE);
}
开发者_JAVA百科
the GUID_DEVINTERFACE_LIST holds a bunch of Device GUIDs including USBs, Disks/volumes, HIDs and LAN. They all work perfectly by the way.
The thing is that from a Window application I can get the notification through WM_DEVICECHANGE. But since my application is a service I can't get that message.
So if someone had this problem before or if there is a work around to get TrueCrypt device notifications in a windows service am all ears.
Most likely the reason is that truecrypt sends the notification only to it's own session, and your service works in a different session. In our Virtual Storage products we implemented a helper DLL that is loaded by Explorer in each user session. It communicates with the mounting code and sends broadcasts further, in each session it's loaded. Most likely you would need to do something similar.
精彩评论