Search This Blog

Thursday, April 16, 2009

Registry Read example to get Proxy IP and Port settings

void GetProxyIPPortRegistry()
{
     HKEY hkResult;
     LPCTSTR lpValueName = "VALUE_NAME";
     LPCTSTR lpszKeyName = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
     LPCTSTR lpszValueName = "ProxyServer";
     int iValue;

     // Opens a key in the registry and keeps an handle - hkResult - for future use.
     LONG Res = RegCreateKeyEx (HKEY_CURRENT_USER, lpszKeyName,
          0,NULL,
          REG_OPTION_NON_VOLATILE ,KEY_ALL_ACCESS,NULL, &hkResult,
          NULL);
     if (Res != ERROR_SUCCESS)
          throw "Unable to open the key";

    
     CString csIP,csIPAddress,csPort;
     // Reads a value from the registry.
     unsigned long lSize=1;
     DWORD dwType;
     char *lpValue;
     if ((Res = RegQueryValueEx(hkResult,lpszValueName,NULL,NULL,
          NULL,&lSize)) == ERROR_SUCCESS)
     {
          lpValue = new char [lSize + 1];
          Res =
               RegQueryValueEx(hkResult, (LPTSTR)lpszValueName,NULL, &dwType,
               (LPBYTE)lpValue, &lSize);

          if (Res != ERROR_SUCCESS)
               throw "Unable to query value";
          iValue = ::atoi(lpValue);
          csIP=lpValue;
     }
     Res = RegCloseKey(hkResult);

     int nPos=-1;
     if( (nPos=csIP.Find(":"))>-1)
     {
          csIPAddress=csIP.Mid(nPos+1);
          csPort=csIP.Left(nPos);

     }

     delete lpValue;


}

No comments: