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