How to fix Exception thrown: 'System.UnauthorizedAccessException' in WinRT.Runtime.dll
When calling BluetoothLEAdvertisementPublisher.Start(), I get "Exception thrown: 'System.UnauthorizedAccessException' in WinRT.Runtime.dll". This is an unpackaged version of the app with FullTrust. Even so, I have added 'bluetooth' to the capabilities in the Package.appxmanifest. This same code works under UWP, and WPF. What else can I do to enable BluetoothLEAdvertisement publishing in Win UI 3?
Windows App SDK
Windows 11
-
Jeanine Zhang-MSFT 9,661 Reputation points • Microsoft Vendor
2023-09-07T03:21:18.7933333+00:00 I followed the Doc: Bluetooth LE Advertisements to try to reproduce the issue.
I tried the code in a winui3 blank project (packaged or unpackaged), and I couldn't reproduce the issue. Even though I have no Bluetooth device on my computer, I couldn't reproduce this issue.
I suggest you could try to enable BluetoothLEAdvertisement publishing in a blank project. If you can reproduce the issue, could you please provide a complete reproducible sample in the blank project? And provide the detailed steps to help us reproduce the issue.
-
John Carroll 0 Reputation points
2023-09-07T18:09:47.6166667+00:00 Yes, I will get back to you asap on this. Thank you!
-
John Carroll 0 Reputation points
2023-09-07T18:27:31.21+00:00 Ok, I have a very simple version. Built from the sample app. How can I share it with you?
-
Jeanine Zhang-MSFT 9,661 Reputation points • Microsoft Vendor
2023-09-08T01:04:15.92+00:00 You could share your sample via github.
-
John Carroll 0 Reputation points
2023-09-08T02:44:08.0933333+00:00 Ok, here you go :)
-
Jeanine Zhang-MSFT 9,661 Reputation points • Microsoft Vendor
2023-09-08T06:36:39.7233333+00:00 According to the code you provided, I still couldn't reproduce the error . Have you tried to use the offical sample: https://learn.microsoft.com/en-us/windows/uwp/devices-sensors/ble-beacon#publishing-advertisements
-
John Carroll 0 Reputation points
2023-09-08T07:56:34.5+00:00 First, thank you for your assistance on this. Yes, I first wrote this code in C++ using WinRT in a dll, and it works for a win32 test application, it also works for a WPF test application using the same dll with a flat c wrapper (for interop between C# and C++), and it works for UWP (ported and rewritten entirely in C#, same code as in this WinUI3 test). It just doesn't work with WinUI3. I've worked with WinRT working on Ble both Avertisement, and Generic Attribute Profile classes exensively for over 2 years now. This is my first try with WinUI3. From the library point of view, I've tried everything I can think of. Here's some more details (not sure if it's reflected in the project), I'm building for a Target OS version of 10.0.19041.0, with a Supported OS Version of the same. I'm using Microsoft.WindowsAppSDK version 1.4.230822000. I'm building with VS Community version 17.7.2 64bit Debug. Here is the failure (I've bolded it, and provided a little before and after context):
'TestingBleUnderWinUI3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\john.carroll\source\repos\TestingBleUnderWinUI3\TestingBleUnderWinUI3\bin\x86\Debug\net6.0-windows10.0.19041.0\win10-x86\Microsoft.InteractiveExperiences.Projection.dll'.
'TestingBleUnderWinUI3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\john.carroll\source\repos\TestingBleUnderWinUI3\TestingBleUnderWinUI3\bin\x86\Debug\net6.0-windows10.0.19041.0\win10-x86\System.Linq.Expressions.dll'.
'TestingBleUnderWinUI3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\john.carroll\source\repos\TestingBleUnderWinUI3\TestingBleUnderWinUI3\bin\x86\Debug\net6.0-windows10.0.19041.0\win10-x86\System.Reflection.Emit.dll'.
'TestingBleUnderWinUI3.exe' (CoreCLR: clrhost): Loaded 'Snippets'.
Exception thrown: 'System.UnauthorizedAccessException' in WinRT.Runtime.dll
Access is denied. (0x80070005 (E_ACCESSDENIED))
'TestingBleUnderWinUI3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\john.carroll\source\repos\TestingBleUnderWinUI3\TestingBleUnderWinUI3\bin\x86\Debug\net6.0-windows10.0.19041.0\win10-x86\System.Text.Encoding.Extensions.dll'.
'TestingBleUnderWinUI3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\john.carroll\source\repos\TestingBleUnderWinUI3\TestingBleUnderWinUI3\bin\x86\Debug\net6.0-windows10.0.19041.0\win10-x86\System.Diagnostics.StackTrace.dll'.
'TestingBleUnderWinUI3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\john.carroll\source\repos\TestingBleUnderWinUI3\TestingBleUnderWinUI3\bin\x86\Debug\net6.0-windows10.0.19041.0\win10-x86\System.Reflection.Metadata.dll'.
'TestingBleUnderWinUI3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\john.carroll\source\repos\TestingBleUnderWinUI3\TestingBleUnderWinUI3\bin\x86\Debug\net6.0-windows10.0.19041.0\win10-x86\System.Collections.Immutable.dll'.
Here's the excpetion detail:
System.UnauthorizedAccessException HResult=0x80070005 Message=Access is denied. (0x80070005 (E_ACCESSDENIED)) Source=WinRT.Runtime StackTrace: at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
And here is the call stack when the exception is thrown:
WinRT.Runtime.dll!WinRT.ExceptionHelpers.ThrowExceptionForHR.__Throw|20_0(int hr) Unknown Microsoft.Windows.SDK.NET.dll!ABI.Windows.Devices.Bluetooth.Advertisement.IBluetoothLEAdvertisementPublisherMethods.Start(WinRT.IObjectReference _obj) Unknown Microsoft.Windows.SDK.NET.dll!Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementPublisher.Start() Unknown > TestingBleUnderWinUI3.dll!WinUI3Test.Bluetooth.BleAdvertisements.StartAdvertising(bool useExtendedAdvertising, bool sendTxPowerLevel, short? preferredTxPowerLevel) Line 193 C# TestingBleUnderWinUI3.dll!WinUI3Test.Bluetooth.BluetoothManager.Initialize() Line 19 C# TestingBleUnderWinUI3.dll!TestingBleUnderWinUI3.MainWindow.myButton_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) Line 37 C# Microsoft.WinUI.dll!WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.GetEventInvoke.AnonymousMethod__1_0(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) Line 21 C# Microsoft.WinUI.dll!ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(System.IntPtr thisPtr, System.IntPtr sender, System.IntPtr e) Line 136 C# [Native to Managed Transition] [Managed to Native Transition] Microsoft.WinUI.dll!ABI.Microsoft.UI.Xaml.IApplicationStaticsMethods.Start(WinRT.IObjectReference _obj, Microsoft.UI.Xaml.ApplicationInitializationCallback callback) Line 18 C# Microsoft.WinUI.dll!Microsoft.UI.Xaml.Application.Start(Microsoft.UI.Xaml.ApplicationInitializationCallback callback) Line 252 C# TestingBleUnderWinUI3.dll!TestingBleUnderWinUI3.Program.Main(string[] args) Line 31 C#
-
Jeanine Zhang-MSFT 9,661 Reputation points • Microsoft Vendor
2023-09-08T08:51:45.2766667+00:00 WindowsAppSDK version 1.4.230822000.
You could try to use WindowsAppSDK version 1.2 or WindowsAppSDK version 1.3.
If the issue persists, I suggest you could post the issue to Github Issue for better help.
-
Castorix31 84,466 Reputation points
2023-09-08T09:52:06.9733333+00:00 Your sample also works normally for me (Windows 10 22H2, Unpackaged or Packaged)
-
John Carroll 0 Reputation points
2023-09-08T13:56:15.01+00:00 Deleted comment, rewritten below.
-
John Carroll 0 Reputation points
2023-09-08T14:01:45.04+00:00 One last Comment, I am on Windows 11 Pro 22H2. To actually stop on the exception, you have to enable All Common Language Runtime Exceptions in the Exception Settings. Otherwise, it's easy to miss as without that setting it just logs the exception in the output and continues on its merry way. Thanks again Jeanine for all of your help on this and thank you too Castorix31.
I will try falling back on the SDK versions, and if that doesn't work, I will try the Github Issue route as suggested.
-
John Carroll 0 Reputation points
2023-09-08T14:29:30.42+00:00 Ok, tried WindowsAppSdk 1.3, and 1.2 no go with either one. I'll try the Github Issue route as suggested for resolution.
-
John Carroll 0 Reputation points
2023-09-08T20:08:55.4133333+00:00 I have found the issue. If I set the Target OS Version to 10.0.19041.0, and the Supported OS Version to the same, and then try to set:
_bluetoothLEAdvertisementPublisher.UseExtendedAdvertisement = useExtendedAdvertising; _bluetoothLEAdvertisementPublisher.IncludeTransmitPowerLevel = sendTxPowerLevel; _bluetoothLEAdvertisementPublisher.PreferredTransmitPowerLevelInDBm = preferredTxPowerLevel;
It will fail. If I comment out the above code and set the Target OS Version to the above and the Supported OS Version to 10.0.17763.0 it doesn't fail, but if I uncomment out the above code the VS Editor tells me that the call-sites in the above code are only supported on Windows 10.0.17763 and later. Seems like there's some discombobulation here. Will try adding 'guards' around this code.
-
John Carroll 0 Reputation points
2023-09-08T20:20:55.7433333+00:00 Ok, no matter which way I go, adding guards, or setting the Supporting OS, I get the unauthorized error when setting the BluetoothLEAdvertisementPublisher properties. This seems like a root cause.
-
John Carroll 0 Reputation points
2023-09-08T20:22:02.62+00:00 I'm currently setting those values to true, true, and -10 respectively.
-
Jeanine Zhang-MSFT 9,661 Reputation points • Microsoft Vendor
2023-09-11T06:22:40.19+00:00 I suggest you could post the issue to Github Issue for better help.
-
John Carroll 0 Reputation points
2023-09-11T14:38:21.5333333+00:00 Thank you, Jeanine, for your help. If this continues to be a problem, I will go to Github.
Sign in to comment