【C学习】全通纸笔王网上阅卷系统 APMS_Setup.exe IP修改程序

版权声明:本文由博主黄仁来原创,未经同意,不得转载。邮箱: [email protected] https://blog.csdn.net/weixin_43334745/article/details/84949665

前段时间用APMS远程改卷,发现浏览器需要安装阅卷插件,安装的过程提示IP无效,无法正常安装插件。后来用UEdit打开可见.exe文件中已经内置配置好的IP地址,可以通过修改为指定的IP后,便可以在本地正常安装插件并正常进入阅卷系统了。

为了方便起见,用C和C#以及Delphi写了一份代码,可以灵活实现IP的修改。比较简单:

 C语言版本:(HINT:在写入文件之前没有进行备份操作)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h> 

#define FILENAME "APMS_Setup.exe"


int GetPos(char str[], char key){
	int i;
	//通过循环依次取得字符串每个字符,循环结束条件 str[i] != '\0'
	for (i = 0; str[i] != '\0'; i++) {
		if (str[i] == key){
			return i;
		}
	}
	//如果没有结束,开始比较 key str[i] 
	return -1;
}

int IsValidIP(const char *ip)
{
	int section = 0;  //每一节的十进制值 
	int dot = 0;       //几个点分隔符 
	int last = -1;     //每一节中上一个字符 
	while (*ip){
		if (*ip == '.'){
			dot++;
			if (dot > 3){
				return 0;
			}
			if (section >= 0 && section <= 255){
				section = 0;
			}
			else{
				return 0;
			}
		}
		else if (*ip >= '0' && *ip <= '9'){
			section = section * 10 + *ip - '0';
			if (last == '0'){
				return 0;
			}
		}
		else{
			return 0;
		}
		last = *ip;
		ip++;
	}

	if (section >= 0 && section <= 255){
		section = 0;
	}
	return 1;
}



int main()
{
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);// 前景色_加强);
	printf("   ------------------------------------------------------------------\n");
	printf("\t\tAPMS全纸通阅卷插件-IP地址修改程序 v1.0\n");
	printf("\n");
	printf("    程序说明:初次在登录外网APMS阅卷系统时,需要安装阅卷插件才能阅卷。\n");
	printf("    由于远程APMS系统的IP地址问题,在安装插件时出现无法与服务器建议连接的错误。\n");
	printf("    本程序可以把插件中的IP地址修改为AMPS服务器的IP,使插件能正常安装。\n");
	printf("\t\tby  黄仁来 2018-9  东莞四中 via C Language");
	printf("\n\t\t   updated: 2018-12");
	printf("\n");
	printf("   ------------------------------------------------------------------\n");
	printf("\n");
	//还原回白色(RGB) 
	SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);



	/* 检查文件是否存在 */
	if ((_access(FILENAME, 0)) == -1)
	{
		SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
		printf("错误提示:文件不存在,请把本程序放在APMS_Setup.exe同一个目录下再运行。\n");
		SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
		system("pause");
		exit(1);
	}


	FILE * fp = fopen(FILENAME, "r+");
	if (fp == NULL) {
		SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
		printf("错误提示:文件打开失败!\n请关闭所有打开该文件的程序后再运行本程序。\n");
		SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
		system("pause");
		exit(1);
	}
	fseek(fp, 0xE0B6C, SEEK_SET);//光标移到0xE0B6C。
	char data[30] = { 0 };
	fgets(data, 30, fp);//读取指定偏移30个字节 

    //以下是IP地址在.exe中的偏移:
	/*
	000e0b6ch: 31 31 2E 32 32 2E 33 33 2E 34 34 2F 41 50 4D 53 ; 11.22.33.44/APMS
	000e0b7ch: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
	000e0b8ch: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
	000e0b9ch: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
	000e0bach: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
	000e0bbch: 20 20 20 20                                     ;
	*/

	int ipos = GetPos(data, 0x2F);//读取斜杠/在data字符串中的位置;
	char ip[15] = { 0 }; //IP地址长度不超过15位 111.111.111.111
	strncpy(ip, data, ipos); //读取data中前 ipos个字符。  
	printf("源文件(%s)读取成功!\n源文件IP地址:%s\n", FILENAME, ip);

	printf("\n请输入新的IP地址并回车:");


	char newip[30] = { 0 };
	while (1)
	{
		scanf("%15s", newip);
		if (!IsValidIP(newip)) {
			printf("IP地址格式不对,请重新输入IP地址:");
			scanf("%15s", newip);
		}
		else break;

	}
	strcat(newip, "/APMS");
	//newip[strlen(newip)] = 0x20;

	fseek(fp, 0xE0B6C, SEEK_SET);
	fputs(newip, fp);
	fclose(fp);
	printf("IP更新成功!\n");

	system("pause");
	return 0;

}

运行截图:

猜你喜欢

转载自blog.csdn.net/weixin_43334745/article/details/84949665