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

About Sheng Jiang 蒋晟

Microsoft MVP in Visual C , 2004- Forum moderator of the Visual C and .Net forums on CSDN Forum moderator of Chinese forums on Microsoft's MSDN forums
This entry was posted in Active Template Library, enmsdn, Microsoft, Visual C++, Visual Studio and tagged , , . Bookmark the permalink.

One Response to How to change the user agent and download control flags in a webbrowser control host

  1. Pingback: C#怎么修改通过注册表修改ie的user-agent - c# - 开发者问答

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s