_SetUserAgent() Change IE COM Session User Agent

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

_SetUserAgent() Change IE COM Session User Agent

25 Oct 2013, 17:37

_SetUserAgent() sets a customized User Agent that identifies the IE COM object.

Tested and working on IE 7, 8.
To change the User Agent in later versions of IE, see the posts below


Typical default User Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0

Call the function after creating the COM Object and before the Navigate() or Navigate2() methods.
Or you can call it from a BeforeNavigate2() Event or Class Event.

Examples:

Code: Select all

; Switch from IE to Safari User Agent
_SetUserAgent("Windows; U; Windows NT 6.1; tr-TR) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27")

Code: Select all

; Switch from IE to Opera User Agent
_SetUserAgent("Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14")

Code: Select all

; Fun nonsense 
_SetUserAgent("kittens like this User Agent")
Function:

Code: Select all

_SetUserAgent(dwOpt) 
{
    ; Discovered by Inthemist
    ; AutoIt >> AutoHokey port by TLM
    ; ANSI fix by GeekDude

    DllCall("urlmon.dll\UrlMkSetSessionOption"
    	   , uint, 0x10000001
    	   , aStr, dwOpt
   	       , uint, StrLen(dwOpt)
    	   , uint, 0)
}
This basically changes the User Agent for an IE object session
as Navigate/2() headers only work once.

You can verify the User Agent by navigating to: http://whatsmyuseragent.com
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: _SetUserAgent() Change IE COM Session User Agent

22 Jan 2014, 12:01

For IE 10 ( and quite possibly 9 )
stemsmit from http://ahk4.me/KG5Ovk wrote: After investigating further I was able to discover this which works for IE 10:

Code: Select all

wb := ComObjCreate("InternetExplorer.Application")
wb.Visible := True
wb.Navigate("http://whatsmyuseragent.com",0,0,0,"User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25")
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: _SetUserAgent() Change IE COM Session User Agent

22 Jan 2014, 12:09

TLM wrote:For IE 10 ( and quite possibly 9 )
stemsmit from http://ahk4.me/KG5Ovk wrote: After investigating further I was able to discover this which works for IE 10:

Code: Select all

wb := ComObjCreate("InternetExplorer.Application")
wb.Visible := True
wb.Navigate("http://whatsmyuseragent.com",0,0,0,"User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25")
Useful! Thanks for posting it! :D
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: _SetUserAgent() Change IE COM Session User Agent

03 Feb 2023, 11:38

There is a bug in function (tested on win11) - We need to load dll before use it:, like this:

Code: Select all

_SetUserAgent(dwOpt) 
{
    ; Discovered by Inthemist
    ; AutoIt >> AutoHokey port by TLM
    ; ANSI fix by GeekDude
    DllCall("LoadLibrary", "str", "urlmon.dll")
    DllCall("urlmon.dll\UrlMkSetSessionOption"
    	   , uint, 0x10000001
    	   , aStr, dwOpt
   	       , uint, StrLen(dwOpt)
    	   , uint, 0)
}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: ConnorMcLoud, makdc96 and 150 guests