Internet Tips - by example.
To check if the user is connected to the internet :-
You can use the InternetGetConnectedState function
BOOL bResult = InternetGetConnectedState(&dwFlags,0);
// If bResult is TRUE, then the user is connected.
// If its FALSE, then the connection has not been made.
To download a file from the internet :-
HINTERNET hInternet = ::InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,0,0,0);
if(hInternet != NULL)
HINTERNET hopenfile = ::InternetOpenUrl(hInternet,"http://www.yahoo.com/main.html",
"Accept: binary/*\r\nUser-Agent: gethttpfile\r\n",-1L,PRE_CONFIG_INTERNET_ACCESS, 0);
if(hopenfile != NULL)
int BuffSize = 1024;
char *pBuff = new char[BuffSize];
while(::InternetReadFile(hopenfile, pBuff, BuffSize, &byteRead) && byteRead>0)
// pBuff contains part (BuffSize bytes max) of desired HTML file
TRACE("%d bytes read\n", byteRead);
1. Add #include
2. Do not forget to link your project with "wininet.lib"
To launch the Web Browser from your application :-
ShellExecute(NULL, "open", "http://www.foo.com", NULL, NULL, SW_SHOWNORMAL);
To open a URL from your application :-
CString sUrl = "http://codeguru.com";
pFile = Session.OpenURL(sUrl);
To send an email from your application :-
ShellExecute( NULL, NULL,"mailto:firstname.lastname@example.org",NULL, NULL, SW_SHOW );
This will open the default email program ( Outlook express..) and add the specified address
to the Recipient's column of the email program.
To use FTP to upload and download files from the net :-
1) Add the following statement in your header file.
2) In your header file, add the following Member Variables
3) In your application's initialization ( OnInitDialog or InitInstance functions), add
the following lines.
m_pFtpConnection = NULL;
// Here usr is the username, pwd is the password and ftpsite.com is the name
// of the ftp site which you want to connect to.
m_pFtpConnection = m_Session.GetFtpConnection("ftpSite.com","usr","pwd",
m_pFtpConnection = NULL;
4) To upload a file, add the following lines of code :-
// Here c:\\Myfile.bmp is the name of the file that you want to upload
// It neednt necessarily be a bitmap file. You can upload any file that you want to.
// The CString strFileName is used so that the same name is uploaded to the ftp server.
// After uploading, the file in the ftp server will have the same name as your local file.
// You can also rename it to anything
strFileName = Finder.GetFileName();
BOOL bUploaded = m_pFtpConnection->PutFile("C:\\Myfile.bmp",strFileName,
5) To download a file from a ftp site, you can use the following code. Here the first
parameter is the file in the ftp server. The 2nd parameter is the location where you
want to store it in your hard disk.
6) To close the connection
Thats it. Its so simple to use FTP. Isnt it ?
To get the name of the local computer and its IP address :-
struct sockaddr_in sock;
HOSTENT * lpHost = gethostbyname(hostname);
memcpy(&(sock.sin_addr), lpHost->h_addr_list, lpHost->h_length);
printf("Host Name: %s IP address : %s",hostname, inet_ntoa(sock.sin_addr));