Search This Blog

Friday, December 19, 2008

MFC code to Download a webPage

#include "afxinet.h" //add this header file

BOOL DownloadURL(const char *url, const char *filename, CString &errorMessage)
{
const int FILEBUFLEN = 1024;
char httpBuff[FILEBUFLEN];
TCHAR szErr[255];
errorMessage = "";

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);
CFile localFile(filename, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
int numBytes;
while (numBytes = remoteFile->Read(httpBuff, FILEBUFLEN))
{
localFile.Write(httpBuff, numBytes);
}
}
CATCH_ALL(error) {
error->GetErrorMessage(szErr,254,NULL);
errorMessage.Format("%s",szErr);
return FALSE;
}
END_CATCH_ALL;
return TRUE;
}

void CDownloadWebPageDlg::OnOK() //Copy the following Lines where you need to download.
{
CString csErr;
if(!DownloadURL("http://www.google.com","c:\\google.htm",csErr))
{
AfxMessageBox(csErr);
}
}

Hide the Taskbar application button

Open the .rc file in notepad
search for the line

"EXSTYLE WS_EX_APPWINDOW "

and replace "WS_EX_APPWINDOW" with "WS_EX_TOOLWINDOW".

Save and Close

MFC Code to avoid a Application running Many times

Paste this following code in BOOL CYourProjectNameApp::InitInstance() function of you Aplliction.


CreateMutex(NULL,TRUE,"Sample");
// mutex will be automatically deleted when process ends.
BOOL bAlreadyRunning = (GetLastError() == ERROR_ALREADY_EXISTS);
if(bAlreadyRunning)
{
::MessageBox(NULL, "This Appliction is Already Running ", "Information",
MB_ICONINFORMATION|MB_OK|MB_DEFBUTTON1);
return 0;
}