Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
VB6: UserDataFolder and UserAgent are ignored
#1
Hi.

I am testing AntView (32-bit, v1.0.111.0) in Visial Basic 6 (on Windows 10)
and I see that two properties UserDataFolder and UserAgent are not working correct.

I set own values for UserDataFolder and UserAgent
but AntView ignore that. This is my VB6 code

Private Sub Form_Load()
AntView.UserDataFolder = "C:\Users\Tester\AppData\Roaming\Soft22\WebView"
AntView.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0"
End Sub

Private Sub Form_Unload(Cancel As Integer)
Debug.Print AntView.UserDataFolder ‘C:\Users\Tester\AppData\Local\Temp\vb6.exe.WebView2
Debug.Print AntView.UserAgent ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36 Edg/100.0.1185.39
AntView.CloseBrowserProcess
AntView.CloseWebView
End Sub

In other words, the AntView does not take entered values but uses the default

Could tell me how to fix it?
Reply
#2
Hello,



OK, I had a look.




The first issue, setting the UserDataFolder is being ignored, because at least one instance of the WebView2 control is already created by that time. You can only set this one time and it must be before any instance has been created.




This is mostly due to a design decision mistake on my end.

At first when I build the wrapper control, I thought it was a good idea to automatically create the WebView2 control when the control is initialised. It sure seemed like a nice feature.

Boy was I wrong there.. and especially when using VB6.




This has a lot of side effects, like in the VB6 IDE it is already creating the webview2 control when in design mode. That then interferes with properties, the ability to navigate there and a variety of usability things. Even detecting design mode is problematic as it happens after that -out of process- webview2 control is created.

I did have the foresight to have a property "CreateWebViewOnCreate" that one can set to false in the OnCreate event. Sadly .. that particular event does not work in VB6 or VBA whereis it works in a variety of other programming environments.

So far I have not been able to figure out why it doesn't trigger.

This will be fixed, but at least there is a workaround now, see below.



So ... the above is a "catch 22" as the Americans say. Yes there are workarounds like always creating the AntView control dynamically, but that is ... cumbersome.




Starting with version 1.1 of the AntView control the property CreateWebViewOnCreate defaults to false.

Existing users of my control can fix that either by:

- adding CreateWebView to their logic



or



- create a new global object based on AntViewGlobal and set CreateWebViewOnCreate to True in that object. All the AntView control instances will look there when they are created.

I expect AntView 1.1 to go in public beta soon.






In recap:


Setting the UserDataFolder before creating the control with CreateWebView like below will work in the new build I will email you in a few minutes.



The code with the new build then looks like:

Code:
Private Sub Form_Load()
  EdgeWebBrowser.UserDataFolder = "C:\Users\Wil\AppData\Roaming\Soft22\WebView"
  EdgeWebBrowser.CreateWebView
End Sub

Private Sub Form_Unload(Cancel As Integer)
  Debug.Print EdgeWebBrowser.UserDataFolder
  Debug.Print EdgeWebBrowser.UserAgent
  EdgeWebBrowser.CloseBrowserProcess
  EdgeWebBrowser.CloseWebView
End Sub





The second issue.

The UserAgent setting being ignored.

In the latest public version (1.0.111-2) you can set most properties now before the control is created. This wasn't the case in the past as for Microsoft you can't set properties for WebView2 before the out of process control has been created.

This is now handled for most properties via caching the setting in the wrapper as it otherwise makes it all a bit finicky as to determine when you can/cannot use a property.

By caching those properties for AntView you can work with those properties like normal properties... except for the UserAgent.



The UserAgent property presented a problem for me. I don't know the default/current value before the control exists.

So while I can -at least in theory- catch your setting, querying that setting before it exists would give another result as after it exists. Which is why I haven't added the caching feature for the UserAgent property.



The workaround? Set it once the control exists.

Eg. like so:

Code:
Private Sub EdgeWebBrowser_OnCreateWebviewCompleted(ByVal HResult As Long)
  EdgeWebBrowser.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0"
End Sub


Then it works.



--

Wil
Reply
#3
Dear Wil.

Thank you for your detailed explanation.
I followed your comments and it seems that now (private build: 1.1.150.0) "AntView.UserDataFolder" works as expected.

Back to the second case (UserAgent) I made a minor correction to your code.

This example works correctly

Code:
Private Sub EdgeWebBrowser_OnCreateWebviewCompleted(ByVal HResult As Long)
EdgeWebBrowser.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0"
End Sub

When using this code, the WebBrowser still takes the default value from user system/Edge browser

Code:
Private Sub EdgeWebBrowser_OnCreate()
EdgeWebBrowser.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/95.0"
End Sub

In summary, you should set the UserAgent only when "OnCreateWebviewCompleted" event fired
Reply
#4
(2022-04-19, 08:18:07)MrPrzemek Wrote: Dear Wil.

Thank you for your detailed explanation.
I followed your comments and it seems that now (private build: 1.1.150.0) "AntView.UserDataFolder" works as expected.

Back to the second case (UserAgent) I made a minor correction to your code.

This example works correctly

Code:
Private Sub EdgeWebBrowser_OnCreateWebviewCompleted(ByVal HResult As Long)
EdgeWebBrowser.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0"
End Sub

When using this code, the WebBrowser still takes the default value from user system/Edge browser

Code:
Private Sub EdgeWebBrowser_OnCreate()
EdgeWebBrowser.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/95.0"
End Sub

In summary, you should set the UserAgent only when "OnCreateWebviewCompleted" event fired

Hi Przemek,

In other environments than VB6, the alternative using OnCreate works.
Sadly, as I mentioned above, at the moment, the OnCreate event never fires on VB6 and VBA.
This is a known issue that I haven't been able to track down so far.

FWIW, I'm considering to also cache the UserAgent property and just document the "it doesn't return the default useragent until invoked" part. That wouldn't make things worse and a bit easier for scenarios like yours.
--
Wil
Reply
#5
FYI, in the next public update, the UserAgent property is now also cached as well, so no longer need to worry if the WebView2 control has been created already.

IOW, this is in the private update I just send you.
--
Wil
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)