C++を用いた軽度~高度な実験的デモプログラム集

誤動作やデモのためのC++コード例

以下のコードは、あくまで學習・共有を目的としており、悪用しないよう慎重に扱う必要があります。

レベル1:日常的な注意程度で済むデモ

1. 連続で画面に文字を出力(改行あり)
#include <iostream>
#include <string>

int main() {
    const std::string message = " tái duìxiàng xiǎng duì nǐ shuō de huà";
    while (true) {
        std::cout << message << std::endl;
    }
    return 0;
}
2. 連続で画面に文字を出力(改行なし)
#include <iostream>
#include <string>

int main() {
    const std::string msg = "无限重复的消息";
    while (true) {
        std::cout << msg;
    }
    return 0;
}

※ 出力が••••に多くなると端末が応答を停止する可能性があります。

3. コンソールの色を変更する(Windows件)
#include <iostream>

int main() {
    // 背景を黒、文字を赤(例)
    system("color 40");
    std::cout << "警告:画面の色が変化しました" << std::endl;
    return 0;
}

色コードは2桁で表記: 先頭:背景色、末尾:文字色(0~F:Black~Bright White)

コード
0
1
2
3水色
4
5
6
7
8グレー
9淡い青
A薄緑
B明るい水色
C淡い赤
D淡い紫
E薄黄色
F格外の白
4. ディレクトリ構造を再帰表示する(一時的に応答を停止させる)
#include <iostream>

int main() {
    std::cout << "コンピュータの内容を一時的に解析中..." << std::endl;
    system("dir /s");
    return 0;
}

レベル2:若干の対策が必要なデモ

5. Windows.MESSAGEBOXによる強調表示
#include <iostream>
#ifdef _WIN32
#include <windows.h>
int main() {
    const char* text = "注意:システムに異常が検出されました";
    const char* title = "警告";
    MessageBoxA(NULL, text, title, MB_ICONWARNING | MB_OK);
    return 0;
}
#else
int main() {
    std::cout << "この機能はWindows環境でのみ動作します" << std::endl;
    return 0;
}
#endif

アイコン定数: MB_ICONINFORMATION、MB_ICONWARNING、MB_ICONERROR ボタン定数: MB_OK、MB_YESNO、MB_OKCANCEL、MB_RETRYCANCEL

6. 無限に新規ターミナルを起動させる
#include <iostream>
#ifdef _WIN32
#include <windows.h>
int main() {
    while (true) {
        system("start cmd");
    }
    return 0;
}
#else
int main() {
    while (true) {
        system("x-terminal-emulator &");
    }
    return 0;
}
#endif

⚠ 解法:処理を強制終了させるには、タスクマネージャーまたはCtrl+Alt+Delで該当プロセスを終了させてください。

7. 一時的にマウスカーソルを不安定にする
#include <iostream>
#ifdef _WIN32
#include <windows.h>
#include <cstdlib>
#include <ctime>

int main() {
    srand(static_cast<unsigned>(time(0)));
    const int w = GetSystemMetrics(SM_CXSCREEN);
    const int h = GetSystemMetrics(SM_CYSCREEN);

    while (true) {
        const int x = rand() % w;
        const int y = rand() % h;
        SetCursorPos(x, y);
    }
    return 0;
}
#else
// POSIX環境ではX11/Xlibなどが必要(ここでは省略)
int main() {
    std::cerr << "この機能はWindows環境でのみ動作します" << std::endl;
    return 1;
}
#endif

レベル3:重要システム操作に影響するデモ(慎重に使用)

8. 指定時間後にシャットダウンを実行
#include <iostream>
#ifdef _WIN32
#include <cstdlib>
int main() {
    const int seconds = 60; // 例:60秒後にシャットダウン
    std::string cmd = "shutdown /s /t " + std::to_string(seconds);
    system(cmd.c_str());
    return 0;
}
#else
#include <cstdlib>
int main() {
    const int delay = 60;
    std::string cmd = "shutdown -h +" + std::to_string(delay / 60);
    system(cmd.c_str());
    return 0;
}
#endif

✅ 解除方法(Windows):cmdで `shutdown -a` を実行 ✅ 解除方法(Linux/macOS):`sudo shutdown -c`

9. 即時シャットダウン
#ifdef _WIN32
#include <cstdlib>
int main() {
    system("shutdown /s /t 0");
    return 0;
}
#else
#include <cstdlib>
int main() {
    system("shutdown -h now");
    return 0;
}
#endif
10. 即時再起動
#ifdef _WIN32
#include <cstdlib>
int main() {
    system("shutdown /r /t 0");
    return 0;
}
#else
#include <cstdlib>
int main() {
    system("reboot");
    return 0;
}
#endif
11. 時間指定で再起動
#ifdef _WIN32
#include <cstdlib>
int main() {
    const int delay_seconds = 120;
    std::string cmd = "shutdown /r /t " + std::to_string(delay_seconds);
    system(cmd.c_str());
    return 0;
}
#else
#include <cstdlib>
int main() {
    const int minutes = 2;
    std::string cmd = "shutdown -r +" + std::to_string(minutes);
    system(cmd.c_str());
    return 0;
}
#endif

補足:実行ファイルの拡張子をごまかす方法

  1. 管理権限でファイル名表示設定を確認する(例:[設定]→[ファイル エクスプローラーのオプション])
  2. ファイル名の先頭に U+202E(Right-to-Left Override)を挿入
  3. 実際の拡張子(例:`.exe`)を隠した上で、視認名を`.txt`のように伪装
  4. ショートカットのアイコンをテキストファイル風に変更可能

⚠ 本行為は他者への信任を損なう可能性があるため、絶対に深.queryをしないでください。システムに多大な影響を与えるコードを実行する際は、自己責任でお願いします。

タグ: Windows-API C++-Snippet Shell-Command Console-Color Process-Control

5月17日 23:41 投稿