跳到正文

个人记录

洛谷安装程序

EricTang约 18 分钟
原文
文章信息与版本

文章信息

发布
2026年9月19日
收录
2026年9月19日
更新
2026年11月15日
篇幅
约 18 分钟

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

洛谷安装程序 1.0 by EricTang

你还在担心你不会输 洛谷 网站吗?
你还总是把 luogu.com.cn 输成 luogu.com.cc 吗?
你还在因为上不去 洛谷 而被老师制裁吗?
快来试试 洛谷安装程序,轻松在你的电脑上安装 洛谷。

C++
#include<windows.h>
#include<conio.h>
#include<cstdio> 
using namespace std;
const char* invalid="<>\"|?*";
char file[250],command[250],uninstallString[500],installDate[100],displayIcon[250],msg[500],installpos[MAX_PATH+5]="D:\\Luogu";
bool isinvalid(const char*path){
    for(int i=0;path[i]!='\0';++i)if(strchr(invalid,path[i])!=NULL)return true;
    return false;
}
bool checkchar(char c){return (c>='A'&&c<='Z')||(c>='a'&&c<='z');}
bool checkpath(const char*path){
    char fullPath[MAX_PATH]={0};DWORD len;DWORD attr;char p[MAX_PATH];size_t plen;size_t pos;char parent[MAX_PATH];DWORD pattr;
    if(path==NULL||path[0]=='\0')return false;
    if(strlen(path)>=2&&checkchar(path[0])&&path[1]==':'&&path[2]!='\\'&&path[2]!='/'){
        return false;
    }
    if(strlen(path)>=MAX_PATH||isinvalid(path))return false;
    len=GetFullPathNameA(path,MAX_PATH,fullPath,NULL);
    if(len==0||len>=MAX_PATH)return false;
    attr=GetFileAttributesA(path);
    if(attr!=INVALID_FILE_ATTRIBUTES){
        if(!(attr&FILE_ATTRIBUTE_DIRECTORY))return false;
        return true;
    }
    strncpy(p,path,MAX_PATH-1);
    p[MAX_PATH-1]='\0';
    plen=strlen(p);
    while(plen>3&&(p[plen-1]=='\\'||p[plen-1]=='/'))p[plen-1]='\0',--plen;
    pos=strcspn(p,"\\/");
    if(pos==plen){
        pattr=GetFileAttributesA(p);
        if(!(pattr!=INVALID_FILE_ATTRIBUTES&&(pattr&FILE_ATTRIBUTE_DIRECTORY)))return false;
        return true;
    }
    else{
        size_t last=0;
        int found=0;
        for(size_t i=0;i<plen;++i)if(p[i]=='\\'||p[i]=='/')last=i,found=1;
        if(!found){
            pattr=GetFileAttributesA(p);
            if(!(pattr!=INVALID_FILE_ATTRIBUTES&&(pattr&FILE_ATTRIBUTE_DIRECTORY)))return false;
            return true;
        }
        pos=last;
    }
    if(pos==0)parent[0]='\\',parent[1]='\0';
    else strncpy(parent,p,pos),parent[pos]='\0';
    if(strlen(parent)==2&&parent[1]==':')parent[2]='\\',parent[3]='\0';
    pattr=GetFileAttributesA(parent);
    if(pattr==INVALID_FILE_ATTRIBUTES||!(pattr&FILE_ATTRIBUTE_DIRECTORY))return false;
    return true;
}
int main(){
	SetConsoleTitleA("洛谷 For Windows 1.0 安装程序");
	MessageBox(NULL,"欢迎使用 洛谷 For Windows 1.0 安装程序\n\n此程序将引导你完成 洛谷 For Windows 1.0_by EricTang 的安装。\
\n单击“确定”继续 洛谷 的安装。","洛谷 For Windows 1.0 安装程序",MB_OK|MB_ICONINFORMATION);
	int choice=IDTRYAGAIN;
	do{
		sprintf(msg,"安装程序将把 洛谷 For Windows 安装到以下目录:\n\"%s\"\n单击“继续”开始对 洛谷 For Windows 的安装。\
\n单击“重试”更改 洛谷 For Windows 的安装目录。\n单击“取消”取消对 洛谷 For Windows 的安装。",installpos);
		choice=MessageBox(NULL,msg,"洛谷 For Windows 1.0 安装程序",MB_CANCELTRYCONTINUE|MB_ICONINFORMATION|MB_DEFBUTTON3);
		if(choice==IDCANCEL)return 0;
		else if(choice==IDTRYAGAIN){
			bool flag=false;
			do{
				system("cls");
				printf("请输入安装目录: ");
				scanf(" %[^\n]",file);
				flag=checkpath(file);
				if(!flag)MessageBox(NULL,"输入的路径无效!","错误",MB_OK|MB_ICONERROR);
			}while(!flag);
			strcpy(installpos,file);
		}
	}while(choice==IDTRYAGAIN);
	sprintf(command,"rd /s /q \"%s\">nul 2>&1",installpos); 
	system(command);
	sprintf(command,"mkdir \"%s\">nul 2>&1",installpos);
	system(command);
	int num=MessageBox(NULL,"是否隐藏 洛谷 For Windows 1.0 安装文件夹?\n(安装文件夹仅约5KB,占用空间极小,建议隐藏以保持磁盘整洁)","洛谷 For Windows 1.0 安装程序",MB_YESNO|MB_ICONQUESTION);
	printf("洛谷 For Windows 正在安装 . . .\n\n");
	printf("状态: 准备图标(从洛谷下载) . . .\n");
	system("powershell -command \"Invoke-WebRequest -Uri 'https://www.Luogu.com.cn/favicon.ico' -OutFile 'Luogu.ico'\"");
	FILE* LuoguIcon=fopen("Luogu.ico","rb");
	if(LuoguIcon==NULL){
	    printf("由于图标准备失败,安装程序无法继续。请尝试重新以管理员身份运行安装程序。");
		fclose(LuoguIcon);
	    return 1;
	}
	fclose(LuoguIcon);
	printf("状态: 复制图标 . . .\n");
	sprintf(command,"copy /Y Luogu.ico \"%s\\Luogu.ico\">nul 2>&1",installpos);
	if(system(command)){
		printf("由于图标复制失败,安装程序无法继续。请尝试重新以管理员身份运行安装程序。");
		return 1;
	}
	system("del Luogu.ico>nul 2>&1");
	printf("状态: 正在创建卸载程序 . . .\n");
	sprintf(file,"%s\\Uninst.bat",installpos);
	FILE *fp=fopen(file,"w");
	fprintf(fp,"@echo off\ndel %%userprofile%%\\desktop\\洛谷.lnk\nreg delete \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Luogu\" /f\
\ndel \"%%USERPROFILE%%\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\*洛谷.lnk\"\nstart \"\" mshta vbscript:CreateObject(\"WScript.Shell\")\
.Popup(\"洛谷 For Windows 已完全从你的电脑中删除。\",5,\"提示\",64)(window.close)\nstart \"\" cmd /c \"timeout /t 2>nul 2>&1&rd /s /q ^\"%s^\"\"",installpos);
	fclose(fp);
	FILE* LuoguUninst=fopen(file,"rb");
	if(LuoguUninst==NULL){
	    printf("由于卸载程序准备失败,安装程序无法继续。请尝试重新以管理员身份运行安装程序。");
		fclose(LuoguUninst);
	    return 1;
	}
	fclose(LuoguUninst);
	if(num==IDYES){
		printf("状态: 隐藏安装文件夹 . . .\n");
		sprintf(command,"attrib +s +h \"%s\"", installpos);
		system(command);
	}
	printf("状态: 正在创建桌面快捷方式 . . .\n");
	sprintf(command,"powershell -NoProfile -Command \"$s=(New-Object -ComObject WScript.Shell).CreateShortcut((Join-Path ([Environment]::\
GetFolderPath('Desktop')) '洛谷.lnk'));$s.TargetPath='https://www.luogu.com.cn';$s.IconLocation='%s\\Luogu.ico';$s.Save()\"", installpos);
	system(command);
	printf("状态: 正在创建开始菜单快捷方式 . . .\n");
	sprintf(command,"powershell -NoProfile -Command \"$s=(New-Object -ComObject WScript.Shell).CreateShortcut((Join-Path ([Environment]::\
GetFolderPath('Programs')) '!洛谷.lnk'));$s.TargetPath='https://www.luogu.com.cn';$s.IconLocation='%s\\Luogu.ico';$s.Save()\"", installpos);
	system(command);
	sprintf(command,"powershell -NoProfile -Command \"$s=(New-Object -ComObject WScript.Shell).CreateShortcut((Join-Path ([Environment]::\
GetFolderPath('Programs')) '!卸载洛谷.lnk'));$s.TargetPath='%s\\Uninst.bat';$s.IconLocation='%s\\Luogu.ico';$s.Save()\"", installpos, installpos);
	system(command);
	printf("状态: 正在写入注册表项 . . .\n\n");
	HKEY hKey;LONG lResult;
    const char* subKey="Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Luogu";
    lResult=RegCreateKeyExA(HKEY_CURRENT_USER,subKey,0,NULL,REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&hKey,NULL);
	if(lResult!=ERROR_SUCCESS) {
        printf("由于创建注册表项失败,安装程序无法继续。请尝试重新以管理员身份运行安装程序。\n错误码: %ld\n",lResult);
        return 1;
    }
    const char* displayName="洛谷";
	sprintf(uninstallString,"\"%s\\Uninst.bat\"",installpos);
    const char* displayVersion="1.0";
    const char* publisher="EricTang";
    sprintf(displayIcon,"%s\\Luogu.ico",installpos);
    SYSTEMTIME st;GetLocalTime(&st);
    sprintf(installDate,"%04d%02d%02d",st.wYear, st.wMonth, st.wDay);
    DWORD estimatedSize=6,noModify=1,noRepair=1;
    RegSetValueExA(hKey,"DisplayName"    ,0,REG_SZ,(const BYTE*)displayName    ,strlen(displayName)    +1);
    RegSetValueExA(hKey,"UninstallString",0,REG_SZ,(const BYTE*)uninstallString,strlen(uninstallString)+1);
    RegSetValueExA(hKey,"DisplayVersion" ,0,REG_SZ,(const BYTE*)displayVersion ,strlen(displayVersion) +1);
    RegSetValueExA(hKey,"Publisher"      ,0,REG_SZ,(const BYTE*)publisher      ,strlen(publisher)      +1);
    RegSetValueExA(hKey,"InstallDate"    ,0,REG_SZ,(const BYTE*)installDate    ,strlen(installDate)    +1);
    RegSetValueExA(hKey,"DisplayIcon"    ,0,REG_SZ,(const BYTE*)displayIcon    ,strlen(displayIcon)    +1);
    RegSetValueExA(hKey,"EstimatedSize"  ,0,REG_DWORD,(const BYTE*)&estimatedSize,sizeof(estimatedSize));
    RegSetValueExA(hKey,"NoModify"       ,0,REG_DWORD,(const BYTE*)&noModify     ,sizeof(noModify));
    RegSetValueExA(hKey,"NoRepair"       ,0,REG_DWORD,(const BYTE*)&noRepair     ,sizeof(noRepair));
    RegCloseKey(hKey);
    if(MessageBox(NULL,"洛谷 For Windows 的安装已完成。\n单击“是”查看 洛谷 For Windows 1.0 的特色,\
\n单击“否”退出安装程序。","洛谷 For Windows 1.0 安装程序",MB_YESNO|MB_ICONINFORMATION|MB_DEFBUTTON2)==IDYES){
		system("cls");
		printf("1) 安装程序会在桌面和“开始菜单”里添加“洛谷”快捷方式,妈妈再也不用担心我不会输洛谷网站或是手滑输成 luogu.com.cc了!\
\n请选择:\n[1] 尝试从“开始菜单”打开洛谷\n[Enter] 跳过\n");
		char ch;
		do{
			ch=_getch();
			if(ch=='1'){
				printf("\r“开始菜单”已打开。");
				keybd_event(VK_LWIN,0,0,0);
				keybd_event(VK_LWIN,0,KEYEVENTF_KEYUP,0);
			}
		}while(ch!='\r');
		printf("\n\n2) 安装程序会在“程序和功能”和“开始菜单”卸载项,这样如果你不想用洛谷了也可以随时卸载 (doge)。\
\n请选择:\n[1] 打开“开始菜单”\n[2] 打开“程序和功能”\n[3] 查看 卸载脚本(估计你看不懂)\n[Enter] 跳过\n");
		do{
			ch=_getch();
			if(ch=='1'){
				printf("\r“开始菜单”已打开。");
				keybd_event(VK_LWIN,0,0,0);
				keybd_event(VK_LWIN,0,KEYEVENTF_KEYUP,0);
			}
			if(ch=='2'){
				printf("\r“程序和功能”已打开。请使用滚轮滚动到底部以查看“洛谷”的卸载项。");
				system("appwiz.cpl");
			}
			if(ch=='3'){
				printf("\r卸载脚本 已在“记事本”打开。");
				sprintf(command,"notepad \"%s\\Uninst.bat\"",installpos);
				system(command);
			} 
		}while(ch!='\r');
		printf("\n\n3) 此程序的笑点:\n计算:\n“开始菜单”的 !洛谷.lnk     204字节\n“开始菜单”的 !卸载洛谷.lnk 578字节\n注册表项\
                     约200字节\n桌面的 洛谷.lnk              204字节\n图标                         4286字节\n卸载程序                     405字节\
\n总计                         5742 字节\n此.cpp文件                   9810字节\n(安装程序比它安装出来的所有东西加起来还要大)\n[Enter] 结束\n");
		do{ch=_getch();}while(ch!='\r');
	}
	return 0;
}
原文