MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
444 stars 53 forks source link

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

Open cuiliang opened 4 years ago

cuiliang commented 4 years ago

Description Condition:

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

champnic commented 4 years ago

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.

cuiliang commented 4 years ago

@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.

sachin-jose commented 2 years ago

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.

champnic commented 2 years ago

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

olegsavelos commented 1 year ago

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 commented 11 months ago

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

olegsavelos commented 3 months ago

Its been three years guys.

olegsavelos commented 1 week ago

four years