3

My library should have different behaviour depending on app platform. As I know, I can determine that app is UWP-application using this code. But it returns true even for WinUI3 Win32 app.

As there any way to determine that app is WinUI3 Win32 app and can call any APIs outside of AppContainer sandbox?

okolobaxa
  • 338
  • 1
  • 4
  • 16
  • 1
    The thing you want to do that is blocked by the sandbox -- just try it and see if it succeeds, or if it fails with ERROR_ACCESS_DENIED. – Raymond Chen Dec 26 '21 at 22:34
  • @RaymondChen There should be more elegant way – okolobaxa Dec 29 '21 at 21:45
  • The elegant way to know if X is possible is to try X and see if it works. If you say "If Y is true, then I will assume I can to X", then you will run into problems when the relationship between X and Y changes. If the rules for what can and cannot be done from the sandbox change (and they do change), your assumptions may stop being valid. If you share more details about the API you want to call, maybe we can come up with efficient ways of detecting support for it. – Raymond Chen Dec 30 '21 at 00:12
  • Depending on what you are trying to accomplish, the elegant way would probably be to target multiple frameworks, use preprocessor symbols in your code and distribute separate assemblies for the `uap` and`net*` frameworks. – mm8 Jan 03 '22 at 19:01
  • @mm8 my lib is used to set-up a proxy. For win32 apps I'd like to set up system-wide proxy automatically, but for UWP apps it is not possible due to security restrictions. So I need a helper,. that could determine current platform – okolobaxa Jan 04 '22 at 20:10

1 Answers1

0

I guess you could use the Window.Current property to determine the AppModel of the app.

It returns a CoreWindow when called from a UI thread in a UWP app and it always returns null in a desktop app:

if (Window.Current != null)
{
    // UWP app model
}
mm8
  • 163,881
  • 10
  • 57
  • 88