Argument sifatida berilgan protsessni ishlab turgan protsesslar ichidan qidiradi, agar topilmasa, uni ishga tushiradi.

 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
// processList.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include <iostream>
using namespace std;
#include "tchar.h"
#include "tlhelp32.h"
BOOL checkProcess(TCHAR* processName)
{
HANDLE hSnapshot = NULL;
PROCESSENTRY32 processEntry;
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot != NULL)
{
processEntry.dwSize = sizeof(processEntry);
if (Process32First(hSnapshot, &processEntry))
{
do
{
if (_tcscmp(processName, processEntry.szExeFile) == 0)
{
CloseHandle(hSnapshot);
return TRUE;
}
processEntry.dwSize = sizeof(processEntry);
}
while (Process32Next(hSnapshot, &processEntry));
}
else
{
cout << "Cannot getting first process." << endl;
}
}
else
{
cout << "hSnapshot is NULL." << endl;
}
CloseHandle(hSnapshot);
return FALSE;
}
int _tmain(int argc, _TCHAR* argv[])
{
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInformation;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
ZeroMemory(&processInformation, sizeof(processInformation));
if (argc < 3)
{
_tprintf(TEXT("Usage: %s <process name> <full path to programm>\nFor example, %s cmd.exe \"C:\\Windows\\system32\\cmd.exe\""), argv[0], argv[0]);
return 0;
}
_tprintf(TEXT("%s\n"), argv[2]);
while (TRUE)
{
Sleep(5000);
if (checkProcess(argv[1]))
{
cout << "Process is running..." << endl;
}
else
{
_tprintf(TEXT("Process is not running...\n Creating process %s\n"), argv[2]);
if(!CreateProcess(argv[2],
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&startupInfo,
&processInformation))
{
cout << "Creating process failed." << endl;
}
else
{
cout << "Process successfully created." << endl;
}
}
}
}