Search This Blog

Thursday, April 16, 2009

Read and write .ini files



void ReadwriteINI()
{
     CString csIniPath,csKey;
     int nPos;

     char lpszKeyNames[4096];
     DWORD dSize;
     dSize = sizeof(lpszKeyNames);
     csIniPath="settings.ini";
     GetPrivateProfileSection( "IPADDRESS" , lpszKeyNames , dSize , csIniPath );
     char *pKeyName = lpszKeyNames;    
     CString csIP(""), csMyKey(""),csSubnet,csProxyIP,csGateway ,csPort;
    
     while (*pKeyName != 0x00)
     {
          nPos = -1;
          if(*pKeyName != 0x00)
          {
               csKey = pKeyName;
               if((nPos = csKey.Find("=")) > -1)
               {    
                    csMyKey = csKey.Left(nPos);
                    if( csMyKey.Find("IPADDR") > - 1 )
                    {
                         csIP= csKey.Mid(nPos+1);
                    }
                    if( csMyKey.Find("SUBNET") > - 1 )
                    {
                         csSubnet= csKey.Mid(nPos+1);
                    }
                    if( csMyKey.Find("GATEWAY") > - 1 )
                    {
                         csGateway= csKey.Mid(nPos+1);
                    }
                    if( csMyKey.Find("PROXYIP") > - 1 )
                    {
                         csProxyIP= csKey.Mid(nPos+1);
                    }
                    if( csMyKey.Find("PORT") > - 1 )
                    {
                         csPort= csKey.Mid(nPos+1);
                    }
               }
          }
          pKeyName = pKeyName + strlen(pKeyName) + 1;
     }


     if(csIP.IsEmpty())
     {
          csIP="192.168.0.2;
          WritePrivateProfileString("IPADDRESS","IPADDR",csIP,csIniPath);
     }
     if(csSubnet.IsEmpty())
     {
          csSubnet="255.255.255.0";
          WritePrivateProfileString("IPADDRESS","SUBNET ",csSubnet,csIniPath);
     }
     if(csGateway.IsEmpty())
     {
          csGateway="192.168.0.3";
          WritePrivateProfileString("IPADDRESS","GATEWAY",csGateway,csIniPath);
     }
     if(csProxyIP.IsEmpty())
     {
          csProxyIP="198.168.0.1";
          WritePrivateProfileString("IPADDRESS","PROXYIP",csProxyIP,csIniPath);
     }
     if(csPort.IsEmpty())
     {
          csPort="3128";
          WritePrivateProfileString("IPADDRESS","PORT",csPort,csIniPath);
     }

    

}

INI file content:
[IPADDRESS] IPADDR= SUBNET= GATEWAY= PROXYIP= PORT= 


Registry Write example by enable Proxy settings

void EnableProxy()
{
     HKEY hkResult;
     LPCTSTR lpValueName = "VALUE_NAME";
     LPCTSTR lpszKeyName = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
     LPCTSTR lpszValueName = "ProxyEnable";
     // 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";

     // Write value in the registry
     DWORD dwData;
     dwData=1;
     Res=RegSetValueEx(hkResult, TEXT(lpszValueName),0, REG_DWORD,(const BYTE*) &dwData, sizeof(DWORD));
     if (Res != ERROR_SUCCESS)
          throw "Unable to set value";


     Res = RegCloseKey(hkResult);    

}

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;


}

CFileDialog -File open dialog example

CFileDialog fileDlg(TRUE,NULL,NULL,OFN_OVERWRITEPROMPT,"Text & CSV Files (*.txt,*.csv)|*.txt;*.csv|All Files(*.*)|*.*||");
int iRet = fileDlg.DoModal();
CString csFileName;
csFileName = fileDlg.GetPathName();

if(iRet == IDOK)
{
    AfxMessageBox(csFileName);
}
else
{
     AfxMessageBox("No File Selected!");
}

Download Webpage to content to CString

BOOL DownloadURLContent(const char *url, CString &csContent, CString &errorMessage)
{
     const int FILEBUFLEN = 1024;
     char *httpBuff = new char[FILEBUFLEN+1];
     memset( httpBuff ,0 , FILEBUFLEN+1 );
     TCHAR szErr[255];
     errorMessage = "";
     CString csTemp;
    
     TRY {
          CInternetSession session;
          session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000);
          session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 3);
          CFile *remoteFile = session.OpenURL(url, 1 ,INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);
         
          int numBytes;
          while (numBytes = remoteFile->Read(httpBuff, FILEBUFLEN))
          {
               //localFile.Write(httpBuff, numBytes);

               csTemp=httpBuff;
               memset( httpBuff ,0 , FILEBUFLEN+1 );

               csContent=csContent+csTemp;
              
          }
     }
     CATCH_ALL(error) {
          error->GetErrorMessage(szErr,254,NULL);
          errorMessage.Format("%s",szErr);
          return FALSE;
     }
     END_CATCH_ALL;
    
     return TRUE;
}