|
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 |
#include <QCoreApplication>#include <QObject>#include <QTimer>#include <QString>#include <iostream>#include <windows.h>#include <tlhelp32.h>QString fullname;void
check_and_restart(){ PROCESSENTRY32 pe32; pe32.dwSize = sizeof(pe32); HANDLE
snap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if
(snap == INVALID_HANDLE_VALUE){ std::cout << "CreateToolhelp32Snapshot failed."
<< std::endl; return; } BOOL
more = ::Process32First(snap,&pe32); bool
exists = false; QString name = fullname.mid(fullname.lastIndexOf(‘\\‘)); while
(more){ if
(name.compare(QString::fromWCharArray(pe32.szExeFile)) == 0){ exists = true; break; } more = ::Process32Next(snap,&pe32); } ::CloseHandle(snap); if
(!exists){ STARTUPINFO si; PROCESS_INFORMATION pi; si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = TRUE; if
(CreateProcess(fullname.toStdWString().c_str(),NULL,NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi)){ CloseHandle(pi.hThread); CloseHandle(pi.hProcess); } }}int
main(int
argc, char
*argv[]){ QCoreApplication a(argc, argv); if
(argc < 2){ std::cout << "wrong arguments."
<< std::endl; } fullname = QString(argv[1]); QTimer timer; QObject::connect(&timer, &QTimer::timeout, check_and_restart); timer.start(1000); return
a.exec();} |
原文:http://www.cnblogs.com/leon-/p/3553381.html