Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Coonected USB Device

Coonected USB Device

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Davitor

    Hi All I have a problem to get information of connected USB Device.I am useing

    OnMyDeviceChange(WPARAM wParam, LPARAM lParam);

    function which is provide me all information.But when USB Device is connected all ready and my application is start after connecteion of USB Device then i havn't found any information of USB Device.Can any one give me advice to how to get connected USB Device information.Plz help me

    X Offline
    X Offline
    Xing Chen
    wrote on last edited by
    #2

    1.You must use RegisterDeviceNotification() to let system notify you application. 2.Add message handler for WM_DEVICECHANGE.

    D 1 Reply Last reply
    0
    • D Davitor

      Hi All I have a problem to get information of connected USB Device.I am useing

      OnMyDeviceChange(WPARAM wParam, LPARAM lParam);

      function which is provide me all information.But when USB Device is connected all ready and my application is start after connecteion of USB Device then i havn't found any information of USB Device.Can any one give me advice to how to get connected USB Device information.Plz help me

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #3

      See http://www.beyondlogic.org/[^].

      Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

      1 Reply Last reply
      0
      • X Xing Chen

        1.You must use RegisterDeviceNotification() to let system notify you application. 2.Add message handler for WM_DEVICECHANGE.

        D Offline
        D Offline
        Davitor
        wrote on last edited by
        #4

        i am useing both RegisterDeviceNotification and WM_DEVICECHANGE.And i am getting information when my application is running and USB Device is connect.But When my application is not running and USB drive is connected and my application is start after connection of USB Device then information is not showing.But USb Device is still connected. So problem is that how to get information allready connection of USB Device.Plz help me

        X 1 Reply Last reply
        0
        • D Davitor

          i am useing both RegisterDeviceNotification and WM_DEVICECHANGE.And i am getting information when my application is running and USB Device is connect.But When my application is not running and USB drive is connected and my application is start after connection of USB Device then information is not showing.But USb Device is still connected. So problem is that how to get information allready connection of USB Device.Plz help me

          X Offline
          X Offline
          Xing Chen
          wrote on last edited by
          #5

          To get USB Device informations which already connected to system, use SetupDiEnumDeviceInfo() take this article as reference: Detecting Hardware Insertion and/or Removal[^] Hope this[^] sample code can help you too.

          1 Reply Last reply
          0
          • D Davitor

            Hi All I have a problem to get information of connected USB Device.I am useing

            OnMyDeviceChange(WPARAM wParam, LPARAM lParam);

            function which is provide me all information.But when USB Device is connected all ready and my application is start after connecteion of USB Device then i havn't found any information of USB Device.Can any one give me advice to how to get connected USB Device information.Plz help me

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            Your RegisterDeviceNotification[^] code is probably working correctly, however it will only detect changes while your application is running. You failed to mention what type of USB device your application needs to enumerate. I will assume that you need all USB device types. You will essentially need to call SetupDiGetClassDevs[^] and pass the enumerator filter of "USB". Dont be frightened by the Windows Driver Kit references in the MSDN documentation. You will be able to use the SetupAPI without the DDK installed, although I believe you will need Platform SDK/Microsoft SDK. The following link should be enough to get you started: How to enumerate hardware devices by using SetupDi calls[^] You should modify the Microsoft sample code to something like the following:#pragma comment (lib,"Setupapi.lib") hDevInfo = SetupDiGetClassDevs(NULL,"USB", 0,DIGCF_PRESENT | DIGCF_ALLCLASSES );
            You can perform additional device filtering using the System-Supplied Device Setup Classes[^] or other Enumerator strings such as "USBSTOR". Best Wishes, -David Delaune

            1 Reply Last reply
            0
            • D Davitor

              Hi All I have a problem to get information of connected USB Device.I am useing

              OnMyDeviceChange(WPARAM wParam, LPARAM lParam);

              function which is provide me all information.But when USB Device is connected all ready and my application is start after connecteion of USB Device then i havn't found any information of USB Device.Can any one give me advice to how to get connected USB Device information.Plz help me

              J Offline
              J Offline
              Jonathan Davies
              wrote on last edited by
              #7

              Hi, Have a look at the my article [^] which describes a class a wrote for detecting devices. The example screen shots show the detection of USB devices, amongst others. The code example

              const GUID Guid = GUID_DEVCLASS_USB;
              CDevInfo cDevInfo(m_hWnd, DIGCF_PRESENT , &Guid);

              shows how to use the class to detect just USB devices actually present.

              D 1 Reply Last reply
              0
              • J Jonathan Davies

                Hi, Have a look at the my article [^] which describes a class a wrote for detecting devices. The example screen shots show the detection of USB devices, amongst others. The code example

                const GUID Guid = GUID_DEVCLASS_USB;
                CDevInfo cDevInfo(m_hWnd, DIGCF_PRESENT , &Guid);

                shows how to use the class to detect just USB devices actually present.

                D Offline
                D Offline
                Davitor
                wrote on last edited by
                #8

                Getting error

                al error C1083: Cannot open include file: 'atlapp.h': No such file or directory

                J 1 Reply Last reply
                0
                • D Davitor

                  Getting error

                  al error C1083: Cannot open include file: 'atlapp.h': No such file or directory

                  J Offline
                  J Offline
                  Jonathan Davies
                  wrote on last edited by
                  #9

                  AtlApp.h is part of WTL. The article uses WTL for the windows, dialogs, listboxes etc. I guess you're using MFC?

                  D 1 Reply Last reply
                  0
                  • J Jonathan Davies

                    AtlApp.h is part of WTL. The article uses WTL for the windows, dialogs, listboxes etc. I guess you're using MFC?

                    D Offline
                    D Offline
                    Davitor
                    wrote on last edited by
                    #10

                    yes i am useing MFC.

                    J 1 Reply Last reply
                    0
                    • D Davitor

                      yes i am useing MFC.

                      J Offline
                      J Offline
                      Jonathan Davies
                      wrote on last edited by
                      #11

                      OK, the classes depend on WTL, but I just tried some code an MFC dialog and started getting names of devices. I put this in OnInitDialog to test it as the first wizard generated comment shows. Try it. You'll need to #include devguid.h; and then put a break point on nMemberIndex++; and look at the name in szFriendlyName. After the first name go round the loop looking at the names.

                      // TODO: Add extra initialization here
                      const GUID Guid = GUID\_DEVCLASS\_PORTS;
                      DWORD Flags = DIGCF\_PRESENT |  DIGCF\_DEVICEINTERFACE;
                      HDEVINFO hDevInfo = SetupDiGetClassDevs(&Guid, 0, 0, Flags);
                      
                      
                      BOOL bDevInfo;
                      int nMemberIndex = 0;
                      do{
                      	SP\_DEVINFO\_DATA spDevInfoData;
                      	spDevInfoData.cbSize = sizeof(SP\_DEVINFO\_DATA);
                      	bDevInfo = ::SetupDiEnumDeviceInfo(hDevInfo, nMemberIndex, &spDevInfoData);
                      
                      	DWORD Property = SPDRP\_FRIENDLYNAME;
                      	wchar\_t  szFriendlyName\[1024\] = {0};
                      	BOOL bGotRegProp = ::SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData,
                      													  Property,
                      													  0L,
                      													  (PBYTE)szFriendlyName,
                      													  2048,
                      													  0);
                      
                      	nMemberIndex++;
                      }
                      while(bDevInfo);
                      

                      Once you have this working then change GUID_DEVCLASS_PORTS for something that finds your device, but start with it as it is then were running the same code.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups