jfversluis / Plugin.Maui.ScreenBrightness

Plugin.Maui.ScreenBrightness provides the ability to get or set the screen brightness inside a .NET MAUI application.
MIT License
44 stars 3 forks source link

System.Management.ManagementException: Not supported on Windows #9

Open DeanZhuo opened 1 month ago

DeanZhuo commented 1 month ago

I tried the Plugin and it works like magic on my laptop (win10, 10.0.19045), both on Debug and Release. But then when I deployed it to my target device (win11, 10.0.22631), the app crashed and I found this on AppCenter:

System.Management ManagementException.ThrowWithExtendedInfo (ManagementStatus errorCode) System.Management.ManagementObjectCollection ManagementObjectEnumerator.MoveNext () Plugin.Maui.ScreenBrightness ScreenBrightnessImplementation.get_Brightness () GlassCockpit.Components.FLIRView GlassCockpit.Components.FLIRView..ctor() GlassCockpit MainPage.InitializeComponent () GlassCockpit.MainPage GlassCockpit.MainPage..ctor(MainViewModel vm) System RuntimeMethodHandle.InvokeMethod (Object target, Void** arguments, Signature sig, Boolean isConstructor) System.Reflection MethodBaseInvoker.InvokeDirectByRefWithFewArgs (Object obj, Span1 copyOfArgs, BindingFlags invokeAttr) System.Reflection MethodBaseInvoker.InvokeWithOneArg (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) System.Reflection RuntimeConstructorInfo.Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) Microsoft.Extensions.DependencyInjection.ServiceLookup CallSiteRuntimeResolver.VisitConstructor (ConstructorCallSite constructorCallSite, RuntimeResolverContext context) Microsoft.Extensions.DependencyInjection.ServiceLookup CallSiteRuntimeResolver.VisitRootCache (ServiceCallSite callSite, RuntimeResolverContext context) Microsoft.Extensions.DependencyInjection.ServiceLookup CallSiteVisitor2.VisitCallSite (ServiceCallSite callSite, TArgument argument) Microsoft.Extensions.DependencyInjection.ServiceLookup CallSiteRuntimeResolver.Resolve (ServiceCallSite callSite, ServiceProviderEngineScope scope) Microsoft.Extensions.DependencyInjection ServiceProvider.CreateServiceAccessor (ServiceIdentifier serviceIdentifier) System.Collections.Concurrent ConcurrentDictionary2.GetOrAdd (TKey key, Func2 valueFactory) Microsoft.Extensions.DependencyInjection ServiceProvider.GetService (ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope) Microsoft.Extensions.DependencyInjection.ServiceLookup ServiceProviderEngineScope.GetService (Type serviceType) Microsoft.Maui.MauiContext WrappedServiceProvider.GetService (Type serviceType) Microsoft.Maui.Controls.ShellContent.<>cDisplayClass19_0.<Microsoft.Maui.Controls IShellContentController.GetOrCreateContent>b__0 () Microsoft.Maui.Controls ElementTemplate.CreateContent () Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls IShellContentController.GetOrCreateContent () Microsoft.Maui.Controls.Handlers ShellContentHandler.CreatePlatformElement () Microsoft.Maui.Handlers ElementHandler`2.OnCreatePlatformElement () Microsoft.Maui.Handlers ElementHandler.SetVirtualView (IElement view) Microsoft.Maui.Controls Element.SetHandler (IElementHandler newHandler) Microsoft.Maui.Platform ElementExtensions.ToHandler (IElement view, IMauiContext context) Microsoft.Maui.Platform ElementExtensions.ToPlatform (IElement view, IMauiContext context) Microsoft.Maui.Controls.Handlers ShellSectionHandler.SyncNavigationStack (Boolean animated, NavigationRequestedEventArgs e) Microsoft.Maui PropertyMapper.UpdateProperties (IElementHandler viewHandler, IElement virtualView) Microsoft.Maui.Controls.Handlers ShellSectionHandler.SetVirtualView (IElement view) Microsoft.Maui.Controls Element.SetHandler (IElementHandler newHandler) Microsoft.Maui.Platform ElementExtensions.ToHandler (IElement view, IMauiContext context) Microsoft.Maui.Controls.Handlers ShellItemHandler.UpdateCurrentItem () Microsoft.Maui PropertyMapper.UpdateProperties (IElementHandler viewHandler, IElement virtualView) Microsoft.Maui.Controls.Handlers ShellItemHandler.SetVirtualView (IElement view) Microsoft.Maui.Controls Element.SetHandler (IElementHandler newHandler) Microsoft.Maui.Platform ElementExtensions.ToHandler (IElement view, IMauiContext context) Microsoft.Maui.Controls.Platform ShellView.CreateShellItemView () Microsoft.Maui.Controls.Platform ShellView.SwitchShellItem (ShellItem newItem, Boolean animate) Microsoft.Maui PropertyMapper.UpdateProperties (IElementHandler viewHandler, IElement virtualView) Microsoft.Maui.Controls.Handlers ShellHandler.SetVirtualView (IView view) Microsoft.Maui.Controls Element.SetHandler (IElementHandler newHandler) Microsoft.Maui.Platform ElementExtensions.ToHandler (IElement view, IMauiContext context) Microsoft.Maui.Platform ElementExtensions.ToPlatform (IElement view, IMauiContext context) Microsoft.Maui.Handlers WindowHandler.MapContent (IWindowHandler handler, IWindow window) Microsoft.Maui PropertyMapper.UpdateProperties (IElementHandler viewHandler, IElement virtualView) Microsoft.Maui.Controls Element.SetHandler (IElementHandler newHandler) Microsoft.Maui.Platform ElementExtensions.SetHandler (IWinRTObject nativeElement, IElement element, IMauiContext context) Microsoft.Maui.Platform ApplicationExtensions.CreatePlatformWindow (Application platformApplication, IApplication application, OpenWindowRequest args) Microsoft.Maui MauiWinUIApplication.OnLaunched (LaunchActivatedEventArgs args) Microsoft.UI.Xaml.Application.Microsoft.UI.Xaml IApplicationOverrides.OnLaunched (LaunchActivatedEventArgs args) ABI.Microsoft.UI.Xaml IApplicationOverrides.Do_Abi_OnLaunched_0 (IntPtr thisPtr, IntPtr args) WinRT ExceptionHelpers.gThrow|39_0 (Int32 hr) WinRT ExceptionHelpers.ThrowExceptionForHR (Int32 hr) ABI.Windows.ApplicationModel.Core IUnhandledErrorMethods.Propagate (IObjectReference _obj) Windows.ApplicationModel.Core UnhandledError.Propagate () Microsoft.AppCenter.Utils ApplicationLifecycleHelperWinUI.b__0_3 (Object sender, UnhandledErrorDetectedEventArgs eventArgs)

My implementation using two buttons:

private void FlirUp_Clicked(object sender, EventArgs e)
{
    ScreenBrightness.Default.Brightness += ScreenBrightness.Default.Brightness != 1 ? 0.01f : 0;
    flirLabel.Text = ScreenBrightness.Default.Brightness.ToString();
}

private void FlirDown_Clicked(object sender, EventArgs e)
{
    ScreenBrightness.Default.Brightness -= ScreenBrightness.Default.Brightness != 0 ? 0.01f : 0;
    flirLabel.Text = ScreenBrightness.Default.Brightness.ToString();
}

Do I need some kind of additional permission?

jfversluis commented 2 weeks ago

Not that I know of, did you figure this one out?

DeanZhuo commented 5 days ago

I found some that said it only affects the main screen where the screen brightness could be set from the display setting. I was developing the app using a laptop, so it works. My target device is using an external monitor where I can only set the brightness from the monitor setting (not from the windows display setting).

It work great on laptop and android device, but for PC with external monitor I'm using:

[DllImport("dxva2.dll", EntryPoint = "SetMonitorBrightness")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetMonitorBrightness(IntPtr handle, uint newBrightness);

and user32.dll to find the monitor.