Do you need to Monitor Devices in your Machine from your Delphi Application? Don’t know where to start with ? Don’t worry. MiTec’s System Information Management Suite’s component helps to monitor your USB, Bluetooth devices or TV/monitor connection/disconnection, volumes mount/unmount, CD/DVD insert/eject with less code and we will learn how to use MiTeC_DeviceMonitor in this blog post.
Platforms: Windows.
Installation Steps:
You can easily install this Component Suite from GetIt Package Manager. The steps are as follows.
- Navigate In RAD Studio IDE->Tools->GetIt Package Manager->select Components in Categories->Components->Trail -MiTec system Information Component Suite 14.3 and click Install Button.
- Read the license and Click Agree All. An Information dialog saying ‘Requires a restart of RAD studio at the end of the process. Do you want to proceed? click yes and continue.
- It will download the plugin and installs it. Once installed Click Restart now.
How to run the Demo app:
- Navigate to the System Information Management Suite trails setup, Demos folder which is installed during Get It installation e.g) C:UsersDocumentsEmbarcaderoStudio21.0CatalogRepositoryMiTeC-14.3DemosDelphi8
- Open the DeviceMonitor project in RAD studio 10.4.1 compile and Run the application.
- This Demo App shows how to monitor the USB Devices, Storage Devices upon connection and disconnection to your machine.
Components used in MSIC DeviceMonitor App:
- TMiTeC_DeviceMonitor: Catches USB, Bluetooth devices or TV/monitor connection/disconnection, volumes mount/unmount, CD/DVD insert/eject and other events from other devices. It has events such as OnDeviceConnect, OnDeviceDisconnect, OnMessage, OnVolumeConnect, OnVolumeDisconnect to monitor the devices.
- TListBox to list the event messages upon device gets connected and disconnected other message notifications.
- TMemo to highlight the line from the list box separately for easy reading.
- TButton to save/clear the messages in the list box and for closing.
Implementation Details:
- An instance DeviceMonitor of TMiTeC_DeviceMonitor is created. During DeviceMonitorDeviceConnect, it identifies the device desc class by matching the GUID and logs the message according to the device connected.
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 |
procedure Twnd_dm_Main.DeviceMonitorDeviceConnect(Sender: TObject; DeviceDesc: TDeviceDesc); var t: Integer; g: TGUID; begin if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_USB_DEVICE)) then g:=GUID_DEVCLASS_USB else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_DISK)) then g:=GUID_DEVCLASS_DISKDrive else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_WPD)) then g:=GUID_DEVCLASS_WPD else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_REMOVABLE_STORAGE)) then g:=GUID_DEVCLASS_FDC else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_VOLUME)) then g:=GUID_DEVCLASS_VOLUME else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_BLUETOOTH)) then g:=GUID_DEVCLASS_BLUETOOTH else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_NET)) then g:=GUID_DEVCLASS_NET else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_MOUSE)) then g:=GUID_DEVCLASS_MOUSE else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_KEYBOARD)) then g:=GUID_DEVCLASS_KEYBOARD else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_DISPLAY_ADAPTER)) then g:=GUID_DEVCLASS_DISPLAY else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_MONITOR)) then g:=GUID_DEVCLASS_MONITOR else g:=StringToGUID(DeviceDesc.GUID); //AddLog('GUID: '+DeviceDesc.GUID); SetupDiGetClassImageIndex(spid,g,t); if DeviceDesc.Name<>'' then AddLog(Format('%s (%s) connected.',[DeviceDesc.Location,DeviceDesc.Name]),t) else if DeviceDesc.Description<>'' then AddLog(Format('%s (%s) connected.',[DeviceDesc.Location,DeviceDesc.Description]),t) else if DeviceDesc.SymbolicName<>'' then AddLog(Format('Device (%s) connected.',[ExtractFilename(Uppercase(DeviceDesc.SymbolicName))]),t) else AddLog('Device connected.',t); end; |
- Upon disconnecting, it identifies the device desc class by matching the GUID and logs the message according to the device disconnected.
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 |
procedure Twnd_dm_Main.DeviceMonitorDeviceDisconect(Sender: TObject; DeviceDesc: TDeviceDesc); var t: Integer; g: TGUID; begin if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_USB_DEVICE)) then g:=GUID_DEVCLASS_USB else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_DISK)) then g:=GUID_DEVCLASS_DISKDRIVE else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_WPD)) then g:=GUID_DEVCLASS_WPD else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_REMOVABLE_STORAGE)) then g:=GUID_DEVCLASS_FDC else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_VOLUME)) then g:=GUID_DEVCLASS_VOLUME else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_BLUETOOTH)) then g:=GUID_DEVCLASS_BLUETOOTH else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_NET)) then g:=GUID_DEVCLASS_NET else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_MOUSE)) then g:=GUID_DEVCLASS_MOUSE else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_KEYBOARD)) then g:=GUID_DEVCLASS_KEYBOARD else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_DISPLAY_ADAPTER)) then g:=GUID_DEVCLASS_DISPLAY else if SameText(DeviceDesc.GUID,GUIDToString(GUID_DEVINTERFACE_MONITOR)) then g:=GUID_DEVCLASS_MONITOR else g:=StringToGUID(DeviceDesc.GUID); //AddLog('GUID: '+DeviceDesc.GUID); SetupDiGetClassImageIndex(spid,g,t); if DeviceDesc.Name<>'' then AddLog(Format('%s (%s) disconnected.',[DeviceDesc.Location,DeviceDesc.Name]),t) else if DeviceDesc.Description<>'' then AddLog(Format('%s (%s) disconnected.',[DeviceDesc.Location,DeviceDesc.Description]),t) else if DeviceDesc.SymbolicName<>'' then AddLog(Format('Device (%s) disconnected.',[DeviceDesc.SymbolicName]),t) else AddLog('Device disconnected.',t); end; |
- To monitor device custom event messages,
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 |
procedure Twnd_dm_Main.DeviceMonitorMessage(Sender: TObject; Msg: TMessage); var m,d: string; begin if not cbxAll.Checked then Exit; m:=GetMessageType(Msg.wParam); case Msg.wParam of DBT_DEVICEARRIVAL..DBT_CUSTOMEVENT: begin d:=GetDeviceType(PDevBroadcastHdr(Msg.lParam).dbch_devicetype); case PDevBroadcastHdr(Msg.lParam).dbch_devicetype of DBT_DEVTYP_OEM, DBT_DEVTYP_DEVNODE, DBT_DEVTYP_PORT: AddLog(Format('*%s: %s',[m,d])); DBT_DEVTYP_VOLUME: AddLog(Format('*%s: %s - 0x%x - 0x%x',[m,d, PDevBroadcastVolume(Msg.lParam).dbcv_unitmask, PDevBroadcastVolume(Msg.lParam).dbcv_flags])); DBT_DEVTYP_NET: AddLog(Format('*%s: %s - 0x%x',[m,d,PDevBroadcastNet(Msg.lParam).dbcn_resource])); DBT_DEVTYP_DEVICEINTERFACE: try AddLog(Format('*%s: %s - %s (Class: %s)',[m,d, string(PChar(@PDevBroadcastDeviceInterface(Msg.lParam).dbcc_name)), GUIDToString(PDevBroadcastDeviceInterface(Msg.lParam).dbcc_classguid)])); except AddLog(Format('*%s: %s',[m,d])); end; DBT_DEVTYP_HANDLE: AddLog(Format('*%s - %s - %s',[m,d,GUIDToString(PDevBroadcastHandle(Msg.lParam).dbch_eventguid)])); else AddLog(Format('*%s: %s',[m,d])); end; end; else AddLog(Format('*%s: 0x%x',[m,Msg.lParam])); end; end; |
Developers can use this MiTeC Component suite and able to monitor the devices with writing less code.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition