InternetQueryOption, INTERNET_OPTION_USER_AGENT – Replacement

Posted: August 14, 2010 in general
Tags: , , , ,

This is a clear case of …. uhmm got no clue. All I know is that I’ve tried in quite a few different ways to use the InternetQueryOption API function to retrieve the default User Agent and all have failed. The internet contains posts about other people also not being able to retrieve the User Agent…so I got really frustrated. Finally decided to just retrieve the User Agent the old fashioned way, by directly talking to the registry. So here is a quick function to do that:


/*
 Returns a pointer to the useragent string.
 Return NULL if something goes wrong.
 do NOT forget to free it!
*/
char *getUA(){
 LONG res;
 HKEY regopen;
 char *ua;
 DWORD type;
 DWORD size = 80;

 ua = (char *)malloc(80);
 SecureZeroMemory(ua,80);
 res = RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"),0,KEY_QUERY_VALUE,&regopen);
 if(res == ERROR_SUCCESS){
 res = RegQueryValueEx(regopen,TEXT("User Agent"),0,&type,(BYTE *)ua,&size);
 if(res == ERROR_SUCCESS){
 RegCloseKey(regopen);
 return ua;
 }
 }
 free(ua);
 return NULL;
}

Advertisements
Comments
  1. diablohorn says:

    heheh yeah finding the API sometimes really can be an issue. Oh well as long as search engines lead us to the right answer I’m happy.

  2. Casper says:

    @diablohorn

    Thank you! Wow, easy as that, if you know the library…..
    Thanks, it’s working now. I only concentrated on wininet, while it was in urlmon.

    If MS only documented stuff from the old api´s, we could focus on more important things then looking for a needle in a haystack!
    But that will never ever happen, just like the numerous bugs! Cause, let´s be honest. No one likes to rewrite old code, we all like to code new stuff, so things get deprecated really fast :-)

  3. Casper says:

    Using the registry is very bad… There are several static places where there are keys for useragent, in local_machine as well in current_user. On my laptop, there are 7 entries… The post criteria is found in Wownode… while the default was in a 5.0 subkey and the pre was also in another subkey on another place.

    You could try ‘UrlMkSetSessionOption’ or hooking up on an event like onBeforeNavigate, on an existance instance. You would be suprised this doesn’t work, by a bug acknowledgde by MS. It’s now so many years later and a fix has never donet the job, because they made the workaround DEPRECATED…

    I’ve also tried this method via wininet, without results… It’s possible to sent one in, in creating an instance, but not retrieving it. But it should be there, but no one ever has done this via invoking the wininet lib??

    I wonder how UAPick did it. But maybe one day of searching and testing isn’t enough… I doubt…

  4. s21st says:

    Well, nearly I agreed with you with the link, but the fun is not finish yet. cuz your code picked up HKEY_USERS/.DEFAULT/ thats the new HKCU redirection.

    So if you try to pick up ProxyServer from registry from same location, your code defintely will face the music.

  5. diablohorn says:

    Well in the feeling stupid category…all my bitching was my own fault, someone pointed the following out to me today:

    http://msdn.microsoft.com/en-us/library/ms775114%28VS.85%29.aspx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s