1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| const TCHAR * wnd_class_name = TEXT( "DeviceDetectionWnd" );
void ClearWndStuff()
{
::UnregisterClass( wnd_class_name, ::GetModuleHandle( NULL ) );
}
HWND InitWndStuff()
{
WNDCLASS wc = {};
wc.lpfnWndProc = ::DefWindowProc;
wc.hInstance = ::GetModuleHandle( NULL );
wc.lpszClassName = wnd_class_name;
if ( ::RegisterClass( &wc ) == 0 )
{
log_error() << "Erreur RegisterClass() : " << ::GetLastError();
return NULL;
}
HWND hWnd = ::CreateWindow(
wnd_class_name,
"",
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
::GetModuleHandle( NULL ),
NULL );
if ( hWnd == NULL )
{
ClearWndStuff();
}
return hWnd;
}
void ThreadProc()
{
// obtenir le GUID de la classe associée à la caméra
GUID guidCamera;
if ( !GetCameraGUID( guidCamera ) )
{
return;
}
// créer la fenêtre pour la réception du message de notification
// de branchement / débranchement de la caméra
this->zhWnd = InitWndStuff();
if ( this->zhWnd == NULL )
{
return;
}
// scruter les changements de configuration
BOOL bRet;
MSG msg;
while( ( bRet = ::GetMessage( &msg, NULL, 0, 0 ) ) != 0 )
{
if ( bRet == -1 )
{
log_error() << "Erreur GetMessage() : " << ::GetLastError();
return;
}
else
{
// changement dans la config hardware
if ( msg.message == WM_DEVICECHANGE )
{
bool new_status = IsDevicePlugged( guidCamera );
UpdateCameraStatus( new_status );
}
}
else if ( msg.message == WM_DESTROY )
{
// destruction de la fenêtre : quitter la boucle
::PostQuitMessage( 0 );
}
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
// si on est sorti, c'est que la fenêtre a été détruite,
// donc this->hWnd est invalide
ClearWndStuff();
}
void StopThread()
{
// pour arrêter le thread, il suffit de fermer
// la fenêtre : le thread s'arrêtera tout seul
::PostMessage(
this->zhWnd,
WM_CLOSE,
0,
0 );
} |
Partager