Jump to content

Recommended Posts


Hi Guys,

I'm having a nightmare trying to set the User Agent in an embedded browser.

I have seen examples is several posts that include:

  1. a SetUserAgent fuction
  2. UDF NetSession-0.9d.au3
  3. Editing a REG KEY

I have included my code.  I tried using the _SetUserAgent function in UDF Net Session first.  No joy.  So I commented this out and tried the function (which I renamed SetUserAgent2.

I have got urlmon.dll, but it was read,or execute, so I changed this to write, modify, read, execute for system, admin & user.  Still no joy.

Finally, using the code that tries to write to the REG KEY, just returns "Something went wrong while trying to set the useragent."

All suggestions would be grateful received.  

Many thanks,




#include <Constants.au3>
#include <Inet.au3>
#include <GuiButton.au3>
#include <GuiToolBar.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
#include <NetSession-0.9d.au3>


;~ Local $URL = "http://google.com"
;~ Local $URL2 = "http://youtube.com"
;~ Local $sUserAgent = "Agent User 1.0"
;~ Local $skey = "HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionInternet Settings"
;~ If @OSArch <> "X86" And @AutoItX64 Then $skey = "HKEY_CURRENT_USER64SoftwareMicrosoftWindowsCurrentVersionInternet Settings"
;~ Local $Write = RegWrite($skey, "User Agent", "REG_SZ", $sUserAgent)
;~ If Not $Write Then Exit MsgBox(16, "Error!", "Something went wrong while trying to set the useragent.")
;~ DllCall('WININET.DLL', 'long', 'InternetSetOption', 'int', 0, 'long', 39, 'str', 0, 'long', 0)
;~ $OIE = _IECREATE("http://whatsmyuseragent.com")
;~ Sleep (500000)

; sleep(100000)


GUICreate("Test User-Agent String", 700, 500)
$oIE = _IECreateEmbedded()
GUICtrlCreateObj($oIE, 10, 10, 680, 480)
_SetUserAgent("User-Agent: My super special user agent string.")

While GUIGetMsg() <> -3

Func _SetUserAgent2($agent)
    $agentLen = StringLen($agent)
    Dim $tBuff = DllStructCreate("char["&$agentLen&"]")
    DllStructSetData($tBuff, 1, $agent)
    $chk_UrlMkSetSessionOption = DllCall("urlmon.dll", "long", "UrlMkSetSessionOption", "dword", 0x10000001, "ptr", DllStructGetPtr($tBuff), "dword", $agentLen, "dword", 0)


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.