-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
123 lines (106 loc) · 3.28 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*********************************************************************************
*Copyright(C)
*ClassName: main
*Author: 璞光
*Version: 1.0
*Date: 2019.7.12
*Description: main启动函数
*Others: 命令行不同指令启动,测试没反应,还没找到原因
*Function List: appAutoRun(bool bAutoRun) //写入注册表自启
* bool checkOnly(); //判断是否重复启动,返回false重复启动
* void IpConfig(); //设置本机IP(与网口相机匹配)
*History: //修改历史记录列表,每条修改记录应包含修改日期、修改者及修改内容简介
**********************************************************************************/
#include "maindialog.h"
#include <QFile>
#include <QApplication>
#include <windows.h>
#include <QProcess>
void appAutoRun(bool bAutoRun);
bool checkOnly();
void IpConfig();
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//检测程序重复启动
if(checkOnly()==false)
{return 0;}
//设置本机IP(连相机)
IpConfig();
//命令行启动方式 是否加自启
if(argc == 1)
{
appAutoRun(true);
printf("Auto Run ! ");
}
//else if (argv[1] == "d")
else
{
appAutoRun(false);
printf("delete successful ! ");
}
Q_INIT_RESOURCE(style1);
QFile qss("style.qss");
qss.open(QFile::ReadOnly);
qApp->setStyleSheet(qss.readAll());
qss.close();
MainDialog w;
w.show();
return a.exec();
}
//开机自启
void appAutoRun(bool bAutoRun)
{
QString appName = QApplication::applicationName();//程序名称
QString appPath = QApplication::applicationFilePath();// 程序路径
appPath = appPath.replace("/","\\");
//HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
QSettings *reg=new QSettings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",QSettings::NativeFormat);
if (bAutoRun)
{
QString val = reg->value(appName).toString();// 如果此键不存在,则返回的是空字符串
if(val != appPath)
reg->setValue(appName,appPath);
}
else
{
reg->remove(appName);
}
reg->deleteLater();
}
//防止程序重复启动
bool checkOnly()
{
// 创建互斥量
HANDLE m_hMutex = CreateMutex(NULL, FALSE, L"fortest_abc123" );
// 检查错误代码
if (GetLastError() == ERROR_ALREADY_EXISTS) {
// 如果已有互斥量存在则释放句柄并复位互斥量
CloseHandle(m_hMutex);
m_hMutex = NULL;
// 程序退出
return false;
}
else
return true;
}
void IpConfig()
{
QProcess *process = new QProcess();
//网卡名称
QString name = "\"以太网\" ";
QString cmd = "netsh interface ipv4 set address name = " + name + "source = static address = 218.192.162.1 ";
qDebug()<<"cmd = "<<cmd;
process->start(cmd); //执行dos命令
process->waitForFinished(); //等待执行完成
delete process;
}
//自动关机
//void AutoClose()
//{
// QProcess pro; //通过QProcess类来执行第三方程序
// QString cmd = QString("shutdown -s -t 0"); //shutdown -s -t 0 是window下的关机命令,
// pro.start(cmd); //执行命令cmd
// pro.waitForStarted();
// pro.waitForFinished();
//}