Вот когда-то писал нотифиер.
devicenotifier.h
C++ (Qt)
#ifndef DEVICENOTIFIER_H
#define DEVICENOTIFIER_H
#include <QtCore/QObject>
class DeviceNotifier : public QObject
{
Q_OBJECT
public:
static DeviceNotifier *instance();
virtual ~DeviceNotifier();
protected:
explicit DeviceNotifier (QObject *parent = 0);
private:
static DeviceNotifier *pInstance;
Q_SIGNALS:
void deviceListChanged ();
};
#endif //DEVICENOTIFIER_H
devicenotifier_win.cpp
C++ (Qt)
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include "qt_windows.h"
#include "dbt.h"
#include "devicenotifier.h"
Q_CORE_EXPORT HINSTANCE qWinAppInst ();
DeviceNotifier *DeviceNotifier::pInstance = 0;
HWND wnd = 0;
void createWindow ();
LRESULT CALLBACK windowProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
void destroyWindow ();
DeviceNotifier *DeviceNotifier::instance ()
{
if (!DeviceNotifier::pInstance) {
pInstance = new DeviceNotifier (QCoreApplication::instance ());
}
return pInstance;
}
DeviceNotifier::DeviceNotifier (QObject *parent)
: QObject (parent)
{
createWindow ();
}
DeviceNotifier::~DeviceNotifier ()
{
if (pInstance == this) {
pInstance = 0;
destroyWindow ();
}
}
void createWindow ()
{
WNDCLASS wc;
wc.style = 0;
wc.lpfnWndProc = windowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = 0;
wc.hIcon = 0;
wc.hCursor = 0;
wc.hbrBackground = 0;
wc.lpszMenuName = 0;
wc.lpszClassName = L"DeviceNotifierWindow";
RegisterClass (&wc);
wnd = CreateWindow (wc.lpszClassName,
wc.lpszClassName,
0,
0,
0,
0,
0,
0,
0,
0,
0);
if (!wnd) {
qDebug () << QString ("Error creating DeviceNotifier window %1")
.arg (static_cast <int> (GetLastError()));
}
}
LRESULT CALLBACK windowProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_DEVICECHANGE:
switch (wParam) {
case DBT_DEVICEARRIVAL:
case DBT_DEVICEREMOVECOMPLETE:
QMetaObject::invokeMethod (DeviceNotifier::instance (),
"deviceListChanged",
Qt::AutoConnection);
break;
}
break;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
void destroyWindow ()
{
if (wnd) {
DestroyWindow (wnd);
}
UnregisterClass (L"DeviceNotifierWindow", 0);
}
devicenotifier_unix.cpp
C++ (Qt)
#include <QtCore/QCoreApplication>
#include <QtCore/QFileSystemWatcher>
#include <QtCore/QStringList>
#include "devicenotifier.h"
QFileSystemWatcher *watcher = 0;
DeviceNotifier *DeviceNotifier::pInstance = 0;
DeviceNotifier *DeviceNotifier::instance ()
{
if (!pInstance) {
pInstance = new DeviceNotifier (QCoreApplication::instance ());
}
return pInstance;
}
DeviceNotifier::DeviceNotifier (QObject *parent)
: QObject (parent)
{
if (!watcher) {
watcher = new QFileSystemWatcher ();
watcher->addPath ("/dev");
connect (watcher, SIGNAL (directoryChanged (QString)),
this, SIGNAL (deviceListChanged ()));
}
}
DeviceNotifier::~DeviceNotifier ()
{
if (pInstance == this) {
if (watcher) {
watcher->deleteLater();
}
pInstance = 0;
}
}