none
Access USB device in a Windows UWP application using a native library (DLL) RRS feed

  • Question

  • I asked this question in another forum (https://social.msdn.microsoft.com/Forums/en-US/5800ee6e-1b97-4370-ad56-4d5634d7a249/access-usb-device-in-a-windows-uwp-application-using-a-native-library-dll?forum=windowsgeneraldevelopmentissues ) but it was recommended that I ask it here.  

    We have the need to access a specific USB device from both the Linux and Windows platforms using C#, Java and possibly C therefore I am trying to develop a native library that can be cross compiled, on Windows and Linux, using Rust (however I believe we would have the same problem using other languages like C++ as well).  This seems to work well when I access the USB device within a test Rust Application.  From this application I am able to write to the USB device and get the expected results back.  When I link the DLL to a test Windows UWP application I am unable to open the USB port and receive an “Access is Denied” error.  I am linking the DLL to the test app as described here:  https://social.msdn.microsoft.com/Forums/en-US/26013e5e-3ff1-44a0-8797-0474dabd12d0/uwpincluding-dlls-in-a-uwp-application-in-visual-studio?forum=wpdevelop

    In the Package.appxmanifest file I defined the following capabilities:

      <Capabilities>
        <CapabilityName="internetClient"/>
        <DeviceCapabilityName="serialcommunication">
          <DeviceId="any">
            <FunctionType="name:serialPort"/>
          </Device>
        </DeviceCapability>
      </Capabilities>

    These capabilities allow me to communicate to the device using the standard Windows UWP communication APIs however it does not seem to allow me to access the device from within the linked DLL.

    Is it possible to allow a native library like this to communicate with a USB device when linked to a Windows UWP app?

    Thanks

    Thursday, June 21, 2018 5:50 PM

Answers

  • UWP currently does not grant access to serial ports through CreateFile. The capability in your manifest grants access to a serial port using the SerialCommunications namespace I mentioned above. Either rust needs to support this difference or if you can go native, you can do it yourself.

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Friday, June 22, 2018 5:17 PM

All replies

  • Are you using CreatFile or Windows.Devices.SerialCommunication to open the serial port? When the dll is loaded in a uwp it will have access to all the caps declared in the app’s manifest.

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Thursday, June 21, 2018 10:52 PM
  • Neither, the serial library is written in Rust so we can cross compile it for Linux and Windows without have to change any code however it could just as easily be written in C or C++ using cross platforms serial libraries.  The Rust code looks like this:

    if let Ok(port) = get_port_info() {
       let port_name = port.port_name;        
       let baud_rate = 19200;        
       let mut settings: SerialPortSettings = Default::default();        
        settings.timeout = Duration::from_millis(10);        
        settings.baud_rate = baud_rate.into();        
        if let Ok(mut port) = serialport::open_with_settings(&port_name, &settings) 
        {            
            let new_port = port.try_clone();            
            return Some(port)        
        }    
    }    
    None

    The line "if let Ok(mut port) = serialport::open_with_settings(&port_name, &settings)" returns access denied when I catch the error and throw it back however it works fine when I build a rust console application.

    Friday, June 22, 2018 11:31 AM
  • You have to determine how Rust is implementing serial_port::open_with_settings on windows to further understand where to debug

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Friday, June 22, 2018 12:38 PM
  • This is what I know:

    For the cross platform serial port library I am using serialport-rs and the open_with_settings function (https://gitlab.com/susurrus/serialport-rs/blob/master/src/lib.rs ) uses the windows::COMPort from the serial-rs library.  This library implements the open function for the COMPort using the CreateFileW function (https://github.com/dcuddeback/serial-rs/blob/master/serial-windows/src/com.rs ).  This function is located in the c.rs from the main Rust repository (https://github.com/rust-lang/rust/blob/master/src/libstd/sys/windows/c.rs ) which defines the native calls.  

    whew :) therefore I believe it is using the native CreateFile call to open the serial port.   Does that help?

    Friday, June 22, 2018 1:13 PM
  • You said it is a USB device. Is it a serial port at all? Does the "serialcommunication" capability apply to it?

    -- pa

    Friday, June 22, 2018 4:27 PM
  • Sorry should have specified that.  It is a USB device that uses the FTDi VCP drivers (http://www.ftdichip.com/Drivers/VCP.htm) 
    Friday, June 22, 2018 4:45 PM
  • UWP currently does not grant access to serial ports through CreateFile. The capability in your manifest grants access to a serial port using the SerialCommunications namespace I mentioned above. Either rust needs to support this difference or if you can go native, you can do it yourself.

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Friday, June 22, 2018 5:17 PM
  • Thank you for the information, I guess I can stop banging my head against the wall now.  There is no work around for a UWP app?  The device we are working with is a radio and we wanted to write a single library that we can use on a Windows tablet, with a UWP app, and embedded devices running Linux and Windows so the devices could communicate.  
    Friday, June 22, 2018 5:46 PM
  • Unfortunate choice of a language... why use exotic one for a product if there's C++, C#, Javascript?

    -- pa

    Friday, June 22, 2018 8:15 PM
  • The need to create a cross platform serial port library that would work on Windows and Linux really dictated the choice of language.  Could not compile the Javascript or C# into a library that would run on Linux.  Could possibly do it in C++ however I would end up with the same problem that I am in now since most cross platform libraries probably use the CreateFile as well.  Don't understand why we cannot do something like this.  Might be easier to go with Android tablets where we can use a single library.

    Thanks again for the help.

    Monday, June 25, 2018 11:36 AM
  • .net standard 2.0 runs on Linux and has a cross plat serial port class.  It does the right thing for UWP too!

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Tuesday, June 26, 2018 6:06 AM