Search This Blog

Thursday, April 16, 2009

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;
}

Thursday, March 26, 2009

CIPAddressCtr converting CString IP address to DWORD

CIPAddressCtr converting CString IP address to DWORD



DWORD GetDWORDIPAddress(CString strIPAddress)
{
     strIPAddress.MakeReverse();// Start from behind

     char DOT = '.';
     DWORD dwReturnValue = 0;

     double dPower = 0.0;

     int length = strIPAddress.GetLength();

     DWORD dwByteTemp = 0;
     int nWhichByte = 0;
     int i = 0;
     for(i; i<length ; i++ ){
          volatile int nTemp = 0;
          char aChar = strIPAddress.GetAt(i);

          if(aChar != DOT){
               int nChar = 0;
               switch(aChar){
                    case '1': nChar = 1; break;
                    case '2': nChar = 2; break;
                    case '3': nChar = 3; break;
                    case '4': nChar = 4; break;
                    case '5': nChar = 5; break;
                    case '6': nChar = 6; break;
                    case '7': nChar = 7; break;
                    case '8': nChar = 8; break;
                    case '9': nChar = 9; break;
                    case '0': nChar = 0; break;
                    default: break;
               }
               nTemp = nChar * (int)pow(10.0 ,dPower);
               dwByteTemp += nTemp;
               dPower++;

               if(i == length-1/*Last Byte*/){
                    dwByteTemp <<= (nWhichByte * 8);//8 Bits = Byte Length
                    dwReturnValue = dwReturnValue | dwByteTemp;
                    dPower = 0;
                    dwByteTemp = 0;
                    nWhichByte++;
               }
          } else {
               dwByteTemp <<= (nWhichByte * 8);// 8 Bits = Byte Length
               dwReturnValue = dwReturnValue | dwByteTemp;
               dPower = 0;
               dwByteTemp = 0;
               nWhichByte++;
          }
     }
     strIPAddress.MakeReverse();//Undo
     return dwReturnValue;
}

Example:

CIPAddressCtrl obj;
DWORD dwIP,dwSubnet,dwGateway,dwProxy;
dwIP=GetDWORDIPAddress(csIP);
dwSubnet=GetDWORDIPAddress(csSubnet);
dwGateway=GetDWORDIPAddress(csGateway);
dwProxy=GetDWORDIPAddress(csProxyIP);

Friday, March 13, 2009

Debugging Tips

While debugging some times we use message box or write in a file.
But we may forget to remove that. To avoid such thing we can use _DEBUG macro to make statements execute only in debug mode not in release mode.

Example:
# ifdef _DEBUG
CStdioFile csTempFile;
if( csTempFile.Open("d:\\temp.txt",CFile::modeCreate|CFile::modeWrite) )
csTempFile.Write(csContents.GetBuffer(csContents.GetLength()+1 ),csContents.GetLength());
csTempFile.Close();
#endif


# ifdef _DEBUG
AfxMessageBox(csContent);
#endif

Saturday, February 28, 2009

How to convert CString into LPTSTR ?

CString csStr = "Hello";
LPTSTR lpStr = csStr.GetBuffer( 0 );
csStr.ReleaseBuffer();