CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2007
    Posts
    18

    Question Problem detecting DBT_DEVTYP_VOLUME

    Hello,
    I've made a service that *should* detect when a usb flash drive is plugged in. The service does detect something, but I can't retrieve information from the volume. I'm not sure what's wrong, probably the cast.
    Here's what I have:

    Code:
    //on ServiceMain
    ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
    NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
    NotificationFilter.dbcc_classguid = GUID_DEVINTERFACE_VOLUME;	
    m_hDevNotify = RegisterDeviceNotification(m_hServiceStatus, &NotificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE);
    Code:
    inline void Handler(DWORD dwOpcode,DWORD evtype, PVOID evdata, PVOID Context)
    {
        switch (dwOpcode)
        {
    ...
    	case SERVICE_CONTROL_DEVICEEVENT:
    		OutputDebugString(L"SERVICE_CONTROL_DEVICEEVENT");
    		DeviceEventNotify(evtype, evdata);
    		break;
    ....
    Code:
    BOOLEAN DeviceEventNotify(DWORD evtype, PVOID evdata)
    {
    	if (DBT_DEVICEARRIVAL == evtype || DBT_DEVICEREMOVECOMPLETE == evtype)
    	{
    		DEV_BROADCAST_VOLUME *volume = (DEV_BROADCAST_VOLUME *)evdata;
    
    		if (volume->dbcv_devicetype != DBT_DEVTYP_VOLUME)
    		{
    			OutputDebugString(L"Not a volume");
    			return FALSE;
    		}
    My function always returns FALSE. I have this code, with some minor modifications working in a simple GUI application. But I need this functionality in a service. :\

  2. #2
    Join Date
    Apr 2004
    Posts
    102

    Re: Problem detecting DBT_DEVTYP_VOLUME

    Try changing this...
    Code:
    NotificationFilter.dbcc_classguid = GUID_DEVINTERFACE_VOLUME;
    to this...
    Code:
    NotificationFilter.dbcc_classguid = GUID_DEVINTERFACE_DISK
    
    ;

  3. #3
    Join Date
    Jul 2007
    Posts
    18

    Re: Problem detecting DBT_DEVTYP_VOLUME

    Same result.

    I add this:
    Code:
    if (pHdr->dbch_devicetype != DBT_DEVTYP_VOLUME)
    		{
    			std::wstring my_string = format("TYPE: %x", pHdr->dbch_devicetype);
    			OutputDebugString(my_string.c_str());
    				return FALSE;
    		}
    Output:
    TYPE: 5
    It's not the flash drive problem since my GUI app detects it just fine. :\

  4. #4
    Join Date
    Apr 2004
    Posts
    102

    Re: Problem detecting DBT_DEVTYP_VOLUME

    A quote from this thread

    Using DBT_DEVTYP_VOLUME in a service is trick since it is a basic
    broadcast notification which are sent to top level windows only.
    If a service creates a Windows to receive this, it works only if
    the service is running in the same session as the logged on user.
    So, it will never work under Vista and under XP only for the first
    logged on user.

  5. #5
    Join Date
    Jul 2007
    Posts
    18

    Unhappy Re: Problem detecting DBT_DEVTYP_VOLUME

    Thanks Bob. Unfortunately you're the bearer of bad news (no, you'll not be shot ). I'll have to rewrite my service and make an extra program to be my usb monitor and communicate with the service.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured