Forums: How To Make This Program Not Display Error When No Internet Connection? - Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Make This Program Not Display Error When No Internet Connection?

#1 User is offline   elistian 

  • Private First Class
  • Icon
  • Group: Members
  • Posts: 34
  • Joined: 24-October 05

Posted 31 October 2009 - 04:24 AM

As the title says, I have a program that someone from this forum helped me with in the past and I need some help again by the looks of it. I dont know much about programming so bare with me. This app was written in the past to work along with a php script I had running on my web server. The purpose was to notify my webserver whenever this program was run and the php script would log the time & date along with the IP and computer name that the notification came from.

So this app when run would simply look up the pc's 'computer name', then goto a certain url where the php script was located and append the computers name to the url. For example the program would open this url "http://www.YOUR-WEBSITE.com/c.php?pc=" and append the computer name it looked up to the end of it.

Now my problem is this program is suppose to run silently in the background, which it does most of the time. The problem is if the program is run when the internet connection is down or unavailable, the program will popup and display a error message. I need help making it so no matter what, this program doesnt display any messages of any kind, error or otherwise, even if no internet connection is available I want it to just silently close.

Does anyone know how I would go about modifying the below source code todo this?

#include <stdio.h>
#include <windows.h>
#include <wininet.h>

#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
#pragma comment (lib, "Wininet.lib")

int main(int argc, char* argv[])
{
	HINTERNET Initialize, Connection;
	char cBuffer[MAX_COMPUTERNAME_LENGTH+1] = {'\0'};
	DWORD dwBytes = MAX_COMPUTERNAME_LENGTH+1;
	char logUrl[100] = {'\0'}; //Make It Smaller (url length + MAX_COMPUTERNAME_LENGTH + 1)

	strcpy(logUrl, "http://www.YOUR-WEBSITE.com/c.php?pc=");

	//BOOL GetComputerName(LPTSTR lpBuffer, LPDWORD lpnSize);
	if(! GetComputerName((LPTSTR)&cBuffer, &dwBytes))
	{
		printf("~~GetComputerName() Failed!Last Error:%s\n",GetLastError());
		return 1;
	}
	//Append Computer Name
	strcat((char*)&logUrl, (const char*)&cBuffer);

	Initialize = InternetOpen( "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)", 
				INTERNET_OPEN_TYPE_PRECONFIG , NULL, NULL, 0);

	// Make The Connection To The URL
	// This Is All That Is Needed To Get Your PHP Script To Log The IP & Computer Name
	Connection = InternetOpenUrl(Initialize, logUrl, NULL, 0,
		INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD, 0);
	if(! Connection)
	{	
		printf("~~InternetOpenUrl() Failed! LastError:%s", GetLastError());
		return 1;
	}

	//Close Handles
	InternetCloseHandle(Connection);
	InternetCloseHandle(Initialize);

	return 0;
}


Thanks in advance for anyone that can help!

-elistian
0

#2 User is offline   BobS0327 

  • Private
  • Icon
  • Group: Members
  • Posts: 19
  • Joined: 14-October 07

Posted 18 November 2009 - 05:51 PM

View Postelistian, on 31 October 2009 - 12:24 PM, said:

As the title says, I have a program that someone from this forum helped me with in the past and I need some help again by the looks of it. I dont know much about programming so bare with me. This app was written in the past to work along with a php script I had running on my web server. The purpose was to notify my webserver whenever this program was run and the php script would log the time & date along with the IP and computer name that the notification came from.

So this app when run would simply look up the pc's 'computer name', then goto a certain url where the php script was located and append the computers name to the url. For example the program would open this url "http://www.YOUR-WEBSITE.com/c.php?pc=" and append the computer name it looked up to the end of it.

Now my problem is this program is suppose to run silently in the background, which it does most of the time. The problem is if the program is run when the internet connection is down or unavailable, the program will popup and display a error message. I need help making it so no matter what, this program doesnt display any messages of any kind, error or otherwise, even if no internet connection is available I want it to just silently close.

Does anyone know how I would go about modifying the below source code todo this?

#include <stdio.h>
#include <windows.h>
#include <wininet.h>

#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
#pragma comment (lib, "Wininet.lib")

int main(int argc, char* argv[])
{
	HINTERNET Initialize, Connection;
	char cBuffer[MAX_COMPUTERNAME_LENGTH+1] = {'\0'};
	DWORD dwBytes = MAX_COMPUTERNAME_LENGTH+1;
	char logUrl[100] = {'\0'}; //Make It Smaller (url length + MAX_COMPUTERNAME_LENGTH + 1)

	strcpy(logUrl, "http://www.YOUR-WEBSITE.com/c.php?pc=");

	//BOOL GetComputerName(LPTSTR lpBuffer, LPDWORD lpnSize);
	if(! GetComputerName((LPTSTR)&cBuffer, &dwBytes))
	{
		printf("~~GetComputerName() Failed!Last Error:%s\n",GetLastError());
		return 1;
	}
	//Append Computer Name
	strcat((char*)&logUrl, (const char*)&cBuffer);

	Initialize = InternetOpen( "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)", 
				INTERNET_OPEN_TYPE_PRECONFIG , NULL, NULL, 0);

	// Make The Connection To The URL
	// This Is All That Is Needed To Get Your PHP Script To Log The IP & Computer Name
	Connection = InternetOpenUrl(Initialize, logUrl, NULL, 0,
		INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD, 0);
	if(! Connection)
	{	
		printf("~~InternetOpenUrl() Failed! LastError:%s", GetLastError());
		return 1;
	}

	//Close Handles
	InternetCloseHandle(Connection);
	InternetCloseHandle(Initialize);

	return 0;
}


Thanks in advance for anyone that can help!

-elistian



Change this..

if(! Connection) 
    	{    	
            	printf("~~InternetOpenUrl() Failed! LastError:%s", GetLastError()); 
            	return 1; 
    	} 


to this..


if(! Connection) 
    	{    	
            	printf("~~InternetOpenUrl() Failed! LastError:%d", GetLastError()); 
            	return 1; 
    	} 


Note that I used %d in the printf statement instead of %s because GetLastError returns a numeric not a string.
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users

  • Share



Our Sponsors:


SwiftLayer Affiliate Web Hosting