此篇文章將使用C++程式寫些惱人的程式。測試環境為 Virtualbox 虛擬機上的 Windows XP Pro版本,C++ IDE為Code::Blocks。
貪吃鬼(灌爆硬碟、吃光記憶體)
吃光記憶體C程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <stdio.h> #include <stdlib.h> int main() { char *data; // the data long long int N = (1024 * 1024 * 1024); // Memory size data = (char *) malloc(N); // Generate Random value for(int i=0;i< N;i++){ data[i] = (char) rand(); } return 0; } |
結果:還沒吃記憶體前,XP作業系統會刪掉此程式(當記憶體使用率 Mem Usage快暴時,下圖中的Hi.exe會突然不見)。
灌爆硬碟C程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <stdio.h> #include <stdlib.h> int main() { char *data; // data to be written int N = (1024*1024); // 10M for each flush data = (char *) malloc(N); // Generate Random value for(int i=0;i< N;i++){ data[i] = (char) rand(); } // This is the output file:"hd.dat" FILE *f = fopen("hd.dat", "w"); while(1) { // Write the random data to "hd.dat" fwrite(data, 1, N, f); fflush(f); } fclose(f); return 0; } |
此程式請讀者自行在虛擬機上測試。XP會出現如下訊息:
欺騙的藝術(按鈕的Enter,真的是Enter嗎?)
此例子是使用Code::Blocks 的GUI內建範例(Frame Base):
加上兩個按鈕的程式碼而做成的程式(Windows 程式設計可參考此處):
想想看,按下 Hi 與 Quit 按鈕會發生什麼事情呢?結果有沒有如預期呢?
示範程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | #if defined(UNICODE) && !defined(_UNICODE) #define _UNICODE #elif defined(_UNICODE) && !defined(UNICODE) #define UNICODE #endif #include <tchar.h> #include <windows.h> #define ID_HI 1 #define ID_QUIT 2 /* Declare Windows procedure */ LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); /* Make the class name into a global variable */ TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp"); int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) { HWND hwnd; /* This is the handle for our window */ MSG messages; /* Here messages to the application are saved */ WNDCLASSEX wincl; /* Data structure for the windowclass */ /* The Window structure */ wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */ wincl.style = CS_DBLCLKS; /* Catch double-clicks */ wincl.cbSize = sizeof (WNDCLASSEX); /* Use default icon and mouse-pointer */ wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC_ARROW); wincl.lpszMenuName = NULL; /* No menu */ wincl.cbClsExtra = 0; /* No extra bytes after the window class */ wincl.cbWndExtra = 0; /* structure or the window instance */ /* Use Windows's default colour as the background of the window */ wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; /* Register the window class, and if it fails quit the program */ if (!RegisterClassEx (&wincl)) return 0; /* The class is registered, let's create the program*/ hwnd = CreateWindowEx ( 0, /* Extended possibilites for variation */ szClassName, /* Classname */ _T("Code::Blocks Template Windows App"), /* Title Text */ WS_OVERLAPPEDWINDOW, /* default window */ CW_USEDEFAULT, /* Windows decides the position */ CW_USEDEFAULT, /* where the window ends up on the screen */ 544, /* The programs width */ 375, /* and height in pixels */ HWND_DESKTOP, /* The window is a child-window to desktop */ NULL, /* No menu */ hThisInstance, /* Program Instance handler */ NULL /* No Window Creation data */ ); /* Make the window visible on the screen */ ShowWindow (hwnd, nCmdShow); /* Run the message loop. It will run until GetMessage() returns 0 */ while (GetMessage (&messages, NULL, 0, 0)) { /* Translate virtual-key messages into character messages */ TranslateMessage(&messages); /* Send message to WindowProcedure */ DispatchMessage(&messages); } /* The program return-value is 0 - The value that PostQuitMessage() gave */ return messages.wParam; } /* This function is called by the Windows function DispatchMessage() */ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) /* handle the messages */ { case WM_CREATE: CreateWindowW(L"Button", L"Hi", WS_VISIBLE | WS_CHILD , 20, 50, 80, 25, hwnd, (HMENU) ID_HI, NULL, NULL); CreateWindowW(L"Button", L"Quit", WS_VISIBLE | WS_CHILD , 120, 50, 80, 25, hwnd, (HMENU) ID_QUIT, NULL, NULL); break; case WM_COMMAND: if (LOWORD(wParam) == ID_HI) { MessageBeep(MB_ICONWARNING); } if (LOWORD(wParam) == ID_QUIT) { MessageBox(hwnd, _T("Do you want to exit?!"), _T("What"), MB_OK); //PostQuitMessage(0); } break; case WM_DESTROY: PostQuitMessage (0); /* send a WM_QUIT to the message queue */ break; default: /* for messages that we don't deal with */ return DefWindowProc (hwnd, message, wParam, lParam); } return 0; } |
該休息了(猜錯密碼,電腦就關機)
此程式請在虛擬機測試!!!
打錯密碼的畫面如下:
該休息了程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <stdio.h> #include <stdlib.h> int main() { int password = 5201314; int n; printf("Please enter the password:"); scanf("%d", &n); if(n == password) { printf("Login successfully.\n"); } else { printf("Wrong password!!! The system is going to shutdown.....\n"); system("shutdown -s -t 30"); } return 0; } |
shutdown 是Windows的指令:
當然還有其他的惱人程式,例如讓滑鼠不受使用者控制、一直開啟瀏覽器的分頁等,還請讀者自行發揮想像力囉。
沒有留言:
張貼留言