Example Code

Detect the Insertion/Removal of a USB Device in Windows using Windows Dbt.h

Code and Documents

Attachment

Overview
This example toggles an LED light whenever a USB device is inserted or removed from a computer using LabWindows/CVI.

Description

If the device is a recognized type, the Device Type will be displayed. The following Device Types are recognized:

Device Type 0: OEM- or IHV-defined device type

Device Type 2: Logical volume (eg, thumb drive)

Device Type 3: Port device (serial or parallel)

Information on this Windows header file and associated functionality can be found on MSDN. The following link describes Device Types used by this header file: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363246(v=vs.85).aspx

 

Requirements

  • LabWindows/CVI 2012 (or compatible)

 

Steps to Implement or Execute Code

  1. Open the file " USB Event.cws "
  2. run the program and then insert or remove a USB device.

 **This document has been updated to meet the current required format for the NI Code Exchange.**

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
johnswan
Member
Member
on

The source code would be very helpfu

oldvaxguy
Member
Member
on

Where is the Source code??????

nmira
Member
Member
on

Sorry is possible have the source code?

I tried to use your exemplas but it didnt work?

thanks

nmira
Member
Member
on

Ok now i'ts working,I send my code for any changes?

Thanks

#include "windows.h" 

#include "dbt.h"

#include <cvirte.h>

#include <userint.h>

#include "sizepanel.h"

#include "toolbox.h"

static int panelHandle;

int flag;

DEV_BROADCAST_HDR * DeviceHeader;

int CVICALLBACK UsbMsgCallback   (int panelHandle, int message,

                                      unsigned int* wParam,

                                      unsigned int* lParam,

                                      void* callbackData);

int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,

                       LPSTR lpszCmdLine, int nCmdShow)

{

          int res=0;

          if (InitCVIRTE (hInstance, 0, 0) == 0)

                    return -1;          /* out of memory */

          if ((panelHandle = LoadPanel (0, "sizepanel.uir", PANEL)) < 0)

                    return -1;

          res = InstallWinMsgCallback (panelHandle, WM_DEVICECHANGE,

                                      UsbMsgCallback,

                                      VAL_MODE_INTERCEPT,

                                      NULL, &flag);

          DisplayPanel (panelHandle);

          RunUserInterface ();

          DiscardPanel (panelHandle);

          RemoveWinMsgCallback (panelHandle, WM_CLOSE);

          return 0;

}

int CVICALLBACK QuitCallback (int panel, int control, int event,

                    void *callbackData, int eventData1, int eventData2)

{

          switch (event)

          {

                    case EVENT_COMMIT:

                              QuitUserInterface (0);

                              break;

          }

          return 0;

}

int CVICALLBACK UsbMsgCallback (int panelHandle, int message,

                                    unsigned int* wParam, unsigned int* lParam,

                                    void* callbackData)

          int val=0;

          unsigned int size;

    unsigned int DeviceType;

switch(*wParam)

          {

          case DBT_DEVICEARRIVAL:

           GetCtrlVal(panelHandle,PANEL_LED,&val);

           SetCtrlVal(panelHandle,PANEL_LED,!val);

           DeviceHeader=( DEV_BROADCAST_HDR*)(*lParam);

           size=DeviceHeader->dbch_size;

           DeviceType=DeviceHeader->dbch_devicetype;

           SetCtrlVal(panelHandle,PANEL_NUMERIC,DeviceType);

           break;

          }

    return 0;

}

HowardE
Member
Member
on

I could really use this example - all that's here is the workspace. None of the code. ☹️

 

Does anyone have it?