// 参考链接
// C\C++控制台程序隐藏方法总结 https://blog.csdn.net/lynch0571/article/details/33320551
// 取消提醒《 Win10你要允许此应用对你的电脑进行更改吗 》 http://www.w10zj.com/Win10xy/Win10yh_1623.html
/*
这是一个后台运行的、每隔60s检查一次的、带有日志功能的断网重新连接程序。
早上0到6点,不执行断网重连和写日志功能,只有连续6次ping测试都失败了才会启动联网程序
要想使用它必须要配置好c语言环境,比如安装有mingw的codeblocks,vs不能后台运行,只能在codeblocks中实现后台
实现的方式就是每隔一段时间就去ping一下百度,如果ping失败了就启动上网程序,上网程序会自动连接网络。
使用时将该软件加入开机自启软件即可
*/
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) // 加上后可以后台运行
#include<windows.h>
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <ctime>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h> // linux下头文件
#endif
#include<ctime>
#define FILE_MAX_SIZE (1024*1024)// 日志文件最大为1M
using namespace std;
/*
获得当前时间字符串
@param buffer [out]: 时间字符串
@return 空
*/
void get_local_time(char* buffer)
{
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
(timeinfo->tm_year + 1900), timeinfo->tm_mon + 1, timeinfo->tm_mday,
timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
}
/*
获得日志文件大小
@param filename [in]: 文件名
@return 文件大小
*/
long get_file_size(char* filename)
{
long length = 0;
FILE *fp = NULL;
fp = fopen(filename, "rb");
if (fp != NULL)
{
fseek(fp, 0, SEEK_END);
length = ftell(fp);
}
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
return length;
}
/*
写入日志文件
@param filename [in]: 日志文件名
@param max_size [in]: 日志文件大小限制
@param buffer [in]: 日志内容
@param buf_size [in]: 日志内容大小
@return 空
*/
void write_log_file(char* filename, long max_size, char* buffer, unsigned buf_size)
{
if (filename != NULL && buffer != NULL)
{
// 文件超过最大限制, 删除
long length = get_file_size(filename);
if (length > max_size)
{
unlink(filename); // 删除文件
}
// 写日志
{
FILE *fp;
fp = fopen(filename, "at+");
if (fp != NULL)
{
char now[32];
memset(now, 0, sizeof(now));
get_local_time(now);
fwrite(now, strlen(now) + 1, 1, fp);
fwrite(buffer, buf_size, 1, fp);
fclose(fp);
fp = NULL;
}
}
}
}
/*
后台运行的、每隔60s检查一次的、带有日志功能的断网重新连接程序
*/
void p()
{
// 隐藏窗口
HWND hwnd;
hwnd = FindWindow("ConsoleWindowClass", NULL); //处理顶级窗口的类名和窗口名称匹配指定的字符串,不搜索子窗口。
if (hwnd)
{
ShowWindow(hwnd, SW_HIDE); //设置指定窗口的显示状态
}
while (1)
{
// 睡眠60s
_sleep(60000);
// 早上0到6点,不执行断网重连和写日志功能
// now为1970 到目前经过秒数,ltm->tm_hour为现在的小时数
time_t now = time(0);
tm *ltm = localtime(&now);
if (ltm->tm_hour > 6)
{
int count = 0;// ping失败的次数
for (int i = 0; i < 6; i++)
{
// 写入日志文件
char buffer[32];
memset(buffer, 0, sizeof(buffer));
// 断网重连
clock_t start, end;// 计时
start = clock();
system("ping 8.8.8.8 -w 3500");// system执行cmd命令, 可以用cout打印返回结果
end = clock();
double duration = ((double)end - start) / CLOCKS_PER_SEC;
if (duration > 10.0)
{
count += 1;
sprintf(buffer, "====> %s%d%s\n", "第", i, "次ping失败#############");
}
else
{
sprintf(buffer, "====> %s%d%s\n", "第", i, "次ping成功");
}
char s[] = "log.txt";
write_log_file(s, FILE_MAX_SIZE, buffer, strlen(buffer));
}
if(count == 6)
system("start /b C:\\\"Program Files\"\\\"Ruijie Networks\"\\\"Ruijie Supplicant\"\\RuijieSupplicant.exe");// 路径用\\,双引号为转义字符,前面要加\
// 写入日志文件
char buffer[32];
memset(buffer, 0, sizeof(buffer));
if (count < 6)
sprintf(buffer, "====> %s%d%s\n", "6次ping测试成功了", 6 - count, "次,有网");
else
sprintf(buffer, "====> %s\n", "6次ping测试全部都没有ping通过,没有网#######################################################################################################");
char s[] = "log.txt";
write_log_file(s, FILE_MAX_SIZE, buffer, strlen(buffer));
}
}
}
/*
计时函数,没有用到
*/
void g()
{
clock_t start, end;
start = clock();
_sleep(1234);
end = clock();
double duration = ((double)end - start) / CLOCKS_PER_SEC;
printf("%f second", duration);
}
/*
获取小时函数,这里只是测试,没用到
*/
void time()
{
// now为1970 到目前经过秒数,ltm->tm_hour为现在的小时数
time_t now = time(0);
tm *ltm = localtime(&now);
cout << ltm->tm_hour << "蒋凯楠";
}
int main()
{
p();
return 0;
}