Detection de l'insertion/retrait d'une clef USB
Voici le code minimaliste de detection/retrait d'un peripherique USB
le .cpp
Code:
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
|
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define DBT_DEVICEARRIVAL 0x8000 // le systeme a detecte un nouveau device
#define DBT_DEVICEREMOVECOMPLETE 0x8004 // device est completement desactive
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GetDeviceChange(TMessage &Msg)
{
switch (Msg.WParam)
{
case DBT_DEVICEARRIVAL:
ShowMessage("Peripherique USB connecte");
break;
case DBT_DEVICEREMOVECOMPLETE:
ShowMessage("Peripherique USB deconnecte");
break;
}
} |
le .h
Code:
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
|
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall TForm1::GetDeviceChange(TMessage &Msg);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_DEVICECHANGE,TMessage,GetDeviceChange)
END_MESSAGE_MAP(TForm)
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif |