none
CustomDeviceAccessorUWP failing to connect with CreateFile() RRS feed

  • Question

  • CustomDeviceAccessorUWP failing to connect with CreateFile()

    Sample Project:  CustomDeviceAccessorUWP, C++, x64
    Sample Group: Windows-iotcore-samples-develop.zip
    VisStudio2019, installed 7/29/2019 with almost every possible item (except some ARM items)
    Windows10, 1903 18362.175,  updated after VisStudio2019 was installed

    In the sample project under UWP, CreateFile() returns lasterror = 5 (access denied).
    After placing same code in a non-UWP project, CreateFile() succeeds.

    I suspect this is related to digital signing requirements for UWP, but I am not certain.


    Observations #1:
    In the CustomDeviceAccessorUWP sample project it appears there is a certificate attached to the project.  At least if I examine things via Package.appxmanifest::Tab_Packaging::Choose_Certificate it says the current certificate is CustomDeviceAccessorUWP_TemporaryKey.pfx.  However, if I use Select a Certificate it says there is no certificate available.  Also the Select a Certificate button does not have a drop down for creating a new certificate as I have seen on some webpages.  The generated .exe file does not have a signature according the DigiCertUtil.exe.

    Action #1:
    DigiCertUtil.exe was used to 'Import' CustomDeviceAccessorUWP_TemporaryKey.pfx.

    Observations #2:
    Now when I examined things via Package.appxmanifest::Tab_Packaging::Choose_Certificate::Select_Certificate there is valid certificate available and I chose it to be the certification.  Following a rebuild solution, I get the same 'access denied' error when running it through the debugger.  DigiCertUtil says there is no signature.

    Action#2:
    DigiCertUtil was used to manually sign the compiled .exe file using the certificate imported by DigiCertUtil in the last step.

    Observations #3:
    DigiCertUtil now says there is a signature.  Restarting the VisStudio debugger, without recompiling, I get the same access denied error.
    Also manually starting the .exe (through file explorer) fails due to missing DLLs.  The debug .exe list these dlls in the following order: vccorlib140d_app.DLL, VCRUNTIME140D_APP.dll, MSVCP140D_APP.dll.  The release .exe list these DLLs as missing in this order: VCRUNTIME140D_APP.dll, MSVCP140_APP.dll, vccorlib140_app.DLL.

    Action#3:
    Downloaded redistributables update and performed 'repair' operation with 'Microsoft Visual C++ 2015-2019 Redistributable (x64) - 14.22.27821. Result was Setup Successful. After that, Windows was forced to install all available updates and was rebooted.

    Observations #4:
    Same problems with access denied error and with missing DLLs.


    Questions:
    So after many hours of roaming around the webpages of MSFT and others, I am no closer to figuring out how to make this sample UWP example work.  Is the access denied error really a problem with the digital signing?  (Note the CreateFile() code works in a non-UWP project.)  What do I need to do to get this sample to work?  More generally, what is the current best procedure for signing .exe files for testing during coding with VisualStudio2019?  (Please note, this is a C++ project and there is no Signing Tab, so all those pages that say go to the Signing Tab do not really help.)

    Thanks to anyone can help.
    Monday, August 12, 2019 9:42 PM

All replies

  • Hi,
    Which platform are you running on? Windows IOT Core or enterprise? Or others?

    Best regards,

    Drake


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 13, 2019 7:04 AM
  • Hi Drake,

    Testing is being done on Windows 10 Pro, 1903 18362.267.

    The eventual target is Windows IOT Core.

    VisStudio About Box Information:

    Microsoft Visual Studio Professional 2019
    Version 16.2.1
    VisualStudio.16.Release/16.2.1+29201.188
    Microsoft .NET Framework
    Version 4.8.03752
    Installed Version: Professional
    Visual C++ 2019   00435-30010-29773-AA083
    Microsoft Visual C++ 2019

    Please note the CreateFile() code works in a non-UWP, win32 project compiled with VisStudio 2019.

    The missing DLL problems are resolved, because they were due to trying to start the .exe from Windows Explorer.

    Best regards,

    Craig

    Tuesday, August 13, 2019 4:56 PM
  • Getting same access denied return from CreateFile() in DeviceIoControlUwp sample project from Windows-iotcore-samples-develop.zip.

    Here is some of the output window text:

    'DeviceIoControlUwp.exe' (Win32): Loaded 'C:\Windows\System32\d2d1.dll'.
    Exception thrown at 0x00007FF8EA67A839 (KernelBase.dll) in DeviceIoControlUwp.exe: WinRT originate error - 0x80070005 : 'Failed to open COM port.'.
    'DeviceIoControlUwp.exe' (Win32): Loaded 'C:\Windows\System32\userenv.dll'.
    'DeviceIoControlUwp.exe' (Win32): Loaded 'C:\Windows\System32\profext.dll'.
    The thread 0x388 has exited with code 0 (0x0).
    Exception thrown at 0x00007FF8EA67A839 in DeviceIoControlUwp.exe: Microsoft C++ exception: Platform::Exception ^ at memory location 0x000000CF22FFCBA8. HRESULT:0x80070005 Access is denied.
    WinRT information: Failed to open COM port.
    Exception thrown at 0x00007FF8EA67A839 in DeviceIoControlUwp.exe: Microsoft C++ exception: Platform::AccessDeniedException ^ at memory location 0x000000CF22FFD480. HRESULT:0x80070005 Access is denied.
    WinRT information: Failed to open COM port.
    Stack trace:
     >[External Code]
     >DeviceIoControlUwp.exe!AccessComPort() Line 44
     >DeviceIoControlUwp.exe!DeviceIoControlUwp::MainPage::MainPage() Line 85
     >[External Code]
     >DeviceIoControlUwp.exe!DeviceIoControlUwp::App::[Windows::UI::Xaml::IApplicationOverrides]::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs ^ e) Line 78
     >[External Code]
    The program '[16252] DeviceIoControlUwp.exe' has exited with code 0 (0x0).

    -Craig

    Tuesday, August 13, 2019 5:16 PM
  • The thing is, CreateFile is documented to be desktop only, so I am not surprised that it fails in a UWP application.

    If you look in fileapi.h, you will find that CreateFile is placed in a block specified for desktop applications. In fact, by default, you shouldn't even see CreateFile defined.

    Have you tried using CreateFile2? This is documented to work with UWP.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Tuesday, August 13, 2019 7:53 PM
  • Hi Darran,

    Thanks for your suggestion.

    FYI,

    The projects containing CreateFile() are being used as-is directly out of the MSFT IoT core samples (Windows-iotcore-samples-develop.zip) which, as I understand things, must be UWP.

    And they explicitly have UWP in their names (e.g. CustomDeviceAccessorUWP).

    And they are not being blocked from compiling by flags in the .h files.

    So anyway, the MSFT samples seemed like a reasonable place to start.  Of course, the second UWP sample I tried (StreamSocket, Windows-universal-samples-master_UWPsamples.zip) fails to compile because of an error in one of the auto-generated xaml-related source files (\cpp\generated files\xamlbindinginfo.g.hpp(286): error C3699: '^': cannot use this indirection on type 'T').  So being 0 for 2, I'll lower my expectations, get back at it, and see if CreateFile2() can be made to work.

    Thanks,

    Craig

    Tuesday, August 13, 2019 10:06 PM
  • Hi,

    I suggest to test it again on Windows IOT Core. And feel free to let me know if it still has problem.

    Best regards,

    Drake


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 14, 2019 1:44 AM
  • Dear Drake,

    Based on your comment, am I correct in assuming you believe something different will happen when the code is run on the IOT computer as compared to a Windows10 desktop computer?

    At present I am not able to try that because we are still configuring the IOT computer and porting over our custom USB driver ported.  So while we work on the IOT hardware, I am trying to replicate our existing win32s c++ code under UWP.  Working at my desk, I have worked through switching to cfgmgr32.h from setupai.h to find our custom USB driver, but got stuck when moving on to the CreateFile and DeviceIOControl functions.  The CustomDeviceAccessorUWP sample looked to be on point and it compiled, even though CreateFile and DeviceIOControl did not appear to be valid UWP functions.

    Since CreateFile works in win32s code for both COM1 and our custom driver, I assume the UWP code is imposing some extra restrictions.  If it is not a digital signing issue, then is it a UWP restriction on file locations that is imposed on desktop Windows but not on IOT-core Windows?  Is there some method (permissions, registry, etc.) to allow me to test COM and USB connections with CreateFile under UWP on my Windows10 desktop?  It would make the development process very much simpler.

    Thanks,

    Craig

    Wednesday, August 14, 2019 5:36 PM
  • Hi,

    Since this is related to UWP, So I will move this to UWP forum for better support.

    Best regards,

    Drake


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 16, 2019 3:15 AM
  • Hi,

    Please check the following documents, Requirement for CreatefileA(), this method is not supported in UWP apps. But according to this document:Requirements for CreateFile2(), CreateFile2() is supported in UWP apps. So you will get error when you running the sample in UWP apps. My suggestion is that you might need to use CreateFile2() method instead of the original one when running in UWP apps.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, August 16, 2019 4:04 AM
    Moderator
  • Hi Roy,

    CreateFile2() gives the same access denied error for COM1.

    The projects containing CreateFile() are being used as-is directly out of the MSFT IoT core samples (Windows-iotcore-samples-develop.zip) which, as I understand things, must be UWP, and they are not being blocked from compiling by flags in the .h files.

    I suspect, but have not yet been able to test on our IoT device, that CreateFile and DeviceIOControl may actually work on Windows IoT.  Again I have not found a definitive source, but a number of web pages talk about MSFT having opened up these functions, at least for IoT, because enough people wanted to port over old, plain c++ code that used these functions for accessing hardware devices.

    If you know of a MSFT site that explicitly talks about Windows IoT and CreateFile/DeviceIOControl, I would be very interested in that.

    Thanks,

    Craig

    Friday, August 16, 2019 9:56 PM
  • Hi,

    So have you tried it in IOT devices?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 20, 2019 5:43 AM
    Moderator
  • Hi Roy,

    So far as I can tell, our current IOT device does not appear to have anything matching COM1 to test.  We have not yet been able to get the custom usb driver on the IOT device to test connecting to that.

    FYI, on my desktop Windows10 machine in a UWP project, CreateFile() returns "access denied" for COM1, but returns "file not found" for COM5.  So even if it is not supposed to work at all under UWP, CreateFile() sees the difference between COM5 which is not part of the computer and COM1 which is.

    These are the same returns as from CreateFile2().

    The same syntax can open a filehandle to COM1 when used in a win32 application.

    Best regards,

    Craig

    Tuesday, August 27, 2019 10:41 PM
  • Hi,

    So you just test the sample in desktop and the CreateFile2() also gave the same error, right?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 2, 2019 8:12 AM
    Moderator