WebView2Feedback icon indicating copy to clipboard operation
WebView2Feedback copied to clipboard

System.ArgumentException when Window has TopMost and program running under auto elevated permission

Open cuiliang opened this issue 5 years ago • 8 comments

Description Condition:

  • Window contains WebView2 has TopMost=true setted. In code or XAML.
  • Program has uiAccess="true" flag in manifest file.
  • Code sign program.
  • Start program from subfolders of 'c:\program files'

Exception: Value is not in the expected range

System.ArgumentException
   在 System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32, IntPtr)
   在 System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32)
   在 Microsoft.Web.WebView2.Core.CoreWebView2Environment+<CreateCoreWebView2ControllerAsync>d__20.MoveNext()
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
   在 Microsoft.Web.WebView2.Wpf.WebView2+<>c__DisplayClass25_0+<<EnsureCoreWebView2Async>g__Init|0>d.MoveNext()
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
   在 WpfApp2.MainWindow+<InitializeAsync>d__2.MoveNext()
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
   在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   在 System.Windows.Threading.DispatcherOperation.InvokeImpl()
   在 MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(System.Object)
   在 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   在 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   在 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   在 MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System.Threading.ContextCallback, System.Object)
   在 System.Windows.Threading.DispatcherOperation.Invoke()
   在 System.Windows.Threading.Dispatcher.ProcessQueue()
   在 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   在 MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
   在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
   在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   在 System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
   在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
   在 MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef)
   在 System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
   在 System.Windows.Application.RunDispatcher(System.Object)
   在 System.Windows.Application.RunInternal(System.Windows.Window)
   在 WpfApp2.App.Main()

code:

 public MainWindow()
        {
            InitializeComponent();

            InitializeAsync();
        }

        async void InitializeAsync()
        {

            try
            {
                var folder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Quicker",
                    "WebView2");
                var env = await CoreWebView2Environment.CreateAsync(null, folder).ConfigureAwait(true);
                await webView.EnsureCoreWebView2Async(env).ConfigureAwait(true);

                webView.UpdateWindowPos();

                webView.CoreWebView2.NavigateToString("<h1>Hello</h1>");

            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(ex.Message);
                sb.Append(ex.StackTrace);


                while (ex.InnerException != null)
                {
                    sb.Append("\n\n\n");
                    sb.AppendLine(ex.InnerException.Message);
                    sb.Append(ex.InnerException.StackTrace);
                    ex = ex.InnerException;
                }

                MessageBox.Show(sb.ToString());
            }
           
        }

Version SDK: 0.9.628-prerelease Runtime: 87.0.637.0 Framework: WPF with .Net 4.6.2 OS: Windows 10 2004(19041.450) , Windows 10 1909

Repro Steps

Screenshots

Additional context

AB#29614042

cuiliang avatar Sep 27 '20 06:09 cuiliang

I've filed this as a bug internally, we'll take a look.

Does this repro without "TopMost" being set? Does this repro when running not elevated? I need to confirm, but I think we don't support running elevated.

champnic avatar Sep 28 '20 17:09 champnic

@champnic It will be ok without Topmost or Not elevated.
Sample program attached. \bin\x64\Release\WpfApp2.exe was code signed. Copy \bin\x64\release to c:\program files\ it will report error.

WpfApp2.zip

I have a productivity software(link, Sorry only Chinese now, there are many gif demos on homepage) that is something like 'Shortcuts' on iOS, allow users define custom 'Actions' by 'Blocks' to interact with other softwares like this:

image

It need to be elavated to SendKeys / detect active window / SetForegroundWindow.

It also have UI blocks that allow users to input / select or do other interactions with 'Actions'.

I want to give users a WebView2 block, so that users can make html UI components or do some automation works. Html UI will be more flexable and powerful and give users much more possibility. image

So Please Please support running on elevated process.

cuiliang avatar Sep 28 '20 23:09 cuiliang

Is there any updates regarding above issue(Topmost and auto elevated set true=> webview2 page loading issue) ? I am also working on a task where the above scenario is applied.

sachin-jose avatar Feb 14 '22 11:02 sachin-jose

No update on this one. Running as admin is slowly becoming more supported though, so I'm bumping the priority on this issue. Thanks!

champnic avatar Feb 14 '22 20:02 champnic

I am also having similar problem where System.ArgumentException is raised if application have UiAccess enabled.

This exception was originally thrown at this call stack: System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(int) in Marshal.cs Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateCoreWebView2ControllerAsync(System.IntPtr) Microsoft.Web.WebView2.Wpf.WebView2.EnsureCoreWebView2Async.__Init|0() Microsoft.AspNetCore.Components.WebView.WebView2.WebView2WebViewManager.TryInitializeWebView2() Microsoft.AspNetCore.Components.WebView.WebView2.WebView2WebViewManager.NavigateCore.AnonymousMethod__0() Microsoft.AspNetCore.Components.WebView.Wpf.WpfDispatcher.InvokeAsync(System.Func<System.Threading.Tasks.Task>)

Is there any solution for this ?

olegsavelos avatar May 21 '23 07:05 olegsavelos

Is anyone actually ever going to be fixing those issues ? Its been 3 years since this was reported.

olegsavelos avatar Oct 17 '23 13:10 olegsavelos

Its been three years guys.

olegsavelos avatar Jun 28 '24 14:06 olegsavelos

four years

olegsavelos avatar Sep 20 '24 18:09 olegsavelos

We also face issue with our win32 webview app with UIAccess flag set and the TopMost property is set. WV controller creation fails with Parameter is incorrect error. Without the top most property seems to work fine. Is there any update on this issue?

mhdshameel avatar Oct 07 '24 15:10 mhdshameel

@mhdshameel yes it is related to topmost parameter, not sure how many years need to pass until ms address it.

olegsavelos avatar Oct 07 '24 15:10 olegsavelos