Thread: Changing User Agent for webbrowser control

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    1

    Changing User Agent for webbrowser control

    How do I change the user agent for the embedded web browser's process in C++?
    Looking for a way to do it so the user agent is set for the entire time (multiple hyperlink clicks).

    I know how to do it in VB.NET but starting out with C++ and it's an essential part of my project. Tried looking into UrlMkSetSessionOption but didn't understand it.

    My C++ code so far

    Code:
        private: System::Void webBrowser1_DocumentCompleted(System::Object^  sender, System::Windows::Forms::WebBrowserDocumentCompletedEventArgs^  e) {
                     
                     this->webBrowser1->ScriptErrorsSuppressed = true;
    
                 }
    
        private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                     this->webBrowser1->Navigate("http://whatsmyuseragent.com");
                 }
        };
    }
    How I do it in VB.NET
    Code:
    Private Property uag As String <DllImport("urlmon.dll", CharSet:=CharSet.Ansi)> _ Private Shared Function UrlMkSetSessionOption(ByVal dwOption As Integer, ByVal pBuffer As String, ByVal dwBufferLength As Integer, ByVal dwReserved As Integer) As Integer End Function Const URLMON_OPTION_USERAGENT As Integer = &H10000001 Public Function ChangeUserAgent(ByVal Agent As String) UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, Agent, Agent.Length, 0) #End Function
    
    Then
    
            uag = ("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.2a1pre) Gecko/20110323 Firefox/4.2a1pre")
            ChangeUserAgent(uag
    I'd really appreciate some help, thanks in advance.

  2. #2
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    The User-Agent is specified in the header of the HTTP request isn't it? So... you don't... change it, you just specify what you want it to be when you're making a request.

    Also... what... is that? That's not C++. That's... I don't know. o3o
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    the first part is C++/CLI - the .Net variant of C++, and the second is Visual Basic. neither language is really supported on this forum. you might try the tech board.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Moved to windows board
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Quote Originally Posted by Elkvis View Post
    the first part is C++/CLI - the .Net variant of C++
    Is that what Managed C++ is? Hm.

    EDIT:: Read a little. Managed C++ is the deprecated older version of C++/CLI.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open exrternal file in a WebBrowser control
    By rebeccar in forum C# Programming
    Replies: 3
    Last Post: 05-26-2010, 09:46 AM
  2. WebBrowser control
    By Overlord in forum Windows Programming
    Replies: 3
    Last Post: 12-21-2008, 06:06 AM
  3. erase cookies cookie from webbrowser control
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 07-23-2007, 01:28 PM
  4. hide scipt errors/warnings in webbrowser control
    By hanhao in forum Windows Programming
    Replies: 1
    Last Post: 06-28-2007, 10:44 AM
  5. Massive - Agent based crowd control AI
    By Mario F. in forum General AI Programming
    Replies: 4
    Last Post: 08-20-2006, 03:41 PM