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. USB interaction from user space. I need some light please!

USB interaction from user space. I need some light please!

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++help
6 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.
  • S Offline
    S Offline
    sinosoidal
    wrote on last edited by
    #1

    Hi, I'm developing a driver for a usb device. I have already a driver sample that allows me to get data from my device. And the sample code to interact with the device is a console application. I need to get that data into a C# program and now I don't know what to do. I can think in some options like: - Do this directly in the driver layer (which is a big puzzle) - Wrap the code of the sample app in a C++ dll which I can then use in the C# side (don't know which kind of project that belongs to) So, I was looking for some lights in order to see where I should focus my development efforts. Any help would be appreciatted. With my best regards, Nuno

    R L 2 Replies Last reply
    0
    • S sinosoidal

      Hi, I'm developing a driver for a usb device. I have already a driver sample that allows me to get data from my device. And the sample code to interact with the device is a console application. I need to get that data into a C# program and now I don't know what to do. I can think in some options like: - Do this directly in the driver layer (which is a big puzzle) - Wrap the code of the sample app in a C++ dll which I can then use in the C# side (don't know which kind of project that belongs to) So, I was looking for some lights in order to see where I should focus my development efforts. Any help would be appreciatted. With my best regards, Nuno

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #2

      I suggest you write a DLL that exposes the functionality of your device in the API. This is the most common way and you'll be able to use the DLL from whatever programming language.

      "It's supposed to be hard, otherwise anybody could do it!" - selfquote
      "High speed never compensates for wrong direction!" - unknown

      1 Reply Last reply
      0
      • S sinosoidal

        Hi, I'm developing a driver for a usb device. I have already a driver sample that allows me to get data from my device. And the sample code to interact with the device is a console application. I need to get that data into a C# program and now I don't know what to do. I can think in some options like: - Do this directly in the driver layer (which is a big puzzle) - Wrap the code of the sample app in a C++ dll which I can then use in the C# side (don't know which kind of project that belongs to) So, I was looking for some lights in order to see where I should focus my development efforts. Any help would be appreciatted. With my best regards, Nuno

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

        Hi sinosoidal, The DeviceIoControl Function[^] can be called directly from your C# application. There is no need to create a C++ DLL when you can pass information to and from a device driver with the C# language. Take a look in your USB driver code and look for the IRP_MJ_DEVICE_CONTROL entry point. Should be something like:

        DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = SomeFunction
        

        This defines the function which processes IOCTL codes. All you need to do is define a new IOCTL code. You simply pass a pointer in the DeviceIoControl lpOutBuffer parameter and the device driver can write data into the buffer at this address. Its extremely simple. I would recommend spending a few hours playing with the sample project at: WinDDK\6001.18002\src\general\ioctl\ This will give you an idea about how simple communication with a device driver is. If the data arriving on the USB device needs to fire an event to notify your usermode application you can view the sample located at: WinDDK\6001.18002\src\general\event I highly recommend investing a few hours experimenting with these samples. The C# language is full featured and there is no reason to write a native DLL if the parent application is managed. You can communicate with the device driver directly from your C# application. Best Wishes, -David Delaune

        S 1 Reply Last reply
        0
        • L Lost User

          Hi sinosoidal, The DeviceIoControl Function[^] can be called directly from your C# application. There is no need to create a C++ DLL when you can pass information to and from a device driver with the C# language. Take a look in your USB driver code and look for the IRP_MJ_DEVICE_CONTROL entry point. Should be something like:

          DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = SomeFunction
          

          This defines the function which processes IOCTL codes. All you need to do is define a new IOCTL code. You simply pass a pointer in the DeviceIoControl lpOutBuffer parameter and the device driver can write data into the buffer at this address. Its extremely simple. I would recommend spending a few hours playing with the sample project at: WinDDK\6001.18002\src\general\ioctl\ This will give you an idea about how simple communication with a device driver is. If the data arriving on the USB device needs to fire an event to notify your usermode application you can view the sample located at: WinDDK\6001.18002\src\general\event I highly recommend investing a few hours experimenting with these samples. The C# language is full featured and there is no reason to write a native DLL if the parent application is managed. You can communicate with the device driver directly from your C# application. Best Wishes, -David Delaune

          S Offline
          S Offline
          sinosoidal
          wrote on last edited by
          #4

          Hi, Thanks for the replies. That was good news for me. I'm trying to explorer the direct connection to the USB device from C# using DeviceIOControl. I found an example with code from an article here in codeproject. The example, basicly interacts with an pci card, but i have seen another example of interaction, but this time with an usb device and they both start with CreateFile passing the device GUID. However, i cant have a valid handle. I think i'm passing the right guid of the device, because i'm getting it from the driver it self.

          hFileHandle = INVALID_HANDLE_VALUE;
          //open handle
          hFileHandle = CreateFile("{00873fdf-61a8-11d1-aa5e-00c04fb1728b}",
          GENERIC_READ |
          GENERIC_WRITE,
          0,
          (IntPtr)0,
          OPEN_EXISTING,
          0,
          NULL);

                      if (hFileHandle == INVALID\_HANDLE\_VALUE)
                      {
                          Console.WriteLine("Cannot open driver handle");
                          return;
                      }
          

          Any tips? Thanks, Nuno

          L 1 Reply Last reply
          0
          • S sinosoidal

            Hi, Thanks for the replies. That was good news for me. I'm trying to explorer the direct connection to the USB device from C# using DeviceIOControl. I found an example with code from an article here in codeproject. The example, basicly interacts with an pci card, but i have seen another example of interaction, but this time with an usb device and they both start with CreateFile passing the device GUID. However, i cant have a valid handle. I think i'm passing the right guid of the device, because i'm getting it from the driver it self.

            hFileHandle = INVALID_HANDLE_VALUE;
            //open handle
            hFileHandle = CreateFile("{00873fdf-61a8-11d1-aa5e-00c04fb1728b}",
            GENERIC_READ |
            GENERIC_WRITE,
            0,
            (IntPtr)0,
            OPEN_EXISTING,
            0,
            NULL);

                        if (hFileHandle == INVALID\_HANDLE\_VALUE)
                        {
                            Console.WriteLine("Cannot open driver handle");
                            return;
                        }
            

            Any tips? Thanks, Nuno

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

            Hi Nuno, You can use the GetLastError Function[^] or the managed equivilent Marshal.GetLastWin32Error()[^] to see why CreateFile is failing. You must add this call immediately following CreateFile. What type of USB device are you interacting with? Typically the SetupDiGetDeviceInterfaceDetail Function[^] is used to obtain the CreateFile() compatible device path. Best Wishes, -David Delaune

            S 1 Reply Last reply
            0
            • L Lost User

              Hi Nuno, You can use the GetLastError Function[^] or the managed equivilent Marshal.GetLastWin32Error()[^] to see why CreateFile is failing. You must add this call immediately following CreateFile. What type of USB device are you interacting with? Typically the SetupDiGetDeviceInterfaceDetail Function[^] is used to obtain the CreateFile() compatible device path. Best Wishes, -David Delaune

              S Offline
              S Offline
              sinosoidal
              wrote on last edited by
              #6

              Hi David, I made it! However it was a little hard to get the device name. But I made it! :) I have to try your suggestion to get the device name more genericly. In what regards to read from the device, I'm using readfile function which was what the test app was calling, because, otherwise, I didn't knew which IO_CTL codes to use in order to invoke the request! :) I'm still ambienting myself to the windows programming model. Your help crucial guys. Thank you very much, With my best regards, Nuno PS: you'll see me again here for sure! :D

              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