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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Reading a USB device

Reading a USB device

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialadobejsonquestion
17 Posts 3 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.
  • K Offline
    K Offline
    koumodaki
    wrote on last edited by
    #1

    Hi, I have been trying to read a file in a program from a USB flash drive. I know that windows allows me to use the device as a drive and I can read the file like reading from any other drive. But the problem is the flash drive will be connected to different PC and the drive name could change. So I cannot use the drive name. I am using SetupDI API to get the interface and I am also able to get the handle using CreateFile(). But I do not know how to read a particular file. Can anyone help me here? Also is there any tutorial that will help me to work with USB drives in WinXP environment?

    D 1 Reply Last reply
    0
    • K koumodaki

      Hi, I have been trying to read a file in a program from a USB flash drive. I know that windows allows me to use the device as a drive and I can read the file like reading from any other drive. But the problem is the flash drive will be connected to different PC and the drive name could change. So I cannot use the drive name. I am using SetupDI API to get the interface and I am also able to get the handle using CreateFile(). But I do not know how to read a particular file. Can anyone help me here? Also is there any tutorial that will help me to work with USB drives in WinXP environment?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      koumodaki wrote:

      But I do not know how to read a particular file.

      Do you know the file's name?

      "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      K 1 Reply Last reply
      0
      • D David Crow

        koumodaki wrote:

        But I do not know how to read a particular file.

        Do you know the file's name?

        "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        K Offline
        K Offline
        koumodaki
        wrote on last edited by
        #3

        Yes I do know the file's name.

        D 1 Reply Last reply
        0
        • K koumodaki

          Yes I do know the file's name.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          So then can't you call ReadFile()?

          "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          K 1 Reply Last reply
          0
          • D David Crow

            So then can't you call ReadFile()?

            "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            K Offline
            K Offline
            koumodaki
            wrote on last edited by
            #5

            I have a file context.cfg which needs to be read form the flash drive. How do I specify in the ReadFile() parameters that I need to read this particular file. From what I know, I can pass the handle I obtain while opening the usb flash drive from CreateFile() to this function. How do I get hold of the file inside the flash drive

            D J 2 Replies Last reply
            0
            • K koumodaki

              I have a file context.cfg which needs to be read form the flash drive. How do I specify in the ReadFile() parameters that I need to read this particular file. From what I know, I can pass the handle I obtain while opening the usb flash drive from CreateFile() to this function. How do I get hold of the file inside the flash drive

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              What does your call to CreateFile() look like?

              "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              K 1 Reply Last reply
              0
              • D David Crow

                What does your call to CreateFile() look like?

                "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                K Offline
                K Offline
                koumodaki
                wrote on last edited by
                #7

                I enumarate the USB device first and get the interface using SetupDiGetDeviceInterfaceDetail(). The name is stored in a vector Devices. Then I use the following call: HANDLE hUsbDevice = CreateFile(Devices[0].c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

                D 1 Reply Last reply
                0
                • K koumodaki

                  I enumarate the USB device first and get the interface using SetupDiGetDeviceInterfaceDetail(). The name is stored in a vector Devices. Then I use the following call: HANDLE hUsbDevice = CreateFile(Devices[0].c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  So what happens if you use:

                  string str = Devices[0] + "\\context.cfg";
                  HANDLE hUsbDevice = CreateFile(str.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

                  "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  K 2 Replies Last reply
                  0
                  • D David Crow

                    So what happens if you use:

                    string str = Devices[0] + "\\context.cfg";
                    HANDLE hUsbDevice = CreateFile(str.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

                    "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    K Offline
                    K Offline
                    koumodaki
                    wrote on last edited by
                    #9

                    Havent tried that. Will check it out

                    1 Reply Last reply
                    0
                    • K koumodaki

                      I have a file context.cfg which needs to be read form the flash drive. How do I specify in the ReadFile() parameters that I need to read this particular file. From what I know, I can pass the handle I obtain while opening the usb flash drive from CreateFile() to this function. How do I get hold of the file inside the flash drive

                      J Offline
                      J Offline
                      JudyL_MD
                      wrote on last edited by
                      #10

                      koumodaki wrote:

                      I can pass the handle I obtain while opening the usb flash drive from CreateFile() to this function.

                      The problem is that this handle is a handle to the physical drive. You can't use that to easily find a file on the drive. Your basic problem is how to convert from a USB identifier to the logical hard drive letter. Using WMI you can go from something like USBSTOR\\DISK&VEN_SANDISK&PROD_CRUZER_MINI&REV_0.1\\20041100801E102869&0 (to take an example not at random) to G:. Stick the USB flash in your system, fire up the WMI object browser and you can see the queries you need to make to WMI to go from the former to the latter. Judy

                      K D 2 Replies Last reply
                      0
                      • D David Crow

                        So what happens if you use:

                        string str = Devices[0] + "\\context.cfg";
                        HANDLE hUsbDevice = CreateFile(str.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

                        "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                        K Offline
                        K Offline
                        koumodaki
                        wrote on last edited by
                        #11

                        I tried the method you suggested. I did not work. The code snippet is std::string path = Devices[0] + "\\context.cfg"; HANDLE hUsbDevice = CreateFile(Devices[0].c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hUsbDevice == INVALID_HANDLE_VALUE) return 1; char data[MAX_PATH]; DWORD nNumberOfBytesToRead = MAX_PATH; DWORD lpNumberOfBytesRead = 0; if(!ReadFile(hUsbDevice,(void*)data, nNumberOfBytesToRead, &lpNumberOfBytesRead,NULL)) { int err = GetLastError(); return 0; } GetLastError() returns ERROR_INVALID_PARAMETER

                        D 1 Reply Last reply
                        0
                        • K koumodaki

                          I tried the method you suggested. I did not work. The code snippet is std::string path = Devices[0] + "\\context.cfg"; HANDLE hUsbDevice = CreateFile(Devices[0].c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hUsbDevice == INVALID_HANDLE_VALUE) return 1; char data[MAX_PATH]; DWORD nNumberOfBytesToRead = MAX_PATH; DWORD lpNumberOfBytesRead = 0; if(!ReadFile(hUsbDevice,(void*)data, nNumberOfBytesToRead, &lpNumberOfBytesRead,NULL)) { int err = GetLastError(); return 0; } GetLastError() returns ERROR_INVALID_PARAMETER

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #12

                          koumodaki wrote:

                          I tried the method you suggested.

                          But the code snippet you posted does not reflect such. You are not using path. I like Judy's suggestion better.

                          "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                          K 1 Reply Last reply
                          0
                          • D David Crow

                            koumodaki wrote:

                            I tried the method you suggested.

                            But the code snippet you posted does not reflect such. You are not using path. I like Judy's suggestion better.

                            "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                            K Offline
                            K Offline
                            koumodaki
                            wrote on last edited by
                            #13

                            I used the path but made a mistake while copying the code here. Ok so maybe I will try using WMI. Do you have any tutorial which explains how to use Windows API in such situatuons?

                            1 Reply Last reply
                            0
                            • J JudyL_MD

                              koumodaki wrote:

                              I can pass the handle I obtain while opening the usb flash drive from CreateFile() to this function.

                              The problem is that this handle is a handle to the physical drive. You can't use that to easily find a file on the drive. Your basic problem is how to convert from a USB identifier to the logical hard drive letter. Using WMI you can go from something like USBSTOR\\DISK&VEN_SANDISK&PROD_CRUZER_MINI&REV_0.1\\20041100801E102869&0 (to take an example not at random) to G:. Stick the USB flash in your system, fire up the WMI object browser and you can see the queries you need to make to WMI to go from the former to the latter. Judy

                              K Offline
                              K Offline
                              koumodaki
                              wrote on last edited by
                              #14

                              Thanks Judy. I will try this

                              1 Reply Last reply
                              0
                              • J JudyL_MD

                                koumodaki wrote:

                                I can pass the handle I obtain while opening the usb flash drive from CreateFile() to this function.

                                The problem is that this handle is a handle to the physical drive. You can't use that to easily find a file on the drive. Your basic problem is how to convert from a USB identifier to the logical hard drive letter. Using WMI you can go from something like USBSTOR\\DISK&VEN_SANDISK&PROD_CRUZER_MINI&REV_0.1\\20041100801E102869&0 (to take an example not at random) to G:. Stick the USB flash in your system, fire up the WMI object browser and you can see the queries you need to make to WMI to go from the former to the latter. Judy

                                D Offline
                                D Offline
                                David Crow
                                wrote on last edited by
                                #15

                                JudyL_FL wrote:

                                ...fire up the WMI object browser and you can see the queries you need to make to WMI to go from the former to the latter.

                                Interesting tool. I inserted a PNY Attache drive and tried it. I found "E:" under the Win32_LogicalDisk node, and "USB\VID_154B&PID_0005\6E68190046F6" under the Win32_USBHub node. I then found "USBSTOR\DISK&VEN_PNY&PROD_USB_2.0_FD&REV_PMAP\6E68190046F6&0" under the Win32_DiskDrive node. Where are these queries to which you refer?

                                "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                J 1 Reply Last reply
                                0
                                • D David Crow

                                  JudyL_FL wrote:

                                  ...fire up the WMI object browser and you can see the queries you need to make to WMI to go from the former to the latter.

                                  Interesting tool. I inserted a PNY Attache drive and tried it. I found "E:" under the Win32_LogicalDisk node, and "USB\VID_154B&PID_0005\6E68190046F6" under the Win32_USBHub node. I then found "USBSTOR\DISK&VEN_PNY&PROD_USB_2.0_FD&REV_PMAP\6E68190046F6&0" under the Win32_DiskDrive node. Where are these queries to which you refer?

                                  "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                  J Offline
                                  J Offline
                                  JudyL_MD
                                  wrote on last edited by
                                  #16

                                  If you follow Win32_SystemDevices to Win32_PnPEntity, you should find a leaf with a device id something like USBSTOR..... From there, you can get (look under Win32_PnPDevice) the correct DeviceId to use for Win32_DiskDrive. From there, you can get (look under Win32_DiskDriveToDiskPartition) the correct DeviceId to use for Win32_DiskPartition. From there, you can get (look under Win32_LogicalDiskToPartition) the correct DeviceId to use for Win32_LogicalDisk. That final device id is the logical drive number. (Whew!! ) Getting that first USBSTOR... monster string is what's usually the hardest part of this sort of thing since you have to use the SetupDi API. That is truly nasty!! Judy

                                  D 1 Reply Last reply
                                  0
                                  • J JudyL_MD

                                    If you follow Win32_SystemDevices to Win32_PnPEntity, you should find a leaf with a device id something like USBSTOR..... From there, you can get (look under Win32_PnPDevice) the correct DeviceId to use for Win32_DiskDrive. From there, you can get (look under Win32_DiskDriveToDiskPartition) the correct DeviceId to use for Win32_DiskPartition. From there, you can get (look under Win32_LogicalDiskToPartition) the correct DeviceId to use for Win32_LogicalDisk. That final device id is the logical drive number. (Whew!! ) Getting that first USBSTOR... monster string is what's usually the hardest part of this sort of thing since you have to use the SetupDi API. That is truly nasty!! Judy

                                    D Offline
                                    D Offline
                                    David Crow
                                    wrote on last edited by
                                    #17

                                    Wow! I need an elevator to come back to the surface from drilling down that deep. It works, though.

                                    "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                    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