[Qt-interest] Detection of new drives

Tiago Correia tiago at cnotinfor.pt
Tue Mar 17 15:22:34 CET 2009


I've made a small experiment and I extended QApplication (it could be
QCoreApplication) and implemented just 2 methods. I just made it possible to
check when a USB or storage device is plug in the computer. The only problem
I found now, but didn't bother to solve is that it message is received 3
times
This solution only works on windows. To go further just read the MSDN
documentation.

*#include "displayapp.h"*
*#include <QMessageBox>*
*#include <windows.h>*
*#include <dbt.h>*
*
*
*DisplayApp::DisplayApp( int & argc, char ** argv )*
* **: QApplication( argc, argv )*
*{*
*}*
*
*
*DisplayApp::~DisplayApp()*
*{*
*}*
*
*
*bool DisplayApp::winEventFilter ( MSG * msg, long * result )*
*{*
* **if( msg != NULL )*
* **{*
* **if( msg->message == WM_DEVICECHANGE && msg->wParam == DBT_DEVICEARRIVAL
)*
* **{*
* **PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR)msg->lParam;*
* **if( pHdr->dbch_devicetype == DBT_DEVTYP_VOLUME )*
* **{*
* **PDEV_BROADCAST_VOLUME pDevVolume = (PDEV_BROADCAST_VOLUME)pHdr;*
* **QChar c = DriveFromMask(pDevVolume->dbcv_unitmask);*
* **QMessageBox::information( 0, "USB plugged", "Drive " + QString(c) );*
* **return true;*
* **}*
* **}*
* **}*
* **return false;*
*}*
*
*
*QChar DisplayApp::DriveFromMask( DWORD unitmask )*
*{*
* **int i;*
*
*
* **for( i = 0; i < 26; ++i )*
* **{*
* **if( unitmask & 0x1 )*
* **{*
* **break;*
* **}*
* **unitmask = unitmask >> 1;*
* **}*
*
*
* **return QChar(i + 'A');*
*}*

2009/3/17 Adam Gibson <lethalmonk at gmail.com>

> This may be of use for Linux/Mac:
>
> http://labs.trolltech.com/blogs/2009/03/09/crouching-leopard-solid-dragon/
>
> 2009/3/17 Tiago Correia <tiago at cnotinfor.pt>
>
> Hi,
>> If you are in Windows that might be easy.
>>
>> Check this article
>> http://www.codeproject.com/KB/system/HwDetect.aspx
>>
>> Now how to read system messages in windows. From what I found in the
>> Assistant, you should use QAbstractEventDispatcher::instance<http://qabstracteventdispatcher.html#instance>
>> ()
>> I've also found that winEventFilter might me also useful.
>>
>> Can any one help on this matter how to get system messages in Windows?
>> I just guessing, since I never tried.
>>
>> 2009/3/16 Benjamin Sonnemann <b.sonnemann at gmail.com>
>>
>>>  Hello,
>>>
>>> i wanted to ask if someone know whether there is already
>>> a class which notifys you of changes in the attached devices.
>>> Like attaching an usb stick.
>>>
>>>
>>> Benjamin Sonnemann
>>>
>>> _______________________________________________
>>> Qt-interest mailing list
>>> Qt-interest at trolltech.com
>>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>>
>>>
>>
>>
>> --
>> Tiago Correia
>> chief technology officer
>>
>> cnoti inovação & desenvolvimento – Aprendizagem enRiquecida pela
>> Tecnologia
>> www.cnotinfor.pt
>> Telefone: +351 239 499 231
>>
>> Subscreva gratuitamente a nossa Newsletter BICA – Boletim informativo de
>> Interactividade, Comunicação e Aprendizagem
>>
>>
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>
>>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>


-- 
Tiago Correia
chief technology officer

cnoti inovação & desenvolvimento – Aprendizagem enRiquecida pela Tecnologia
www.cnotinfor.pt
Telefone: +351 239 499 231

Subscreva gratuitamente a nossa Newsletter BICA – Boletim informativo de
Interactividade, Comunicação e Aprendizagem
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090317/892c4716/attachment.html 


More information about the Qt-interest-old mailing list