How to obtain the useragent of IE under Delphi

Source: Internet
Author: User

Method One:
Using SHDOCVW, some of the methods provided by the MSHTML unit take advantage of the browser's features.

Uses SHDocVw, MSHtml;

function getuseragent:string;
Var
Doc:ihtmldocument2;
Win:ihtmlwindow2;
Wb:twebbrowser;
Begin
Result: = ';
Try
WB: = Twebbrowser.create (nil);
Try
Wb. Navigate (' About:blank ');
While not WB. ReadyState = Readystate_complete Do
Application.processmessages;
Doc: = IHTMLDocument2 (wb. Document);
If not Assigned (DOC) then Exit;
Win: = Doc.parentwindow;
If Assigned (Win) Then
Result: = win.clientInformation.userAgent;
Finally
Wb. Free;
End
Except
End
End

Note: This method is to get the system default UserAgent method, not necessarily the system default IE browser useragent, if you need to get IE browser useragent, You can change the WebBrowser object to an interface such as IWebBrowser2 or ishellwindows.



Method Two:
It's really simple to read from the registry. However, the method read out is not complete.

Uses Registry;

function getdefaultuseragent:string;
Var
Reg:tregistry;
Begin
Reg: = tregistry.create;
Try
Reg.rootkey: = HKEY_CURRENT_USER;
If Reg.openkey (' \software\microsoft\windows\currentversion\internet settings\ ', FALSE)
Then Result: = Reg.readstring (' User Agent ');
Finally
Reg.closekey;
Reg.free;
End
End

Note: You can actually read the value of the USER Agent from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings, from HKEY_ Local_machine\software\microsoft\windows\currentversion\internet settings\5.0\user Agent\Post Platform read the. NET CLR 2.0.50727 like such an item name, and then stitching together can make up an IE browser's useragent.


Method Three:
Use the method provided in Urlmon to get the user Agent of the browser, but this method sometimes mistakes, such as my browser is IE8.0, and the result is IE7.0.

Uses UrlMon;

function getieuseragent:string;
Var
Ieuseragent:array[0. MAX_PATH] of Char;
Dwsizeout:dword;
Begin
Result: = ';
Try
Dwsizeout: = 0;
ZeroMemory (@IeUserAgent, MAX_PATH * SIZEOF (Char));
Urlmkgetsessionoption (Urlmon_option_useragent, @IeUserAgent, MAX_PATH, dwsizeout, 0);
Result: = Strpas (ieuseragent);
Except
On E:exception do
Begin
ShowMessage (E.message);
End
End
End

Http://www.lsworks.net/article/42.html

How to obtain the useragent of IE under Delphi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.