How to change the user agent and download control flags in a webbrowser control host#
User vnetvvv wants to know how to change the user agent string in a VB6 webbrowser control.
The knowledge base article PRB: WebBrowser Control Clients Share Global Settings provides the necessary code for changing the user agent of the webbrowser control when the webbrowser ask for it. But how to have the webbrowser control ask for the user agent?
Well the webbrowser control is an ActiveX, so it implements IOleControl::OnAmbientPropertyChange. The host can call the method to notify the webbrowser control about the property change after the browser window is created.
The list of ambient properties can be found in the walkall sample, the most useful ones are DISPID_AMBIENT_USERMODE (which VB6 supports), DISPID_AMBIENT_DLCONTROL and DISPID_AMBIENT_USERAGENT. The documentation of other ambient properties can be found in the documentation of CComControlBase’s GetAmbient Property Methods
Note the knowledge base article PRB: WebBrowser Control Clients Share Global Settings also says DISPID_AMBIENT_USERAGENT cannot be used to alter the user agent string in DOM or when the navigation is from code. Changing the user agent string in registry, of course, can change the behavior but will also have global effect.
Another way to change the user agent is to go directly into the networking layer of webbrowser control and change the user agent process-wide. Since most of the time per-page browser setting isn’t necessary this can also be used in custom browsers. The WinInet function UrlMkSetSessionOption has a flag URLMON_OPTION_USERAGENT that can be used to change the user agent, However it only updates DOM/Javascript and ignore the WinInet stack so other WinInet client won’t be affected, until you call UrlMkSetSessionOption with URLMON_OPTION_USERAGENTREFRESH. This is better for VB6 and .Net programmers since it does not involve declaring a lot of OLE interfaces.
You can get the user agent from the DOM (document.parentWindow.navigator.userAgent).