Thread: Could use a bit of help here ..^^

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    66

    Talking Could use a bit of help here ..^^

    One night I was all lonely because my youtube channel had like 3 views So, naturally since it was 3 AM I had the good idea to create a project that spammed refreshes on it, now I am no longer lonely with over 900 views! XD

    Anyhow, I'm trying to clean the code up and write it in good programming style, so if anyone would mind to review the below files and tell me if their bad or good coding style would be really helpful!

    Code:
    //CViewBot.h
    //
    //Contains CViewBot class declaration
    //
    
    //Make sure we don't include multiple times
    #ifndef CVIEWBOT_H
    #define CVIEWBOT_H
    
    #include <windows.h>	//Required for: HWND function
    #include <direct.h>		//Required for: getcwd()
    #include <fstream>		//Required for: saving/loading config
    #include <iostream>		//Required for: outputing the iteration. YOU *MAY NEED TO* CHANGE THIS LINE
    
    //struct botConfig
    //
    //Contains the structure used by class CViewBot for
    //managing configuration information.
    
    struct botConfig
    {
    	int BufferTime;
    	int ViewIterations;
    	char *Window_ID;
    	char *URL;
    	char *BrowserPath;
    };
    
    
    //Class CViewBot
    //
    //Contains the code for setting, saving, and loading the config of the bot.
    //Also contains the code for all of the bots member functions and defines
    //the variables used.
    
    //**REQUIRES**:
    //struct botConfig
    //
    class CViewBot
    {
    private:
    	//Data used for storing the configuation:
    	botConfig bConfig;
    protected:
    	//Use to get the Browser Handle
    	HWND BrowserHandle(const char *WindowID);
    public:
    	//Constructor Function:
    	//Allocates memory for the pointer values
    	//in bConfig
    	CViewBot();
    
    	//Deconstructor Function:
    	//Frees up all the memory used during
    	//this classes life-time
    	~CViewBot();
    
    	//Run() begins the bot and runs it until
    	//it is finished
    	int Run();
    
    	//SetBuffer() sets the hard-buffer.
    	void SetBuffer(int NewBufferTime);
    
    	//SetViewTimes() sets the amount of times
    	//the bot will open and close the website
    	void SetViewIterations(int NewIterations);
    
    	//SetWindowID() sets the window ID of the
    	//browser, which will be taken as the
    	//title of the browser when you are on
    	//the selected web-page.
    	void SetWindowID(const char *Window_ID);
    
    	//SetURL() sets the URL that you wish
    	//to bot.
    	void SetURL(const char *URL);
    
    	//SetBrowserPath() sets the Path of the Browser
    	//that you wish the bot to use
    	void SetBrowserPath(const char *BrowserPath);
    
    	//SaveConfig() saves the config
    	int SaveConfig(char *file, char *path);
    
    	//LoadConfig() loads the config
    	int LoadConfig(char *file, char *path);
    
    	//Overloaded SaveConfig()--no parameters
    	//simply saves to the default area
    	int SaveConfig();
    
    	//Overloaded LoadConfig()--no parameters
    	//simple loads from the default area
    	int LoadConfig();
    };
    
    //The below line should correspond to:
    //#ifndef CVIEWBOT_H
    //#define CVIEWBOT_H
    #endif CVIEWBOT_H
    Code:
    //CViewBot.cpp
    //
    //Contains the class CViewBot
    //
    
    //Include the header for CViewBot class declaration
    #include "CViewBot.h"
    
    								
    ////////////////////////////////
    //Protected Functions
    
    //Use to get the Browser Handle
    HWND CViewBot::BrowserHandle(const char *WindowID)
    {
    	//If the WindowID is set we can look for it's handle
    	if (WindowID != NULL)
    	{
    		return FindWindow(NULL, WindowID);
    	}
    	else
    	{//Otherwise we can't return it
    		return NULL;
    	}
    }
    
    ////////////////////////////////
    //Public Functions
    
    
    //Constructor Function:
    //Allocates memory for the pointer values
    //in bConfig
    CViewBot::CViewBot()
    {
    	bConfig.BrowserPath	= new char[512];
    	bConfig.URL			= new char[512];
    	bConfig.Window_ID	= new char[512];
    }
    
    //Deconstructor Function:
    //Frees up all the memory used during
    //this classes life-time
    CViewBot::~CViewBot()
    {
    	//Clean up character arrays
    	delete [] bConfig.BrowserPath;
    	delete [] bConfig.URL;
    	delete [] bConfig.Window_ID;
    }
    
    //The run function for the bot, the "main" function
    int CViewBot::Run()
    {
    	//Start off in a loop that will continue
    	//until the bot has viewed the website
    	//the correct amount of times.
    	for (int n=bConfig.ViewIterations;n>0;n--)
    	{
    		//Now we open the browser and include the URL
    		//in the parameters--this is the ShellExecute version
    		ShellExecute(NULL, "open", bConfig.BrowserPath, bConfig.URL,
    					 NULL, NULL);
    		
    		//Sleep the bot for the "buffer"
    		//time as to now over-flow the browser
    		Sleep(bConfig.BufferTime);
    
    		//Exit the Browser
    		PostMessage(BrowserHandle(bConfig.Window_ID), WM_QUIT, 0, 0);
    
    		//Check for any errors that may have occured
    		if (GetLastError() != 0)
    			//return error for look up and quit the loop
    			return GetLastError();
    		
    		//Post what iteration we are on
    		//THIS LINE MAY NEED TO BE CHANGED ACCORDING TO THE PLATFORM
    		std::cout << "\nView Iteration: " << n;
    	}
    
    	//The bot has finished succesfully, return 0 so we
    	//know that no errors occured
    	return 0;
    }
    
    //The SetBuffer function sets the time in-between view refreshs
    void CViewBot::SetBuffer(const int NewBufferTime)
    {
    	//Straight forward function, sets the buffer time in miliseconds
    	bConfig.BufferTime = NewBufferTime;
    }
    
    //The SetViewIterations function sets the amount of times the
    //bot will refresh a webpage.
    void CViewBot::SetViewIterations(const int NewIterations)
    {
    	//Straight forward function, sets the amount of Iterations
    	//for the bot to run
    	bConfig.ViewIterations = NewIterations;
    }
    
    //SetWindowID() sets the window ID of the
    //browser, which will be taken as the
    //title of the browser when you are on
    //the selected web-page.
    void CViewBot::SetWindowID(const char *Window_ID)
    {
    	strcpy(bConfig.Window_ID, Window_ID);
    }
    
    
    //SetURL() sets the URL that you wish
    //to bot.
    void CViewBot::SetURL(const char *URL)
    {
    	strcpy(bConfig.URL, URL);
    }
    
    //SetBrowserPath() sets the Path of the Browser
    //that you wish the bot to use
    void CViewBot::SetBrowserPath(const char *BrowserPath)
    {
    	strcpy(bConfig.BrowserPath, BrowserPath);
    }
    
    //SaveConfig() takes two parameters, the first
    //is the filename to save to, and the second
    //is the path to the directory that you wish
    //the file to be saved in
    int CViewBot::SaveConfig(char *file, char *path)
    {
    	//Create a temp string to store path/file.
    	char *full_path;
    
    	//Allocate some space
    	full_path = new char[512];
    
    	//Create the string
    	strcpy(full_path, path);
    	strcat(full_path, "\\");
    	strcat(full_path, file);
    
    	//Open a filestream object to the file
    	std::ofstream InputFile(full_path, std::ios::trunc);
    
    	//Error checking:
    	if (!InputFile.good())
    		return 1;	// return error
    
    	//Input the config
    	InputFile << bConfig.BrowserPath;
    	InputFile << "\n" << bConfig.BufferTime;
    	InputFile << "\n" << bConfig.URL;
    	InputFile << "\n" << bConfig.ViewIterations;
    	InputFile << "\n" << bConfig.Window_ID;
    
    	//Close our file object
    	InputFile.close();
    
    	//Free up the space we allocated
    	delete [] full_path;
    
    	//return 0 for a succesful save
    	return 0;
    }
    
    //LoadConfig() takes two parameters, the first
    //is the filename to load from, and the second
    //is the path to the directory that you wish
    //the file to be loaded from
    int CViewBot::LoadConfig(char *file, char *path)
    {
    	//Create a temp string to store path/file.
    	char *full_path;
    
    	//Allocate some space
    	full_path = new char[512];
    
    	//Create the string
    	strcpy(full_path, path);
    	strcat(full_path, "\\");
    	strcat(full_path, file);
    
    	//Open a filestream object to the file
    	std::ifstream OpenFile(full_path);
    
    	//Error checking:
    	if (!OpenFile.good())
    		return 1;	// return error
    
    	//Create a buffer for the numerical values
    	char *num_buffer;
    
    	//Allocate some space
    	num_buffer = new char[512];
    
    	//Initilize the buffer - I'm not sure if you HAVE to do this,
    	//but I believe it prevents some weird bugs
    	strcpy(num_buffer, "");
    
    	//Input the config
    	OpenFile.getline(bConfig.BrowserPath, 511, '\n');		//BrowserPath config line
    	OpenFile.getline(num_buffer, 511, '\n');				//BufferTime config line
    
    	//Convert the buffer to BufferTime
    	bConfig.BufferTime = atoi(num_buffer);
    
    	//Clean buffer up for the next use
    	strcpy(num_buffer, "");
    
    	OpenFile.getline(bConfig.URL, 511, '\n');				//URL config line
    	OpenFile.getline(num_buffer, 511, '\n');				//ViewIterations config line
    
    	//Convert the buffer to ViewIterations
    	bConfig.ViewIterations = atoi(num_buffer);
    
    	OpenFile.getline(bConfig.Window_ID, 511, '\n');			//Window_ID config line
    
    	//Close our file object
    	OpenFile.close();
    
    	//Free up the space we allocated
    	delete [] num_buffer;
    	delete [] full_path;
    
    	//return 0 for a succesful save
    	return 0;
    }
    
    //Overloaded SaveConfig()--no parameters
    //simply saves to the default area
    int CViewBot::SaveConfig()
    {
    	//Create a temp string to store path/file.
    	char *full_path;
    
    	//Allocate some space
    	full_path = new char[512];
    
    	//Create the string
    	getcwd(full_path, 500);
    	strcat(full_path, "\\config.txt");
    
    	//Open a filestream object to the file
    	std::ofstream InputFile(full_path, std::ios::trunc);
    
    	//Error checking:
    	if (!InputFile.good())
    		return 1;	// return error
    
    	//Input the config
    	InputFile << bConfig.BrowserPath;
    	InputFile << "\n" << bConfig.BufferTime;
    	InputFile << "\n" << bConfig.URL;
    	InputFile << "\n" << bConfig.ViewIterations;
    	InputFile << "\n" << bConfig.Window_ID;
    
    	//Close our file object
    	InputFile.close();
    
    	//Free up the space we allocated
    	delete [] full_path;
    
    	//return 0 for a succesful save
    	return 0;
    }
    
    //Overloaded LoadConfig()--no parameters
    //simple loads from the default area
    int CViewBot::LoadConfig()
    {
    	//Create a temp string to store path/file.
    	char *full_path;
    
    	//Allocate some space
    	full_path = new char[512];
    
    	//Create the string
    	getcwd(full_path, 500);
    	strcat(full_path, "\\config.txt");
    
    	//Open a filestream object to the file
    	std::ifstream OpenFile(full_path);
    
    	//Error checking:
    	if (!OpenFile.good())
    		return 1;	// return error
    
    	//Create a buffer for the numerical values
    	char *num_buffer;
    
    	//Allocate some space
    	num_buffer = new char[512];
    
    	//Initilize the buffer - I'm not sure if you HAVE to do this,
    	//but I believe it prevents some weird bugs
    	strcpy(num_buffer, "");
    
    	//Input the config
    	OpenFile.getline(bConfig.BrowserPath, 511, '\n');		//BrowserPath config line
    	OpenFile.getline(num_buffer, 511, '\n');				//BufferTime config line
    
    	//Convert the buffer to BufferTime
    	bConfig.BufferTime = atoi(num_buffer);
    
    	//Clean buffer up for the next use
    	strcpy(num_buffer, "");
    
    	OpenFile.getline(bConfig.URL, 511, '\n');				//URL config line
    	OpenFile.getline(num_buffer, 511, '\n');				//ViewIterations config line
    
    	//Convert the buffer to ViewIterations
    	bConfig.ViewIterations = atoi(num_buffer);
    
    	OpenFile.getline(bConfig.Window_ID, 511, '\n');			//Window_ID config line
    
    	//Close our file object
    	OpenFile.close();
    
    	//Free up the space we allocated
    	delete [] num_buffer;
    	delete [] full_path;
    
    	//return 0 for a succesful save
    	return 0;
    }
    Code:
    //----------------------------------//
    // Name:	ViewSpam.cpp			//
    //									//
    // Author:	Chris					//
    // Purpose:	Was really bored	    //
    //									//
    // Usage:							//
    //		Run the program and through //
    //		the UI set the Path of		//
    //		your browser and then the	//
    //		URL you wish to visit, and	//
    //		then the ID of the Window	//
    //		when it is at that site		//
    //		and the number of times		//
    //		you wish to view it.		//
    //									//
    // Major Revisions:					//
    //	1.0  Alpha Testing: Working fine//
    //	1.1  Beta Testing: Adding a UI	//
    //	1.2	 Re-writing code to make it	//
    //		 more readable				//
    //  1.3  Added auto save and auto	//
    //       load features				//
    //----------------------------------//
    
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    //The header that defines the CViewBot class
    #include "CViewBot.h"
    
    //Not using any other namespaces
    using namespace std;
    
    //Current Version
    double dblVer = 1.3;
    
    //Displays the UI and returns the option selected
    int DisplayUI()
    {
    	cout << "\nViewBoT Version " << dblVer << ".\n\n\n";
    	cout << "1: Run Bot\n";
    	cout << "2: Specify Browser Path\n";
    	cout << "3: Specify URL Path\n";
    	cout << "4: Specify Window ID\n";
    	cout << "5: Set number of views\n";
    	cout << "6: Set buffer-time in-between views(in miliseconds)\n";
    	cout << "7: Help\n";
    	cout << "8: Exit\n\n";
    	int ret_val;
    	cin >> ret_val;
    	return ret_val;
    }
    
    //Displays help information
    void DoHelp()
    {
    	cout << "\n\n\n" <<
    		"Help Section - Dedicated Help for the new guy\n\n";
    	cout << "1: Run Bot\n";
    	cout << "By selecting this option you are starting the bot, and it will ";
    	cout <<	"begin to visit the URL in accordance with the set config.\n\n";
    
    	cout << "2: Specify Browser Path\n";
    	cout << "By selecting this option you are setting the Browser Path, this ";
    	cout << "is the path to your browser .exe file.\n\n";
    
    
    	cout << "3: Specify URL Path\n";
    	cout << "By selecting this option you are choosing to set the URL path, this ";
    	cout << "is basically the URL that you wish to viewbot.\n\n";
    
    
    	cout << "4: Specify Window ID\n";
    	cout << "By selecting this option you are choosing to set the Window ID, this ";
    	cout << "is what is displayed in the Title Bar of your browser when you visit the website.\n\n";
    
    
    	cout << "5: Set number of views\n";
    	cout << "By setting the number of views you set the amount of times the bot will ";
    	cout << "the selected webpage.\n\n";
    
    
    	cout << "6: Set buffer-time in-between views\n";
    	cout << "The buffer-time is the amount of time from when the bot opens your browser ";
    	cout << "to the time that the bot closes your browser, 15 seconds-the default setting- ";
    	cout << "is optimal time considering how long some browsers take to fully load, taking ";
    	cout << "into account how long the webpage takes to load as well.  This gives you a net time ";
    	cout << "of roughly 10-15 seconds on average for the average computer.  You can play around ";
    	cout << "/adjust this as you see fit, 15 may be too fast/slow.\n\n";
    	cout << "To tell if you are having an issue with your buffer time look for these signs: ";
    	cout << "\n\t1: The bot does not fully load the webpage.";
    	cout << "\n\t2: The bot loads into tabs rather than new browsers.";
    	cout << "\n\t3: The bot does not close the browser properly.";
    	cout << "\n\nIn any of the above cases you will want to increase your buffer time, ";
    	cout << "I would advise by 2 seconds at a time until the problems go away.\n\n";
    
    }
    
    int main()
    {
    	/*--------------*DISCLAIMER*--------------*/
    	cout << "*--------------*DISCLAIMER*--------------*\n";
    	cout << "This bot is provided to you \"as-is\" and you MAY get in trouble for\n";
    	cout << "the use of this bot as it could be considered a Denial of Service(DoS)\n";
    	cout << "attack if mis-used.  By using this bot you agree that I, the author,\n";
    	cout << "am in no way responsible for your use or mis-use of this bot.  There\n";
    	cout << "are no garuntees on this bot that it is anything at all-it is simply\n";
    	cout << "provided for entertainment and knowledge purposes *ONLY*. By using this\n";
    	cout << "bot you also agree to *NOT* use this for anything that may be considered \n";
    	cout << "\"morally wrong\" use.\n";
    	cout << "*--------------*DISCLAIMER*--------------*\n";
    
    	//The browser path
    	char localPath[512] = "";
    	//The website
    	char localURL[512] = "";
    	//For BrowserHandle()
    	char localWindowID[512] = "";
    	//i = number of views
    	int i = 10;
    	//Number of miliseconds to wait
    	int k = 15000;
    
    	CViewBot cvb;
    
    	//Attempt to load the config
    	cvb.LoadConfig();
    
    	//Call the UI
    	int ret = 9;
    	while ( ret != 8 )
    	{
    		ret = DisplayUI();
    		switch(ret)
    		{
    		case 1:
    			//Run the view bot
    			cvb.Run();
    			break;
    		case 2:
    			//Specify Browser Path
    			cout << "Enter path to browser: \n\t\t";
    			cin.ignore(512,'\n');
    			cin.getline(localPath, 511, '\n');
    			cvb.SetBrowserPath(localPath);
    			cout << "Path Set!  New path to browser: \n";
    			cout << localPath << "\n\n";
    			Sleep(1000);
    			break;
    		case 3:
    			//Specify URL path
    			cout << "Enter URL: \n\t";
    			cin.ignore(512,'\n');
    			cin.getline(localURL, 511, '\n');
    			cvb.SetURL(localURL);
    			cout << "URL Set!  New URL: \n";
    			cout << localURL << "\n\n";
    			Sleep(1000);
    			break;
    		case 4:
    			//Specify Window ID
    			cout << "Enter Window ID: \n\t\t";
    			cin.ignore(512,'\n');
    			cin.getline(localWindowID, 511, '\n');
    			cvb.SetWindowID(localWindowID);
    			cout << "Window ID Set!  New Window ID: \n";
    			cout << localWindowID << "\n\n";
    			Sleep(1000);
    			break;
    		case 5:
    			//Specify Number of Views
    			cout << "Enter amount of views: \n\t\t";
    			cin >> i;
    			cvb.SetViewIterations(i);
    			cout << "Number of Views Set!  New number of Views: \n";
    			cout << i << "\n\n";
    			Sleep(1000);
    			break;
    		case 6:
    			//Specify Buffer-Time
    			cout << "Enter buffer-time(in miliseconds): \n\t\t";
    			cin >> k;
    			cvb.SetBuffer(k);
    			cout << "Buffer Time Set!  New buffer time: \n";
    			cout << k << "\n\n";
    			Sleep(2000);
    			break;
    		case 7:
    			//Help
    			DoHelp();
    			break;
    		case 8:
    			//Free up resources and exit
    			//Start by saving the config
    			cvb.SaveConfig();
    			break;
    		default:
    			//Bad command
    			cout << "\nInvalid entry please try again.\n";
    			break;
    		}
    	}
    
    	return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Code:
    //CViewBot.h
    //
    //Contains CViewBot class declaration
    //
    
    //Make sure we don't include multiple times
    #ifndef CVIEWBOT_H
    #define CVIEWBOT_H
    
    #include <windows.h>	//Required for: HWND function
    #include <direct.h>		//Required for: getcwd()
    #include <fstream>		//Required for: saving/loading config
    #include <iostream>		//Required for: outputing the iteration. YOU *MAY NEED TO* CHANGE THIS LINE
    These should go into the cpp file unless any of them are needed by the header file. You don't want to put more than you have to in a header.

    Code:
    //struct botConfig
    //
    //Contains the structure used by class CViewBot for
    //managing configuration information.
    
    struct botConfig
    {
    	int BufferTime;
    	int ViewIterations;
    	char *Window_ID;
    	char *URL;
    	char *BrowserPath;
    };
    You should use UpperCamelCase for classes and types. You should start members with a lowercase. So either window_id or browserPath. You should be consistant in whatever style you use.


    Code:
    //Class CViewBot
    //
    //Contains the code for setting, saving, and loading the config of the bot.
    //Also contains the code for all of the bots member functions and defines
    //the variables used.
    
    //**REQUIRES**:
    //struct botConfig
    //
    class CViewBot
    {
    private:
    	//Data used for storing the configuation:
    	botConfig bConfig;
    Here this is no reason to have a struct botConfig when you can just make all it's members part of the class.
    Code:
    protected:
    	//Use to get the Browser Handle
    	HWND BrowserHandle(const char *WindowID);
    public:
    	//Constructor Function:
    	//Allocates memory for the pointer values
    	//in bConfig
    	CViewBot();
    
    	//Deconstructor Function:
    	//Frees up all the memory used during
    	//this classes life-time
    	~CViewBot();
    
    	//Run() begins the bot and runs it until
    	//it is finished
    	int Run();
    
    	//SetBuffer() sets the hard-buffer.
    	void SetBuffer(int NewBufferTime);
    
    	//SetViewTimes() sets the amount of times
    	//the bot will open and close the website
    	void SetViewIterations(int NewIterations);
    
    	//SetWindowID() sets the window ID of the
    	//browser, which will be taken as the
    	//title of the browser when you are on
    	//the selected web-page.
    	void SetWindowID(const char *Window_ID);
    
    	//SetURL() sets the URL that you wish
    	//to bot.
    	void SetURL(const char *URL);
    
    	//SetBrowserPath() sets the Path of the Browser
    	//that you wish the bot to use
    	void SetBrowserPath(const char *BrowserPath);
    
    	//SaveConfig() saves the config
    	int SaveConfig(char *file, char *path);
    
    	//LoadConfig() loads the config
    	int LoadConfig(char *file, char *path);
    
    	//Overloaded SaveConfig()--no parameters
    	//simply saves to the default area
    	int SaveConfig();
    
    	//Overloaded LoadConfig()--no parameters
    	//simple loads from the default area
    	int LoadConfig();
    Use default arguements instead. Like this: int LoadConfig(char *file ="config.txt", char *path="\\");

    Also, these should both take const char *s.
    Code:
    };
    
    //The below line should correspond to:
    //#ifndef CVIEWBOT_H
    //#define CVIEWBOT_H
    #endif CVIEWBOT_H
    Code:
    //CViewBot.cpp
    //
    //Contains the class CViewBot
    //
    
    //Include the header for CViewBot class declaration
    #include "CViewBot.h"
    
    								
    ////////////////////////////////
    //Protected Functions
    
    //Use to get the Browser Handle
    HWND CViewBot::BrowserHandle(const char *WindowID)
    {
    	//If the WindowID is set we can look for it's handle
    	if (WindowID != NULL)
    	{
    		return FindWindow(NULL, WindowID);
    	}
    	else
    	{//Otherwise we can't return it
    		return NULL;
    	}
    }
    
    ////////////////////////////////
    //Public Functions
    
    
    //Constructor Function:
    //Allocates memory for the pointer values
    //in bConfig
    CViewBot::CViewBot()
    {
    	bConfig.BrowserPath	= new char[512];
    	bConfig.URL			= new char[512];
    	bConfig.Window_ID	= new char[512];
    }
    
    //Deconstructor Function:
    //Frees up all the memory used during
    //this classes life-time
    CViewBot::~CViewBot()
    {
    	//Clean up character arrays
    	delete [] bConfig.BrowserPath;
    	delete [] bConfig.URL;
    	delete [] bConfig.Window_ID;
    }
    
    //The run function for the bot, the "main" function
    int CViewBot::Run()
    {
    	//Start off in a loop that will continue
    	//until the bot has viewed the website
    	//the correct amount of times.
    	for (int n=bConfig.ViewIterations;n>0;n--)
    Use i,j, or k for loop variables to follow convention.

    Prefer --n over n-- when possible. The reason is that n-- requires the program to make a copy of the n variable. For privative types a good compiler will optimize the problem out, but some compilers don't. For class types, such as iterators, the compiler is required to generate the code that creates a copy.
    Code:
    	{
    		//Now we open the browser and include the URL
    		//in the parameters--this is the ShellExecute version
    		ShellExecute(NULL, "open", bConfig.BrowserPath, bConfig.URL,
    					 NULL, NULL);
    		
    		//Sleep the bot for the "buffer"
    		//time as to now over-flow the browser
    		Sleep(bConfig.BufferTime);
    
    		//Exit the Browser
    		PostMessage(BrowserHandle(bConfig.Window_ID), WM_QUIT, 0, 0);
    
    		//Check for any errors that may have occured
    		if (GetLastError() != 0)
    			//return error for look up and quit the loop
    			return GetLastError();
    		
    		//Post what iteration we are on
    		//THIS LINE MAY NEED TO BE CHANGED ACCORDING TO THE PLATFORM
    		std::cout << "\nView Iteration: " << n;
    	}
    
    	//The bot has finished succesfully, return 0 so we
    	//know that no errors occured
    	return 0;
    }
    
    //The SetBuffer function sets the time in-between view refreshs
    void CViewBot::SetBuffer(const int NewBufferTime)
    {
    	//Straight forward function, sets the buffer time in miliseconds
    	bConfig.BufferTime = NewBufferTime;
    }
    
    //The SetViewIterations function sets the amount of times the
    //bot will refresh a webpage.
    void CViewBot::SetViewIterations(const int NewIterations)
    {
    	//Straight forward function, sets the amount of Iterations
    	//for the bot to run
    	bConfig.ViewIterations = NewIterations;
    }
    
    //SetWindowID() sets the window ID of the
    //browser, which will be taken as the
    //title of the browser when you are on
    //the selected web-page.
    void CViewBot::SetWindowID(const char *Window_ID)
    {
    	strcpy(bConfig.Window_ID, Window_ID);
    }
    This could lead to buffer overflow. If you are going to use C strings, you need to check to make sure the string copied is not bigger than the buffer.

    Code:
    //SetURL() sets the URL that you wish
    //to bot.
    void CViewBot::SetURL(const char *URL)
    {
    	strcpy(bConfig.URL, URL);
    }
    
    //SetBrowserPath() sets the Path of the Browser
    //that you wish the bot to use
    void CViewBot::SetBrowserPath(const char *BrowserPath)
    {
    	strcpy(bConfig.BrowserPath, BrowserPath);
    }
    //SaveConfig() takes two parameters, the first
    //is the filename to save to, and the second
    //is the path to the directory that you wish
    //the file to be saved in
    int CViewBot::SaveConfig(char *file, char *path)
    {
    	//Create a temp string to store path/file.
    	char *full_path;
    
    	//Allocate some space
    	full_path = new char[512];
    
    	//Create the string
    	strcpy(full_path, path);
    	strcat(full_path, "\\");
    	strcat(full_path, file);
    	//Open a filestream object to the file
    	std::ofstream InputFile(full_path, std::ios::trunc);
    
    	//Error checking:
    	if (!InputFile.good())
    		return 1;	// return error
    
    	//Input the config
    	InputFile << bConfig.BrowserPath;
    	InputFile << "\n" << bConfig.BufferTime;
    	InputFile << "\n" << bConfig.URL;
    	InputFile << "\n" << bConfig.ViewIterations;
    	InputFile << "\n" << bConfig.Window_ID;
    	//Close our file object
    	InputFile.close();
    
    	//Free up the space we allocated
    	delete [] full_path;
    
    	//return 0 for a succesful save
    	return 0;
    }
    
    //LoadConfig() takes two parameters, the first
    //is the filename to load from, and the second
    //is the path to the directory that you wish
    //the file to be loaded from
    int CViewBot::LoadConfig(char *file, char *path)
    {
    	//Create a temp string to store path/file.
    	char *full_path;
    
    	//Allocate some space
    	full_path = new char[512];
    
    	//Create the string
    	strcpy(full_path, path);
    	strcat(full_path, "\\");
    	strcat(full_path, file);
    Same as above for all these.

    Code:
    	//Open a filestream object to the file
    	std::ifstream OpenFile(full_path);
    Again, you should use lower case for all local variables. And be consistent whatever you do.

    Code:
    	//Error checking:
    	if (!OpenFile.good())
    		return 1;	// return error
    
    	//Create a buffer for the numerical values
    	char *num_buffer;
    
    	//Allocate some space
    	num_buffer = new char[512];
    
    	//Initilize the buffer - I'm not sure if you HAVE to do this,
    	//but I believe it prevents some weird bugs
    	strcpy(num_buffer, "");
    
    	//Input the config
    	OpenFile.getline(bConfig.BrowserPath, 511, '\n');		//BrowserPath config line
    	OpenFile.getline(num_buffer, 511, '\n');				//BufferTime config line
    
    	//Convert the buffer to BufferTime
    	bConfig.BufferTime = atoi(num_buffer);
    This code will set bConfig.BufferTime to zero if num_buffer is not a number. There are alternatives to atoi that do error checking.

    Code:
    	//Clean buffer up for the next use
    	strcpy(num_buffer, "");
    
    	OpenFile.getline(bConfig.URL, 511, '\n');				//URL config line
    	OpenFile.getline(num_buffer, 511, '\n');				//ViewIterations config line
    
    	//Convert the buffer to ViewIterations
    	bConfig.ViewIterations = atoi(num_buffer);
    Same as above.

    Code:
    	OpenFile.getline(bConfig.Window_ID, 511, '\n');			//Window_ID config line
    
    	//Close our file object
    	OpenFile.close();
    
    	//Free up the space we allocated
    	delete [] num_buffer;
    	delete [] full_path;
    
    	//return 0 for a succesful save
    	return 0;
    }
    
    //Overloaded SaveConfig()--no parameters
    //simply saves to the default area
    int CViewBot::SaveConfig()
    {
    	//Create a temp string to store path/file.
    	char *full_path;
    
    	//Allocate some space
    	full_path = new char[512];
    
    	//Create the string
    	getcwd(full_path, 500);
    	strcat(full_path, "\\config.txt");
    
    	//Open a filestream object to the file
    	std::ofstream InputFile(full_path, std::ios::trunc);
    
    	//Error checking:
    	if (!InputFile.good())
    		return 1;	// return error
    
    	//Input the config
    	InputFile << bConfig.BrowserPath;
    	InputFile << "\n" << bConfig.BufferTime;
    	InputFile << "\n" << bConfig.URL;
    	InputFile << "\n" << bConfig.ViewIterations;
    	InputFile << "\n" << bConfig.Window_ID;
    
    	//Close our file object
    	InputFile.close();
    
    	//Free up the space we allocated
    	delete [] full_path;
    
    	//return 0 for a succesful save
    	return 0;
    }
    
    //Overloaded LoadConfig()--no parameters
    //simple loads from the default area
    int CViewBot::LoadConfig()
    {
    	//Create a temp string to store path/file.
    	char *full_path;
    
    	//Allocate some space
    	full_path = new char[512];
    
    	//Create the string
    	getcwd(full_path, 500);
    	strcat(full_path, "\\config.txt");
    
    	//Open a filestream object to the file
    	std::ifstream OpenFile(full_path);
    
    	//Error checking:
    	if (!OpenFile.good())
    		return 1;	// return error
    
    	//Create a buffer for the numerical values
    	char *num_buffer;
    
    	//Allocate some space
    	num_buffer = new char[512];
    
    	//Initilize the buffer - I'm not sure if you HAVE to do this,
    	//but I believe it prevents some weird bugs
    	strcpy(num_buffer, "");
    
    	//Input the config
    	OpenFile.getline(bConfig.BrowserPath, 511, '\n');		//BrowserPath config line
    	OpenFile.getline(num_buffer, 511, '\n');				//BufferTime config line
    
    	//Convert the buffer to BufferTime
    	bConfig.BufferTime = atoi(num_buffer);
    
    	//Clean buffer up for the next use
    	strcpy(num_buffer, "");
    
    	OpenFile.getline(bConfig.URL, 511, '\n');				//URL config line
    	OpenFile.getline(num_buffer, 511, '\n');				//ViewIterations config line
    
    	//Convert the buffer to ViewIterations
    	bConfig.ViewIterations = atoi(num_buffer);
    
    	OpenFile.getline(bConfig.Window_ID, 511, '\n');			//Window_ID config line
    
    	//Close our file object
    	OpenFile.close();
    
    	//Free up the space we allocated
    	delete [] num_buffer;
    	delete [] full_path;
    
    	//return 0 for a succesful save
    	return 0;
    }
    Code:
    //----------------------------------//
    // Name:	ViewSpam.cpp			//
    //									//
    // Author:	Chris					//
    // Purpose:	Was really bored	    //
    //									//
    // Usage:							//
    //		Run the program and through //
    //		the UI set the Path of		//
    //		your browser and then the	//
    //		URL you wish to visit, and	//
    //		then the ID of the Window	//
    //		when it is at that site		//
    //		and the number of times		//
    //		you wish to view it.		//
    //									//
    // Major Revisions:					//
    //	1.0  Alpha Testing: Working fine//
    //	1.1  Beta Testing: Adding a UI	//
    //	1.2	 Re-writing code to make it	//
    //		 more readable				//
    //  1.3  Added auto save and auto	//
    //       load features				//
    //----------------------------------//
    
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    //The header that defines the CViewBot class
    #include "CViewBot.h"
    
    //Not using any other namespaces
    using namespace std;
    
    //Current Version
    const double dblVer = 1.3;
    Mark constants as const.

    The generally use style for constants is to use ALL_CAPS.
    Code:
    //Displays the UI and returns the option selected
    int DisplayUI()
    {
    	cout << "\nViewBoT Version " << dblVer << ".\n\n\n";
    	cout << "1: Run Bot\n";
    	cout << "2: Specify Browser Path\n";
    	cout << "3: Specify URL Path\n";
    	cout << "4: Specify Window ID\n";
    	cout << "5: Set number of views\n";
    	cout << "6: Set buffer-time in-between views(in miliseconds)\n";
    	cout << "7: Help\n";
    	cout << "8: Exit\n\n";
    	int ret_val;
    	cin >> ret_val;
    	return ret_val;
    }
    
    //Displays help information
    void DoHelp()
    {
    	cout << "\n\n\n" <<
    		"Help Section - Dedicated Help for the new guy\n\n";
    	cout << "1: Run Bot\n";
    	cout << "By selecting this option you are starting the bot, and it will ";
    	cout <<	"begin to visit the URL in accordance with the set config.\n\n";
    
    	cout << "2: Specify Browser Path\n";
    	cout << "By selecting this option you are setting the Browser Path, this ";
    	cout << "is the path to your browser .exe file.\n\n";
    
    
    	cout << "3: Specify URL Path\n";
    	cout << "By selecting this option you are choosing to set the URL path, this ";
    	cout << "is basically the URL that you wish to viewbot.\n\n";
    
    
    	cout << "4: Specify Window ID\n";
    	cout << "By selecting this option you are choosing to set the Window ID, this ";
    	cout << "is what is displayed in the Title Bar of your browser when you visit the website.\n\n";
    
    
    	cout << "5: Set number of views\n";
    	cout << "By setting the number of views you set the amount of times the bot will ";
    	cout << "the selected webpage.\n\n";
    
    
    	cout << "6: Set buffer-time in-between views\n";
    	cout << "The buffer-time is the amount of time from when the bot opens your browser ";
    	cout << "to the time that the bot closes your browser, 15 seconds-the default setting- ";
    	cout << "is optimal time considering how long some browsers take to fully load, taking ";
    	cout << "into account how long the webpage takes to load as well.  This gives you a net time ";
    	cout << "of roughly 10-15 seconds on average for the average computer.  You can play around ";
    	cout << "/adjust this as you see fit, 15 may be too fast/slow.\n\n";
    	cout << "To tell if you are having an issue with your buffer time look for these signs: ";
    	cout << "\n\t1: The bot does not fully load the webpage.";
    	cout << "\n\t2: The bot loads into tabs rather than new browsers.";
    	cout << "\n\t3: The bot does not close the browser properly.";
    	cout << "\n\nIn any of the above cases you will want to increase your buffer time, ";
    	cout << "I would advise by 2 seconds at a time until the problems go away.\n\n";
            cout << flush;
    It is geneally prefered to put <<endl at the end of each printed line. If you don't do this you should at least put << flush at the end so that the stream will flush it's buffer and print everything you told it to print.
    Code:
    }
    
    int main()
    {
    	/*--------------*DISCLAIMER*--------------*/
    	cout << "*--------------*DISCLAIMER*--------------*\n";
    	cout << "This bot is provided to you \"as-is\" and you MAY get in trouble for\n";
    	cout << "the use of this bot as it could be considered a Denial of Service(DoS)\n";
    	cout << "attack if mis-used.  By using this bot you agree that I, the author,\n";
    	cout << "am in no way responsible for your use or mis-use of this bot.  There\n";
    	cout << "are no garuntees on this bot that it is anything at all-it is simply\n";
    	cout << "provided for entertainment and knowledge purposes *ONLY*. By using this\n";
    	cout << "bot you also agree to *NOT* use this for anything that may be considered \n";
    	cout << "\"morally wrong\" use.\n";
    	cout << "*--------------*DISCLAIMER*--------------*\n";
    
    	//The browser path
    	char localPath[512] = "";
    	//The website
    	char localURL[512] = "";
    	//For BrowserHandle()
    	char localWindowID[512] = "";
    	//i = number of views
    	int i = 10;
    	//Number of miliseconds to wait
    	int k = 15000;
    
    	CViewBot cvb;
    
    	//Attempt to load the config
    	cvb.LoadConfig();
    
    	//Call the UI
    	int ret = 9;
    	while ( ret != 8 )
    	{
    		ret = DisplayUI();
    		switch(ret)
    		{
    		case 1:
    			//Run the view bot
    			cvb.Run();
    			break;
    		case 2:
    			//Specify Browser Path
    			cout << "Enter path to browser: \n\t\t";
    			cin.ignore(512,'\n');
    			cin.getline(localPath, 511, '\n');
    			cvb.SetBrowserPath(localPath);
    			cout << "Path Set!  New path to browser: \n";
    			cout << localPath << "\n\n";
    			Sleep(1000);
    			break;
    		case 3:
    			//Specify URL path
    			cout << "Enter URL: \n\t";
    			cin.ignore(512,'\n');
    			cin.getline(localURL, 511, '\n');
    			cvb.SetURL(localURL);
    			cout << "URL Set!  New URL: \n";
    			cout << localURL << "\n\n";
    			Sleep(1000);
    			break;
    		case 4:
    			//Specify Window ID
    			cout << "Enter Window ID: \n\t\t";
    			cin.ignore(512,'\n');
    			cin.getline(localWindowID, 511, '\n');
    			cvb.SetWindowID(localWindowID);
    			cout << "Window ID Set!  New Window ID: \n";
    			cout << localWindowID << "\n\n";
    			Sleep(1000);
    			break;
    		case 5:
    			//Specify Number of Views
    			cout << "Enter amount of views: \n\t\t";
    			cin >> i;
    			cvb.SetViewIterations(i);
    			cout << "Number of Views Set!  New number of Views: \n";
    			cout << i << "\n\n";
    			Sleep(1000);
    			break;
    		case 6:
    			//Specify Buffer-Time
    			cout << "Enter buffer-time(in miliseconds): \n\t\t";
    			cin >> k;
    			cvb.SetBuffer(k);
    			cout << "Buffer Time Set!  New buffer time: \n";
    			cout << k << "\n\n";
    			Sleep(2000);
    			break;
    		case 7:
    			//Help
    			DoHelp();
    			break;
    		case 8:
    			//Free up resources and exit
    			//Start by saving the config
    			cvb.SaveConfig();
    			break;
    		default:
    			//Bad command
    			cout << "\nInvalid entry please try again.\n";
    			break;
    		}
    	}
    
    	return EXIT_SUCCESS;
    }
    Other notes:
    You should use std::string instead of cstrings. This will eliminate potential problems with buffer overflow and having to allocate memory.
    Last edited by King Mir; 10-07-2007 at 02:09 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    66

    Thanks!

    Thank you very much for taking the time to help me out! Looks like I have some work to do,

    I really mean that though, thank you thank you thank you!

    Honestly I don't think I could say that enough, thank you!

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Someone's got a lot of time on their hands

    Just one nitpick:
    It is geneally prefered to put <<endl at the end of each printed line.
    Disagree. If you have a large block of output, avoid endl in there. Because it forces a flush, it will slow down output for no gain.
    Putting a single flush at the end of the block is good, though.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    Thanks for the note on flush ^^

    I've finished re-writing the code bits and using camelCase and UpperCamelCase conventions, however I have one question.

    I'd rather stick with cstrings, is this a bad thing? I have a hard time trying to pass std::string off as a parameter in functions like ShellExecute() where cstrings work perfectly fine. Is this a bad thing? Or am I just not doing something right, aka the likely answer.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If you need a const char* from a std::string, just calls its c_str() member function.

    std::string is inherently a lot safer than C strings.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 16 bit compilar or 32 bit compilar
    By rajkumarmadhani in forum C Programming
    Replies: 16
    Last Post: 12-04-2007, 09:48 AM
  2. Replies: 3
    Last Post: 11-10-2005, 10:53 AM
  3. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  4. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM