MicrosoftEdge / WebView2Feedback

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

WebView2 on Custom Desktop without Explorer.exe #960

Open dbuechel opened 3 years ago

dbuechel commented 3 years ago

We plan to integrate WebView2 into our lockdown application Safe Exam Browser. We're using CreateDesktop and its related functions to create and activate a custom desktop on which we run our main UI. In the process of implementing the WebView2 integration, we had to notice that it doesn't seem to work on our custom desktop where explorer.exe isn't running.

We have found quite an old issue on the topic (#48), but are not sure whether it still is accurate. Thus we'd first like to ask whether you could confirm that the incompatibility is still the case, and secondly, if it is, whether there are any plans to make WebView2 work on custom desktops?

AB#31945291

dbuechel commented 3 years ago

@champnic @jasonstephen15 We were planing to integrate WebView2 in our next version, but are currently blocked due to this problem. Would it be possible for someone from MSFT to have a look at this issue and potentially provide feedback?

champnic commented 3 years ago

@dbuechel Sorry this is causing problems and blocking you. I don't think anyone has tested desktop compatibility since that issue you linked, and we don't have current plans in this area. Are you running into a particular bug or exception when trying to launch WebView2 on the custom desktop? If so can you add info and describe your repro steps in detail or provide a sample app?

dbuechel commented 3 years ago

Thanks a lot for your response. I haven't noticed any exception or alike, the problem is that the WebView2 content area remains empty (white), a few times the content was rendered but then disappeared once interacting with it. There also were severe rendering issues (black squares) when resizing the window holding the WebView2 control.

You can test the behavior by installing the following development build of Safe Exam Browser (the setup bundle automatically installs the correct version and dependencies, though not yet the WebView2 runtime which you'd have to install manually): https://sebdev-let.ethz.ch/project/AppVeyor/seb-win-refactoring/builds/299/job/dxao0jjj8plp1rp6/artifacts

champnic commented 3 years ago

Thanks for the repo app - I've added this as a bug on our backlog.

dbuechel commented 3 years ago

Excellent, thanks a lot! We would really like to eventually also replace our main browser engine (CefSharp / CEF) with WebView2, but as long as this incompatibility exists, we won't be able to do so.

dbuechel commented 3 years ago

@champnic Is there any chance that you could specify a time horizon for this issue? We happen to be blocked by several issues in our current browser engine and would thus like to replace it with WebView2 as soon as possible (i.e. once this issue is fixed).

champnic commented 3 years ago

No specific timeline yet, but I've just upped the priority on it. We'll try to start taking a look next week.

dbuechel commented 3 years ago

Great, good to know and thanks a lot for the excellent support!

bhiggins107 commented 3 years ago

Is this issue going to get fixed this time? It has been raised previously for both WebView and WebView2 with no resolution.

We are effectively seeing the same issue, where WebView2 fails to initialize if explorer.exe is not running.

Exception: Type: System.Runtime.InteropServices.COMException Message: Element not found. (Exception from HRESULT: 0x80070490) Source: mscorlib Stacktrace: at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode) at Microsoft.Web.WebView2.Core.CoreWebView2Environment.d32.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Web.WebView2.Wpf.WebView2.<>cDisplayClass25_0.<g__Init|0>d.MoveNext()

champnic commented 3 years ago

We don't know the root cause of the issue yet, so it's unclear what the fix is, the difficulty of the fix, or whether it will be possible. We have identified one case when running as admin which does require explorer.exe to open a Medium IL process, but we would expect this to work when not running as admin and so are digging into that case next.

jamesoli commented 3 years ago

@dbuechel Hi, I'm taking a look at the issue right now. Is there any chance you can give me a simplified version of the program just so I can reproduce the issue a little easier? If it's too difficult to simplify from your existing app, can you tell me what parameters you call CreateDesktop with and if your application is also running from this desktop or is it running from another one?

Also, can you try creating the WebView2 with an additional parameter passed in:

auto options = Microsoft::WRL::Make(); options->put_AdditionalBrowserArguments(L"--no-sandbox"); CreateCoreWebView2EnvironmentWithOptions(browser_path, user_data_folder, options, ...);

That's not a solution, but just trying to diagnose if creating sandboxes fail on a separate desktop.

One last thing, what version of Windows are you running this on? And are you using any Windows Jobs?

Thanks!

dbuechel commented 3 years ago

@jamesoli Excellent, thank you for investigating the issue, it means a lot to us! I have attached a simplified version I used to investigate the issue when I was trying to integrate WebView2 into our software: NewDesktopBug.zip. After building and before executing, you'd need to copy the output of the WindowsFormsApp project into the output folder of the NewDesktopBug project.

In SEB, we have the runtime component (which is a WPF application) which handles the creation, activation and closure of new desktops. Once the new desktop is activated, the runtime starts a new process, the client component (also a WPF application), which then runs on the new desktop and creates the WebView2. You can find the relevant code for creating desktops here:

  1. https://github.com/SafeExamBrowser/seb-win-refactoring/blob/master/SafeExamBrowser.Runtime/Operations/KioskModeOperation.cs#L121
  2. https://github.com/SafeExamBrowser/seb-win-refactoring/blob/master/SafeExamBrowser.WindowsApi/DesktopFactory.cs
  3. https://github.com/SafeExamBrowser/seb-win-refactoring/blob/master/SafeExamBrowser.WindowsApi/Desktop.cs
  4. https://github.com/SafeExamBrowser/seb-win-refactoring/blob/master/SafeExamBrowser.WindowsApi/User32.cs

I'll see that I can try the additional parameters you mentioned, though we're using the .NET version, but I assume that they work the same as the Win32 version?

I am currently running Windows 10 version 2004 build 19041.572, and we're not using any Windows jobs.

Thanks again for the excellent support with this issue!

jamesoli commented 3 years ago

Thanks for the simplified repro app! I was able to get it to run with our sample winforms app. Instead of using the included winforms app that you provided in the solution, I used our winforms browser sample here (As it already creates a WebView2): https://github.com/MicrosoftEdge/WebView2Samples/tree/master/SampleApps/WebView2WindowsFormsBrowser and did the same thing in the Main function that you did in WindowsFormsApp.

So it seems like I can get it to run just fine on my computer. There's a dark background for the desktop but the webview displays the content just fine and it doesn't seem to crash. Is there something I'm missing here? Do I need to do something else to make sure explorer.exe isn't running for example?

dbuechel commented 3 years ago

Thanks a lot for your help. This is more than mysterious, because in my case it neither works with the repro app nor in our production software. There were a very few times where the content was rendered, but as soon as I resized the window or tried to interact with the web page, the content disappeared.

I am not able to build and test the sample app, I get WebView2WindowsFormsBrowser.csproj : error : Project file is incomplete. Expected imports are missing.. I am using Visual Studio Enterprise 2017 and do not have .NET 5 installed, our software still runs on .NET Framework 4.7.2. Could that be the issue?

Did you try it with the WindowsFormsApp as well, i.e. does it also work there for you? When creating the new desktop, explorer.exe shouldn't be running by default (e.g. no shell with taskbar etc.), so I think that isn't the case. Of course it is possible that I am doing something wrong when integrating the WebView2, but I can't really see what that would be. And apparently there are also other people having the same issue.

One thing that I forgot to mention above is how we start the client component on the new desktop: https://github.com/SafeExamBrowser/seb-win-refactoring/blob/master/SafeExamBrowser.WindowsApi/ProcessFactory.cs#L189. I don't think the issue lies there, but one never knows...

jamesoli commented 3 years ago

Are you talking about setting STARTUPINFO.lpDesktop? I think your repro app does that too.

Do you happen to only run your app as administrator? In that mode, we do require explorer to launch our webview in medium IL and I will get the same problem you're having. The other issue you mentioned #48 where @david-risney tested seemed to indicate that WebView2 was working correctly on other desktops in general as well although I'm not sure he tested with admin mode.

As for building the project, I don't think you need .NET 5. Maybe 4.8.

dbuechel commented 3 years ago

Are you talking about setting STARTUPINFO.lpDesktop? I think your repro app does that too.

Yes, exactly.

Do you happen to only run your app as administrator?

No, we don't run our app as administrator, it runs with normal user privileges.

I've meanwhile managed to build the official sample application you mentioned and unfortunately do experience the same issue there as well. And as @bhiggins107 is stating above, it doesn't really seem to work in general (though in my case, I do not encounter any exceptions, it simply fails to load the content in the manner I described above).

For us it really is a showstopper if the WebView2 can't be used without explorer.exe resp. on custom desktops, depending on what the underlying issue turns out to be. We'd be thus be extremely grateful if the issue would be investigated further and eventually fixed, if possible.

bhiggins107 commented 3 years ago

@jamesoli

The sample application you linked above (https://github.com/MicrosoftEdge/WebView2Samples/tree/master/SampleApps/WebView2WindowsFormsBrowser) is using the Evergreen version of the WebView2 runtime. Are there fixes in the latest Evergreen code to deal with explorer issues that have not made it into the fixed version of the runtime (v89) yet?

jamesoli commented 3 years ago

@bhiggins107 I'm not sure what the problem actually is yet as I haven't been able to reproduce the issue. There is always the chance that some other code change we did fixed this issue recently and that's why I haven't been able to see it. The chances of that seem to be small though.

@dbuechel Can you confirm that the process is not crashing? Can you bring up task manager when you're on the separate desktop (ctrl + alt + del -> Task Manager or ctrl + shift + esc should still work) and see if the msedgewebview2.exe processes still exist when you see the issue? If it doesn't exist, it could have crashed and you can get a full dump with these instructions: https://docs.microsoft.com/en-us/windows/win32/wer/collecting-user-mode-dumps

If they all still exist, then it could be that the process is just stuck waiting on something and not responding. I would need to be able to reproduce the issue in that case.

jamesoli commented 3 years ago

Ok, I have been able to reproduce the issue. I think it may be a navigation/timing issue on a new desktop. In your CoreWebView2InitializationCompleted handler, can you navigate to a site and then sleep for 2 seconds and then navigate to a different site and see if it works (And don't set the source property separately.)?

So something like:

CoreWebView2InitializationCompletedHandler(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
  ...
  this.webView2Control.CoreWebView2.Navigate("https://www.microsoft.com");
  System.Threading.Thread.Sleep(2000);
  this.webView2Control.CoreWebView2.Navigate("https://www.bing.com");
  ...
}

If this indeed is the same issue you're experiencing then you don't need to do any of the other things and I'll be looking into this further.

dbuechel commented 3 years ago

Thanks a lot for following up with this issue. I have tried the Thread.Sleep(...) in between the navigation calls, but unfortunately with no success, the WebView2 remains blank (white). And to answer your previous question: Yes, I can confirm that the process isn't crashing, I don't see anything in the Windows event log nor does the WebView2 process terminate unexpectedly when running on a new desktop.

jamesoli commented 3 years ago

@dbuechel I wanted to see if this is the same issue I'm seeing. I've made another branch off of the sample app I linked above here: https://github.com/MicrosoftEdge/WebView2Samples/tree/CreateDesktop/SampleApps/WebView2WindowsFormsBrowser

That has the Thread.Sleep workaround for the issue I was seeing with the blank webview.

You just have to modify the NewDesktopBug application to launch the webview2 browser with this change in the OpenForm function: var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + $@"\WebView2WindowsFormsBrowser.exe {temporary.handle}";

Put NewDesktopBug.exe in the same folder as WebView2WindowsFormsBrowser.exe that is built from the solution and see if you can get the webview to navigate.

dbuechel commented 3 years ago

Yes, I am still experiencing the same issue, i.e. the WebView2 remains blank and I am unable to interact with it.

bhiggins107 commented 3 years ago

Do you happen to only run your app as administrator? In that mode, we do require explorer to launch our webview in medium IL and I will get the same problem you're having.

@jamesoli Is the Explorer requirement for administrators a technical limitation, or is it something related to security? What do you recommend as a workaround in a kiosk situation where the application requires administrator privileges?

mdongilli commented 3 years ago

Sorry, we have the same problem, starting our app as admin got the issue, start the app without runas, the app works perfectly.

bhiggins107 commented 3 years ago

@jamesoli Do you have any update on this issue, or on my question from a week ago? Thanks.

jamesoli commented 3 years ago

@bhiggins107 @mdongilli Sorry for the delayed response. We are still working on the issue but as for the admin problem, the design of WebView2 is that if the client creating the WebView has admin rights, we require explorer to launch our WebView2 process (msedgewebview2.exe) as medium IL.

jamesoli commented 3 years ago

To clarify, the intent of launching webview2 as non admin was for security reasons and using shell is a technical limitation. In the case where you have no shell.. we may investigate the ramifications of allowing webview to launch as admin and the changes that need to be made to allow it.

dbuechel commented 3 years ago

@jamesoli Are there any updates regarding this issue?

dbuechel commented 3 years ago

This is still quite a big issue for us and forces us to keep a pretty inconvenient workaround in our code. And to clarify, we do not run our app with elevated privileges / as admin but nonetheless experience the issue originally posted above. Any feedback or updates regarding this topic would be highly appreciated.

alesebi91 commented 3 years ago

I have the same issue with my app running in kiosk mode (explorer.exe not available). I have already tryied to run the app with standard privileges but issue still there.

I currently use CefSharp but I would like to use WebView2.

Any feedback would be higly appreciated!

Thanks

vicglarson commented 3 years ago

We have the same issue. App ran with standard privileges on non-default desktop. --no-sendbox env variable and disabled uac didn't help. Webview2 crashes. Is any other option to workaround this problem?

cfrank19 commented 2 years ago

Any updates on this? We are running into this issue - however only on Win10 devices, no issues on Win7 with the same setup.

WebView2 version: Fixed Runtime 92.0.902.73 Winform app with Elevated privileges and non explorer.exe desktop Issue Win10 build: 18363.900 and 19041.264 Working Win7 build (x64 and x32): 7601 (SP1)

champnic commented 2 years ago

No tangible update, although we are discussing potential designs on how we de-elevate the process that would avoid this issue.

ludan508 commented 2 years ago

This text was translated with google translator

Good day, A few months ago I bought a map component from the TMS Software brand, which uses the "msedgewebview2.exe" application to use the browser to access google maps.

I send you a small video of how the application fails me. I will explain:

My users enter the point of sale system through remote desktop, and they enter the application directly, they do not have access to the windows explorer (explorer.exe).

If you see in the video, in the task manager, when executing the pMapas.exe application, it does nothing, it just gives me a blank screen, and it doesn't load "msedgewebview2.exe", so I have to end the process " pMaps.exe".

The Maps application works correctly for me, only if I run the "explorer.exe" application first, opening the windows explorer, doing this, if I press the Maps button again, it opens the application loading "msedgewebview2.exe".

Do I need any special configuration of the "msedgewebview2.exe" component to work without the "explorer.exe"? Or what do you recommend me to do?

The attached video is compressed in a .zip file, which is in .mp4 format

EdgeWebView versión: 102.0.1245.41 Windows Server 2008 R2 Enterprise SP1 The users works on remote desktop

Ejemplo_Mapa_convertido.zip

bhiggins107 commented 2 years ago

@champnic

Is there any update on this issue?

Earlier in this thread, @jamesoli, said that "To clarify, the intent of launching webview2 as non admin was for security reasons and using shell is a technical limitation. "

I don't really understand this statement. Are you protecting my aging in-laws from web threats? Is there a reason the same restrictions have to be enforced on Win10 IoT Enterprise? If someone has our device pen tested and asks why we are running explorer when it is not required (this has happened, by the way), how do we explain to them that Microsoft says it is more secure to have explorer present than it is to run webview2 without it?

From my point of view, in an embedded situation, there is a major gap in the underlying logic here.

memiamp commented 2 years ago

We have experienced a similar issue to this where we defined a custom shell in Winlogon and the WebView2 component was not activating correctly.

When using the WebView2 Runtime version 103.0.1264.49, we experienced a COMException in forms application with error code 0x80070490. However, if we replaced the customer shell in Winlogon with explorer.exe and rebooted, the control was created successfully and worked as expected.

Somewhat inexplicably, uninstalling the WebView2 Runtime 103.0.1264.49 and then installing 102.0.1245.41 allowed the component to work with the custom shell. No elevation was required for the control to operate as required.

Conclusion\Work-around

We found that using the version 102.0.1245.41 of the WebView2 Runtime allowed the WebView2 control to work with a custom shell.

champnic commented 2 years ago

@LiangTheDev Did we make recent changes in how we use explorer.exe recently?

LiangTheDev commented 2 years ago

Yes, there is a recent change in 103 to address https://github.com/MicrosoftEdge/WebView2Feedback/issues/2356. The change was to de-elevate when launching webview browser process for apps running by admin user. The "de-elevate" means launch via explorer. As it made it worse for the scenarios like the above, we've modified it further in Edge 105 (which is scheduled to be released in the week of 01-Sep-2022) to try simple CreateProcess if de-elevate failed. So, the above scenario will work again with Edge 105.

jeroenhabets commented 1 year ago

Yes, there is a recent change in 103 to address #2356. ... we've modified it further in Edge 105 (which is scheduled to be released in the week of 01-Sep-2022) ... So, the above scenario will work again with Edge 105.

Since @LiangTheDev's comment we are on Edge 111 so I was hopeful but @jamesoli when I understand the issues closed since then correctly, this issue is still not resolved, right?

As a user of @dbuechel 's excellent Safe Exam Browser, I'd love to see this get resolved. Hence my humble question: if it hasn't been already are they any plans perhaps to get this fixed?

LiangTheDev commented 1 year ago

There is no specific timeline for when to solve all the issues for apps running as admin.

We are trying to improve experience for apps running as admin and made a change recently in Edge 112 though. Could you please try it out to see if that works better?

jeroenhabets commented 1 year ago

Hi @LiangTheDev, thanks! Sorry, I can neither readily test as unlike @dbuechel, I am not one of the developers of Safe Exam Browser. Nor can I assess if that change covers enough of his original request to make it worthwhile a test for their project. Cheers, Jeroen

dbuechel commented 1 year ago

I unfortunately won't be able to having a look at it before end of March, as we're currently extremely busy with preparing the upcoming release version. But just to reiterate what I've already mentioned almost 2 years ago: The Safe Exam Browser component using WebView2 is not running with elevated privileges.

sln162 commented 1 year ago

@LiangTheDev I am looking forward to the changes in version 112. Among the few questions I raised, there are two or three error codes that only appear when running with administrator privileges. If you do not run with administrator privileges, you can initialize normally.

jeroenhabets commented 1 year ago

@sln162 just to keep things clear, also for @LiangTheDev seeing his last comment, this issue is not about running with admin privileges but rather "WebView2 on Custom Desktop without Explorer.exe" (see title). This was also "reiterated" by @dbuechel as we'd need to see that "without Explorer.exe" resolved to resolve his (and mine as a user) known SEB issue.

jeroenhabets commented 1 year ago

@LiangTheDev did my (last) comment in March clarify things? (In full appreciation): Any chance to get the run "without Explorer.exe" resolved any time soon?

LiangTheDev commented 1 year ago

It makes it clear that it is not apps running elevated. I'll discuss with the team to see whether we could make progress on the issue.

FWIW, it is possible to create WebView2 for normal apps without explorer.exe running (like killed), the key should be running in separate desktop.

dbuechel commented 1 year ago

FWIW, it is possible to create WebView2 for normal apps without explorer.exe running (like killed), the key should be running in separate desktop.

Yes exactly, we actually do have two kiosk modes in our product (one using the default, the other a custom desktop) and it indeed is the case that only the custom desktop is causing the issues, on the default Windows desktop everything works as expected.

LiangTheDev commented 1 year ago

I have played around with it a bit, and could not repro the issue.

I first verified that #48 is accurate, i.e. WebView sample app works in desktop setup by system internal tools.

I then verified that NewDesktopBug.zip can be used to run WebView sample apps on new desktop, without actually modifying any code in sample app. There is no need to call SetThreadDesktop in the process created on different desktop. According to Thread Connection to a Desktop, if there is an inheritable desktop handle, it will be used for the new process. I also verified that this works after I killed the explorer on default desktop.

When I looked at the https://github.com/SafeExamBrowser/seb-win-refactoring/blob/master/SafeExamBrowser.WindowsApi/DesktopFactory.cs, it seems that the code is creating desktop with null SECURITY_ATTRIBUTES, without the sa.bInheritHandle = 1 code in NewDesktopBug. Is it possible that this is the reason that it doesn't work as expected?

The "Thread Connection to a Desktop" does state that Windows will find the first inheritable desktop handle and use it. So, it means that we could only have 1 opened desktop handle in the process.

LiangTheDev commented 1 year ago

Hm. the NewDesktopBug repro app appears to still work even after I set sa.bInheritHandle = 0. I don't know what's going on.

dbuechel commented 1 year ago

Yes, that is correct, I think the call to SetThreadDesktop is redundant (I just search our actual code base, and it appears we're not using it anymore). That behavior would also coincide with the documentation you've cited:

  1. If the thread did not call SetThreadDesktop and did not inherit a desktop, the system attempts to open for MAXIMUM_ALLOWED access and connect to a desktop as follows:

    If a desktop name was specified in the lpDesktop member of the STARTUPINFO structure that was used when the process was created, the thread connects to the specified desktop.