///////////////////////////////////////////////////////////////////////////////////////////
//
// AutoUp.c
// (c) copyright 2000-2003
// written by Amichai Rothman
// Version 1.1 - Feb 8, 2000
// Version 1.2 - Jun 5, 2003
//
// This command-line utility allows setting/querying of several Windows startup options.
// It can be used to apply settings manually or to startup services from a batch file.
//

#include <windows.h>
#include <stdio.h>
#include <Wininet.h>

#define SERVICE_NOT_FOUND -2
#define OPEN_SERVICES_FAILED -1
#define SERVICE_NOT_STARTED 0
#define SERVICE_STARTED 1

#define STR_SERVICE_NOT_FOUND "service not found"
#define STR_OPEN_SERVICES_FAILED "open services failed"
#define STR_SERVICE_NOT_STARTED "service not started"
#define STR_SERVICE_STARTED "service started"

#define LOGON_COMMAND_OFF 1
#define LOGON_COMMAND_ON 2
#define LOGON_COMMAND_SHOW 3

#define SHUTDOWN_COMMAND_LOGOFF 1
#define SHUTDOWN_COMMAND_POWEROFF 2
#define SHUTDOWN_COMMAND_REBOOT 3
#define SHUTDOWN_COMMAND_SHUTDOWN 4

#define AUTORUN_COMMAND_ADD 1
#define AUTORUN_COMMAND_REMOVE 2

#define INTERNET_CONNECTION_COMMAND_CONNECT 1
#define INTERNET_CONNECTION_COMMAND_DISCONNECT 2

// gets an enumeration of system services. returns number of services
// that were placed in array, negative value for error
int enumerateServices(ENUM_SERVICE_STATUS servstatus[], int len) {
	DWORD numServices = 0;
	DWORD resumeHandle = 0;
	DWORD bytes = 0;
	SC_HANDLE hSC;

	if (servstatus==NULL)
		return SERVICE_NOT_FOUND;

	if ((hSC = OpenSCManager(NULL, NULL, GENERIC_READ)) == NULL)
		return OPEN_SERVICES_FAILED;

    if (!(EnumServicesStatus(hSC, SERVICE_WIN32 | SERVICE_DRIVER, SERVICE_STATE_ALL, servstatus, len, &bytes, &numServices, &resumeHandle)))
		return OPEN_SERVICES_FAILED;

	CloseServiceHandle(hSC);

	return numServices;


}


// parses a parameter string from command line
// converts to lowercase, removes leading '-' or '/'
// returns true if a good parameter is left, false otherwise
// parsed parameter is returned in targetBuff
BOOLEAN parseParameter(char *param, char *targetBuff, int targetBuffLen) {

	if ( (param==NULL) || (*param=='\0') || (targetBuff==NULL) || (targetBuffLen<2) )
		return FALSE;

	strncpy(targetBuff,param,targetBuffLen-1);
		_strlwr(targetBuff);//convert to lowercase
		if ( (targetBuff[0]=='-') || (targetBuff[0]=='/') ) //remove leading / or - from option
			strncpy(targetBuff,&targetBuff[1],sizeof(targetBuff));
	if (strlen(targetBuff)>0)
		return TRUE;
	else
		return FALSE;
}



