locked
Call to GetRawInputDeviceInfo fails through Remote Desktop / Terminal Services RRS feed

  • Question

  • Hi,

    We are developing a Windows application to be used through Remote Desktop. Within this application we enumerate the HID devices connected to the client using GetRawInputDeviceInfo using the following code (simplified snippets):

    private const int RidiDeviceName = 0x20000007;

    [DllImport("User32.dll", SetLastError = true)]
    private static extern uint GetRawInputDeviceInfo(IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize);

    [...]
      for (int i = 0; i < numDevices; ++i)
      {
        uint pcbSize = 0;
        RawInputDeviceList inputDeviceList =
          (RawInputDeviceList)
            Marshal.PtrToStructure(new IntPtr((rawInputDeviceList.ToInt64() + (size * i))),
              typeof(RawInputDeviceList));

        var return_first = GetRawInputDeviceInfo(inputDeviceList.hDevice, RidiDeviceName, IntPtr.Zero, ref pcbSize);

        if (pcbSize > 0)
        {
          var data = Marshal.AllocHGlobal(((int)pcbSize+1)*2);
          var return_second = GetRawInputDeviceInfo(inputDeviceList.hDevice, RidiDeviceName, data, ref pcbSize);
         
          string deviceName = Marshal.PtrToStringAnsi(data);
          if (!String.IsNullOrEmpty(deviceName) && errorCode == 0)
          {
     [...]

    When running the application on a client pc, the above code works fine and correct device names are found for each device. However when executing the application on a server through Remote Desktop or as RemoteApp, the first call to GetRawInputDeviceInfo only returns pcbSize = 2 and subsequently the second call returns garbled values in the data variable.

    When run through Remote Desktop / Remote App, neither of the two calls returns any (windows) error codes (error code is 0) and the first return value (return_first) is 0. The second return value (return_second) is however 4294967295.

    Are there any reason as to why the calls to GetRawInputDeviceInfo does not work through Remote Desktop? Are we missing any special permissions/grants which needs to be set or are there some other details we have missed? The user running the code is administrator on the server and should have all permissions.

    Best Regards

    Morten Klitgaard,

    Software Developer & Project Manager

    Logimatic, Denmark.

    Monday, September 26, 2016 6:20 PM

All replies

  • Is it possible to try this program on the server (or similar machine having the same Operating System) directly?

    Which value is in pcbSize after the call that returned 4294967295?

    Monday, September 26, 2016 7:36 PM
  • Hi,

    We have tested on both Windows 8.1 and Windows Server 2012 R2 and the issue appears on both OS. On Windows 8.1. we have been able to test the same code both directly on the pc (where the code worked as intented) as well as on the pc through a remote desktop client with the above mentioned results.

    The value of pcbSize after the last call is still 2.

    Monday, September 26, 2016 7:51 PM
  • Hi MortenFk,

    For your question, you mean it works well on client pc, but failed on server.

    Could you please provide more details? Is there any difference between client pc and server? system?

    What device and device name? Is there any difference about device in client pc and server?

    More details would be helpful for us to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, September 27, 2016 11:04 AM
  • Hi Wendy,

    As mentioned in my reply to Viorel_ we have (besides testing it on a Windows Server 2012 R2) also tested on a Windows 8.1. machine both as local client directly logging on to the Wind8 machine as well as through remote desktop from another machine to the Win8 machine. In this case the code works as intended when running as local client and is fails when run through remote desktop.

    Thus as far as I can see there is in this win8 setup no difference on the underlying OS or user account logging in to the system - the only difference is whether you are using the client directly or through a remote desktop client connection.

    Regarding the devices, none of the attached HID devices (available in the device manager) are retrievable through GetRawInputDeviceInfo when running Remote Desktop. The available devices (in the device manager) covers keyboards, mices and HID barcode scanners.

    Best Regards,

    Morten Klitgaard

    Tuesday, September 27, 2016 12:02 PM
  • Hi MortenFK,

    Thank you for feedback.

    For your question, none of the attached HID devices (available in the device manager) are retrievable through GetRawInputDeviceInfo when running Remote Desktop.

    Maybe you could try the code below. The list will also include "root" keyboard and mouse devices that are used for Terminal Services or Remote Desktop connections. The following code will skip those when they are encountered in the loop.

    if (deviceName.ToUpper().Contains("ROOT"))
    {
      continue;  //Drop into next iteration of the loop
    }

    For more details, you could refer to the link.

    I hope this would be helpful to you.

    If you have something else, please feel free to contact us.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 29, 2016 10:55 AM
  • Hi Wendy,

    Thank you for the response.

    Just to ensure that I understand you 100% correctly - when running through Remote Desktop to e.g. a server or remote machine it is not possible to enumerate any of the HID devices available in the Device Manager on the server/remote machine through GetRawInputDeviceInfo.

    This would of cause explain why I get the garbled data and pcbSize = 2 when I am running my code on a remote machine through remote desktop.

    However on the other hand. If the above understanding is correct - then I might not understand your suggestion regarding filtering out devices with a device name containing "ROOT". Due to the above-mentioned problems all the device names I get when using GetRawInputDeviceInfo through the remote desktop are garbled. 

    ---

    If the GetRawInputDeviceInfo is not working through Remote Desktop for getting device name per device handle for all the HID devices available through the device manager on the remote machine.

    Do you know of any other way that I can get the device name from a list of handles (from GetRawInputDeviceList) or a combined list of handles/device names?

    Best Regards

    Morten

    Thursday, September 29, 2016 11:11 AM
  • Hi Logimatic,

    Per my understanding, GetRawInputDeviceInfo is not working through Remote Desktop for getting device name.

    >>Do you know of any other way that I can get the device name from a list of handles (from GetRawInputDeviceList) or a combined list of handles/device names?

    I would suggest you using WMI but I have not tested it. You can try. Full details of the implementation is located here.

    Basically WMI is an SQL like query language that allows you to query various aspects of the computers infrastructure.

    I think this will help you more,

    How To Get Hardware Information (CPU ID, MainBoard Info, Hard Disk Serial, System Information , ...)

    Here is a link to a sample bit of code that allows you to browse through all the schemas provided in WMI.

    If you have any updates, please let me know, thanks.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Kristin Xie Tuesday, October 18, 2016 6:49 AM
    Tuesday, October 18, 2016 6:48 AM
  • Hi Kristin,

    Thank you very much for the WMI suggestion and links. We will look into that and post back.

    Best Regards,

    Morten

    Tuesday, October 18, 2016 7:04 AM
  • Hi Kristin,

    Thank you very much for the WMI suggestion and links. We will look into that and post back.

    Best Regards,

    Morten

    @Morten,

    Hope you bring good news!

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 18, 2016 7:07 AM
  • Hi Morten,

    don´t know if this is still of any use to you, but I´ve encountered the same issue. I can confirm everything you write. 

    We also have to handle HID devices locally as well as in remote sessions, and luckily we have never used the Raw Input APIs. We are using SetupDi* APIs, and these always work, regardless whether devices are connected locally or to a remote session client.

    I believe this is something Microsoft needs to fix. You are not the only one with this problem. For instance, you might know that Gamepad devices are supported by Chrome through the Gamepad API. Well, on Windows Chrome uses Raw Input APIs to enumerate and handle events from gamepads, just like you. And guess what - gamepads do not work in Chrome running on a remote target. 

    BR Robert

    Thursday, December 10, 2020 3:41 PM