int main(int argc, char** argv) {
	int iStatus=SERVICE_NOT_FOUND;
	int i=0;
	int j=0;
	int iTimeout=0;
	int iPause=0;
	int numServices=0;
	int logonCommand=0;
	int shutdownCommand=0;
	int autoRunCommand=0;
	int internetConnectionCommand=0;
	char requestedService[101];
	char logonSetting[5];
	char logonDomain[101];
	char logonUser[101];
	char logonPassword[101];
	char logonPassword2[101];
	char autoRunCommandLine[_MAX_PATH];
	char autoRunCommandName[101];
	HKEY hkey;
	ENUM_SERVICE_STATUS servstatus[1000];
	char arg[101];
	BOOLEAN res=FALSE;
	BOOLEAN bBadParameter=FALSE;
	BOOLEAN bList=FALSE;
	BOOLEAN bStatus=FALSE;
	BOOLEAN bTimeout=FALSE;
	BOOLEAN bIndefinateTimeout=FALSE;
	BOOLEAN bPause=FALSE;
	BOOLEAN bAutoLogon=FALSE;
	BOOLEAN bShutdown=FALSE;
	BOOLEAN bAutoRun=FALSE;
	BOOLEAN bAutoRunMachine=FALSE;
	BOOLEAN bInternetConnection=FALSE;

	printf("\nAutoUp 1.21 (c)copyright 2000-2003 by Amichai Rothman www.freeutils.net\n");

	if ((argc<2) || (argv[1]==NULL))
		bBadParameter=TRUE;

	// make sure we're terminated
	arg[sizeof(arg)-1]='\0';
	requestedService[sizeof(requestedService)-1]='\0';
	logonUser[0]='\0';
	logonDomain[0]='\0';
	logonPassword[0]='\0';
	logonPassword2[0]='\0';
	logonSetting[0]='\0';
	autoRunCommandLine[0]='\0';
	autoRunCommandName[0]='\0';

	///////////////////////////////////////////////////////////////////////////
	// parse command line arguments
	for (i=1; i<argc; i++) { //start with 1 coz argv[0] is executable name
		res = parseParameter(argv[i],arg,sizeof(arg)-1);
		if ((res) && (strlen(arg)==1)) { // we got good option
			switch (arg[0]) {
				// list all services
				case 'l' :  {
					bList = TRUE;
					break;
				}
				// get service status
				case 's' : {
					if (++i>=argc) {
						bBadParameter=TRUE;
					} else {
						res = parseParameter(argv[i],arg,sizeof(arg)-1);
						if (res) {
							strncpy(requestedService,arg,sizeof(arg)-1);
							bStatus=TRUE;
						}
						else bBadParameter=TRUE;
					}
					break;
				}
				// timeout
				case 't' : {
					if (++i>=argc) {
						bBadParameter=TRUE;
					} else {
						res = parseParameter(argv[i],arg,sizeof(arg)-1);
						if (res) {
							j=atoi(arg);
							if (j>0) {
								j*=1000; // turn to milliseconds
								iTimeout=j;
								bTimeout=TRUE;
							} else if (j==0) {
								iTimeout=0;
								bTimeout=TRUE;
								bIndefinateTimeout=TRUE;
							}
						}
						else bBadParameter=TRUE;
					}
					break;
				}
				// pause
				case 'p' : {
					if (++i>=argc) {
						bBadParameter=TRUE;
					} else {
						res = parseParameter(argv[i],arg,sizeof(arg)-1);
						if (res) {
							j=atoi(arg);
							if (j>0) {
								j*=1000; // turn to milliseconds
								iPause=j;
								bPause=TRUE;
							}
						}
						else bBadParameter=TRUE;
					}
					break;
				}
				// auto logon commands
				case 'g' : {
					if (++i>=argc) {
						bBadParameter=TRUE;
					} else {
						res = parseParameter(argv[i],arg,sizeof(arg)-1);
						if (res) {
							if (strcmp(arg,"show")==0) { // display autologon info
								logonCommand=LOGON_COMMAND_SHOW;
								bAutoLogon=TRUE;
							} else if (strcmp(arg,"off")==0) {
								logonCommand=LOGON_COMMAND_OFF;
								bAutoLogon=TRUE;
							} else if (strcmp(arg,"on")==0) {
								if (++i>=argc) {
									bBadParameter=TRUE;
								} else {
									res = parseParameter(argv[i],arg,sizeof(arg)-1);
									if (res) {
										strcpy(logonPassword,argv[i]); // we used arg to check if parameter is good,
																	   // but we must take it from argv coz it's case sensitive!
										logonCommand=LOGON_COMMAND_ON;
										bAutoLogon=TRUE;
									}
								}
							} else {
								bBadParameter=TRUE;
							}
						}
						else bBadParameter=TRUE;
					}
					break;
				}

				// auto run commands
				case 'r' :
				case 'm' : {
					if (arg[0] == 'm')
						bAutoRunMachine = TRUE;
					if (++i>=argc) {
						bBadParameter=TRUE;
					} else {
						res = parseParameter(argv[i],arg,sizeof(arg)-1);
						if (res) {
							if (strcmp(arg,"add")==0) { // display autologon info
								autoRunCommand=AUTORUN_COMMAND_ADD;
								bAutoRun=TRUE;
							} else if (strcmp(arg,"del")==0) {
								autoRunCommand=AUTORUN_COMMAND_REMOVE;
								bAutoRun=TRUE;
							} else {
								bBadParameter=TRUE;
							}
							if (bAutoRun) {
								if (++i>=argc) {
									bBadParameter=TRUE;
								} else {
									res = parseParameter(argv[i],arg,sizeof(arg)-1);
									if (res) {
										strcpy(autoRunCommandName,arg);
									}
									if (autoRunCommand==AUTORUN_COMMAND_ADD) {
										if (++i>=argc) {
											bBadParameter=TRUE;
										} else {
											// rest of line is command line
											for (; i<argc; i++) {
												strcat(autoRunCommandLine,argv[i]);
												strcat(autoRunCommandLine," ");
											}
										}
									}
								}
							}
						}
						else bBadParameter=TRUE;
					}
					break;
				}


				// shutdown
				case 'x' : {
					if (++i>=argc) {
						bBadParameter=TRUE;
					} else {
						res = parseParameter(argv[i],arg,sizeof(arg)-1);
						if (res) {
							if (strcmp(arg,"logoff")==0) { // display autologon info
								shutdownCommand=SHUTDOWN_COMMAND_LOGOFF;
								bShutdown=TRUE;
							} else if (strcmp(arg,"poweroff")==0) {
								shutdownCommand=SHUTDOWN_COMMAND_POWEROFF;
								bShutdown=TRUE;
							} else if (strcmp(arg,"reboot")==0) {
								shutdownCommand=SHUTDOWN_COMMAND_REBOOT;
								bShutdown=TRUE;
							} else if (strcmp(arg,"shutdown")==0) {
								shutdownCommand=SHUTDOWN_COMMAND_SHUTDOWN;
								bShutdown=TRUE;
							} else {
								bBadParameter=TRUE;
							}
						}
						else bBadParameter=TRUE;
					}
					break;
				}

				// internet connection
				case 'i' : {
					if (++i>=argc) {
						bBadParameter=TRUE;
					} else {
						res = parseParameter(argv[i],arg,sizeof(arg)-1);
						if (res) {
							if (strcmp(arg,"connect")==0) {
								internetConnectionCommand=INTERNET_CONNECTION_COMMAND_CONNECT;
								bInternetConnection=TRUE;
							} else if (strcmp(arg,"disconnect")==0) {
								internetConnectionCommand=INTERNET_CONNECTION_COMMAND_DISCONNECT;
								bInternetConnection=TRUE;
							} else {
								bBadParameter=TRUE;
							}
						}
						else bBadParameter=TRUE;
					}
					break;
				}

				default: {
					bBadParameter=TRUE;
				}
			} // switch
		} else {
			bBadParameter=TRUE;
		}
	}

	///////////////////////////////////////////////////////////////////////////
	// display usage
	if (bBadParameter) {
		printf("\nUsage: AutoUp <option> [arguments]\n");
		printf("\nOptions:\n");
		printf("  -i connect\tConnect to the Internet using default connection.\n");
		printf("  -i disconnect\tDisconnect from the default Internet connection.\n");
		printf("  -p nnn\tPause nnn seconds. Can be combined with any other option.\n");
		printf("  -r add <keyname> <commandline>\n  \t\tMake <keyname> run on login (HKCU) using <commandline>.\n");
		printf("  -r del <keyname>\n  \t\tDelete <keyname> from login (HKCU) run list.\n");
		printf("  -m \t\tThe same as -r but sets run on machine startup (HKLM).\n");
		printf("  -g show\tShow current NT auto logon info.\n");
		printf("  -g on <password>\n  \t\tTurn on NT auto logon using case sensitive <password>.\n");
		printf("  -g off\tTurn off NT auto logon. Erases previously set password.\n");
		printf("  -l\t\tList all services and their statuses.\n");
		printf("  -s <service>\tReturn status of <service> immediately.\n");
		printf("  -t nnn\tTimeout value for -s and -i options.\n\t\tWaits nnn seconds for operation to complete successfully.\n\t\tnnn=0 waits indefinitely for successful completion.\n");
		printf("\nExample:\n");
		printf("  To automatically connect to the Internet when the machine is rebooted,\n");
		printf("  set up a default Internet connection with no password prompt etc., then run:\n");
		printf("  AutoUp -m add AutoUp C:\\AutoUp\\AutoUp.exe -i connect -t 0\n");
			
		return 0;
	}

	///////////////////////////////////////////////////////////////////////////
	// pause
	if (bPause) {
		printf("Pausing for %d seconds...",iPause/1000);
		_sleep(iPause);
		printf("done.\n");

		// no return here - we can run another command after this
	}

	///////////////////////////////////////////////////////////////////////////
	// list all services
	if (bList) {
		printf ("Listing all services:\n");
		numServices = enumerateServices(servstatus,sizeof(servstatus));
		if (numServices<1)
			iStatus=OPEN_SERVICES_FAILED;
		else {
			// now show the list
			for (i = 0; i < numServices; i++)
			{
				if (servstatus[i].ServiceStatus.dwCurrentState == SERVICE_RUNNING) {
							iStatus = SERVICE_STARTED;
						} else {
							iStatus = SERVICE_NOT_STARTED;
						}
				printf("service %s (%s): Status is %d\n",
								servstatus[i].lpServiceName,
								servstatus[i].lpDisplayName,
								iStatus);
			} // end loop
		}
		return 0;
	}

	///////////////////////////////////////////////////////////////////////////
	// get status of requested service with optional timeout
	if (bStatus) {
		iStatus=SERVICE_NOT_FOUND; //assume we won't find it
		do {
			numServices = enumerateServices(servstatus,sizeof(servstatus));
			if (numServices<1) {
				iStatus=OPEN_SERVICES_FAILED;
				break;
			} else {
				// now look for a match (both in service name and it's display name)
				for (i = 0; i < numServices; i++)
				{
					_strlwr(servstatus[i].lpServiceName); // to lower case
					_strlwr(servstatus[i].lpDisplayName); // to lower case
					if ( (strcmp(servstatus[i].lpServiceName, requestedService) == 0) ||
						(strcmp(servstatus[i].lpDisplayName, requestedService) == 0) )
					{
						// we found the service - returning status
						if (servstatus[i].ServiceStatus.dwCurrentState == SERVICE_RUNNING) {
							iStatus = SERVICE_STARTED;
							break;
						} else {
							iStatus = SERVICE_NOT_STARTED;
							break;
						}
					}
				}  // end loop
			}
			// if we have a timeout, check once a second
			if (!bIndefinateTimeout)
				iTimeout-=1000; // timeout remains 0 indefinitely
			if ( (iStatus == SERVICE_STARTED) || (iTimeout<0) ) // quit on timeout or if service is up. don't quit if timeout==0!!!
				bTimeout=FALSE;
			if (bTimeout) // sleep only if necessary
				_sleep(1000);
		} while (bTimeout);
		// iStatus = QueryServiceStarted(requestedService);
		printf("Status return values:\n\n");
		printf("\t%d\t%s\n",SERVICE_NOT_FOUND,STR_SERVICE_NOT_FOUND);
		printf("\t%d\t%s\n",OPEN_SERVICES_FAILED,STR_OPEN_SERVICES_FAILED);
		printf("\t %d\t%s\n",SERVICE_NOT_STARTED,STR_SERVICE_NOT_STARTED);
		printf("\t %d\t%s\n",SERVICE_STARTED,STR_SERVICE_STARTED);
		printf("\nStatus of service \"%s\" is %d\n",requestedService,iStatus);
		return iStatus;
	}

	///////////////////////////////////////////////////////////////////////////
	// auto logon routines
	if (bAutoLogon) {
		if ( (logonCommand==LOGON_COMMAND_OFF) || (logonCommand==LOGON_COMMAND_ON) ) {
			printf("WARNING: The entries in the system registry that are about to change\n");
			printf("are vital to system integrity. It is highly recommended that you\n");
			printf("backup the registry by copying the WinNT\\System32\\Config directory\n");
			printf("to a safe location so you may restore registry files in case of failure.\n\n");
			printf("If you've made a backup and wish to proceed, press 9 followed by <Enter>.\n");
			scanf("%s",arg);
			if (strncmp(arg,"9",2)!=0) {
				printf("operation aborted.\n");
				return 0;
			}
		}
		// turn off auto logon and erase password from registry
		if (logonCommand==LOGON_COMMAND_OFF) {
			iStatus=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon",0,KEY_WRITE,&hkey);
			if (iStatus==ERROR_SUCCESS) {
				strcpy(logonSetting,"0");
				// RegSetValueEx(hkey,"AutoAdminLogon",0,REG_SZ,logonSetting,strlen(logonSetting)+1); //length must include terminating null!!!
				RegDeleteValue(hkey,"AutoAdminLogon");
				// strcpy(logonPassword,"\0");
				// RegSetValueEx(hkey,"DefaultPassword",0,REG_SZ,logonPassword,strlen(logonPassword)+1);
				RegDeleteValue(hkey,"DefaultPassword");
				RegFlushKey(hkey); // don't want to take chances coz bad things can happen...
				RegCloseKey(hkey);
			} else
				printf ("\nError accessing registry\n");
		}

		// turn on auto logon and set password in registry
		if (logonCommand==LOGON_COMMAND_ON) {
			// verify password
			printf("\nPlease enter password again for confirmation:\n");
			scanf("%s",&logonPassword2);
			res=(strncmp(logonPassword,logonPassword2,sizeof(logonPassword))==0);
			if (!res) {
				printf("\nPasswords do not match. Remember the password is case sensitive.\n");
			} else {
				// set auto logon
				iStatus=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon",0,KEY_WRITE,&hkey);
				if (iStatus==ERROR_SUCCESS) {
					strcpy(logonSetting,"1");
					RegSetValueEx(hkey,"AutoAdminLogon",0,REG_SZ,logonSetting,strlen(logonSetting)+1); //length must include terminating null
					RegSetValueEx(hkey,"DefaultPassword",0,REG_SZ,logonPassword,strlen(logonPassword)+1); //otherwise we get serious trouble
					RegFlushKey(hkey); // don't want to take chances coz bad things can happen...
					RegCloseKey(hkey);
				} else {
					printf ("\nError accessing registry\n");
				}
			}
		}

		// show current NT auto logon info
		iStatus=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon",0,KEY_READ,&hkey);
		if (iStatus==ERROR_SUCCESS) {
			i=sizeof(logonSetting);
			iStatus=RegQueryValueEx(hkey,"AutoAdminLogon",0,NULL,logonSetting,&i);
			i=sizeof(logonDomain);
			iStatus=RegQueryValueEx(hkey,"DefaultDomainName",0,NULL,logonDomain,&i);
			i=sizeof(logonUser);
			iStatus=RegQueryValueEx(hkey,"DefaultUserName",0,NULL,logonUser,&i);
			i=sizeof(logonPassword);
			iStatus=RegQueryValueEx(hkey,"DefaultPassword",0,NULL,logonPassword,&i);
			RegCloseKey(hkey);

			printf("Current NT auto logon settings:\nEnabled=%s\tDomain=%s\tUser=%s\tPassword=%s\n",logonSetting,logonDomain,logonUser,logonPassword);
		} else
			printf ("\nError accessing registry\n");

		return 0;
	}



	///////////////////////////////////////////////////////////////////////////
	// auto run routines
	if (bAutoRun) {
		HKEY rootKey = bAutoRunMachine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
		// remove autorun entry from registry
		if (autoRunCommand==AUTORUN_COMMAND_REMOVE) {
			iStatus=RegOpenKeyEx(rootKey,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_WRITE,&hkey);
			if (iStatus==ERROR_SUCCESS) {
				RegDeleteValue(hkey,autoRunCommandName);
				RegFlushKey(hkey); // don't want to take chances coz bad things can happen...
				RegCloseKey(hkey);
			} else
				printf("\nError accessing registry\n");
		}

		// add autorun entry to registry
		if (autoRunCommand==AUTORUN_COMMAND_ADD) {
				// set auto logon
				iStatus=RegOpenKeyEx(rootKey,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_WRITE,&hkey);
				if (iStatus==ERROR_SUCCESS) {
					RegSetValueEx(hkey,autoRunCommandName,0,REG_SZ,autoRunCommandLine,strlen(autoRunCommandLine)+1); //length must include terminating null
					RegFlushKey(hkey); // don't want to take chances coz bad things can happen...
					RegCloseKey(hkey);
				} else {
					printf ("\nError accessing registry\n");
				}
		}

		// show current NT auto logon info
		iStatus=RegOpenKeyEx(rootKey,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_READ,&hkey);
		if (iStatus==ERROR_SUCCESS) {
			i=sizeof(autoRunCommandLine);
			iStatus=RegQueryValueEx(hkey,autoRunCommandName,0,NULL,autoRunCommandLine,&i);
			RegCloseKey(hkey);
			if (iStatus==ERROR_SUCCESS)
				printf("Updated Status: autorun key %s=%s\n",autoRunCommandName, autoRunCommandLine);
			else
				printf("Updated Status: autorun key %s no longer exists.\n",autoRunCommandName);
		} else
			printf ("\nError accessing registry\n",autoRunCommandName);

		return 0;
	}



	///////////////////////////////////////////////////////////////////////////
	// shutdown
	if (bShutdown) {
/*
		UINT uFlags=0;
		HANDLE hToken=NULL;
		TOKEN_PRIVILEGES * lptpNew;
		LUID_AND_ATTRIBUTES luidaa;

		switch (shutdownCommand) {
			case SHUTDOWN_COMMAND_LOGOFF:
				uFlags=EWX_LOGOFF;
				break;
			case SHUTDOWN_COMMAND_POWEROFF:
				uFlags=EWX_POWEROFF;
				break;
			case SHUTDOWN_COMMAND_REBOOT:
				uFlags=EWX_REBOOT;
				break;
			case SHUTDOWN_COMMAND_SHUTDOWN:
				uFlags=EWX_SHUTDOWN;
				break;
		}
		uFlags=uFlags | EWX_FORCE; // skip wait for application dialog

		iStatus=0;
		luidaa.Luid=SE_SHUTDOWN_NAME;
		luidaa.Attributes=SE_PRIVILEGE_ENABLED;
		lptpNew=(TOKEN_PRIVILEGES *)malloc(sizeof(DWORD)+sizeof(LUID_AND_ATTRIBUTES));
		lptpNew->PrivilegeCount=1;
		lptpNew->Privileges=&luidaa;

		if (OpenProcessToken(&hInstance,TOKEN_ADJUST_PRIVILEGES,&hToken)) {
			if (AdjustTokenPrivileges(hToken,FALSE,lptpNew,NULL,NULL.NULL)) {
				iStatus=ExitWindowsEx(uFlags,0);
			} else {
				iStatus=-1;
			}

		} else
			iStatus=-1;
		return iStatus;
*/
		printf("\nShutdown feature is currently unsupported.\n");
		return -1;
	}


	///////////////////////////////////////////////////////////////////////////
	// internet connection routines
	if (bInternetConnection) {
		DWORD dwFlags;
		iStatus = 1;
		// connect to internet using default connection
		if (internetConnectionCommand==INTERNET_CONNECTION_COMMAND_CONNECT) {
			while (!InternetGetConnectedState(&dwFlags,0) && (!bTimeout || bIndefinateTimeout || iTimeout > 0)) {
				printf("\nDialing default connection...");
				iStatus=InternetAutodial(INTERNET_AUTODIAL_FORCE_ONLINE  |
										 INTERNET_AUTODIAL_FORCE_UNATTENDED,
										 (DWORD)GetDesktopWindow());
				iStatus = iStatus ? 0 : -1;
				if (!bIndefinateTimeout)
					iTimeout-=1000; // timeout remains 0 indefinitely
				if (bTimeout) // sleep only if necessary
					_sleep(1000);
			}
		}
		// disconnect from default internet connection
		if (internetConnectionCommand==INTERNET_CONNECTION_COMMAND_DISCONNECT) {
			while (InternetGetConnectedState(&dwFlags,0) && (!bTimeout || bIndefinateTimeout || iTimeout > 0)) {
				printf("\nDisconnecting default connection...");
				iStatus=InternetAutodialHangup(0);
				iStatus = iStatus ? 0 : -1;
				if (!bIndefinateTimeout)
					iTimeout-=1000; // timeout remains 0 indefinitely
				if (bTimeout) // sleep only if necessary
					_sleep(1000);
			}
		}
		return iStatus;
	}

	///////////////////////////////////////////////////////////////////////////
	return iStatus;
}
