-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleApplication1.cpp
More file actions
204 lines (163 loc) · 4.75 KB
/
ConsoleApplication1.cpp
File metadata and controls
204 lines (163 loc) · 4.75 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <string>
using namespace std;
void thread()
{
string song[64] = {
"\nDashing through the snow",
"In a one - horse open sleigh",
"O'er the fields we go",
"Laughing all the way",
"\n",
"\n",
"\n",
"\n",
"Oh!Jingle bells, jingle bells",
"Jingle all the way",
"Oh, what fun it is to ride",
"In a one - horse open sleigh, hey",
"\n",
"\n",
"\n",
"\n",
"A day or two ago",
"I thought I'd take a ride",
"And soon, Miss Fanny Bright",
"Was seated by my side",
"\n",
"\n",
"\n",
"\n",
"Hey, jingle bells, jingle bells",
"Jingle all the way",
"Oh, what fun it is to ride",
"In a one - horse open sleigh, hey",
"\n",
"\n",
"\n",
"\n",
"A day or two ago",
"The story I must tell",
"I went out on the snow",
"And on my back I fell",
"\n",
"\n",
"\n",
"\n",
"Jingle bells, jingle bells",
"Jingle all the way",
"Oh, what fun it is to ride",
"In a one - horse open sleigh, hey",
"\n",
"\n",
"\n",
"\n",
"Now the ground is white",
"Go it while you're young",
"Take the girls tonight",
"And sing this sleighing song",
"\n",
"\n",
"\n",
"\n",
"Jingle bells, jingle bells",
"Jingle all the way",
"Oh, what fun it is to ride",
"In a one - horse open sleigh, hey",
"\n",
"\n",
"\n",
"\n" };
for (int i = 0; i < 63; i += 4) {
cout << song[i] << endl;
cout << song[i + 1] << endl;
cout << song[i + 2] << endl;
cout << song[i + 3] << endl;
if (i + 4 > 63) i = 0;
Sleep(2123);
}
}
int main()
{
setlocale(LC_ALL, "ru");
HANDLE hProcess;
hProcess = GetCurrentProcess();
cout << "Parent process: " << endl << "The descriptor: " << hProcess << "\tPID: " << GetProcessId(hProcess) << endl;
char consoleapp2[180] = "C:\\Users\\NoI\\Desktop\\ConsoleApplication2\\x64\\Debug\\ConsoleApplication2.exe";
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = si.hStdOutput = si.hStdError = GetStdHandle(STD_OUTPUT_HANDLE);
if (!CreateProcess(NULL, consoleapp2, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
cout << "\nAn error occurred when starting a child process." << endl;
return 1;
}
string str;
getline(cin, str);
cout << "\nChild process 1: " << endl << "The descriptor: " << pi.hProcess << "\tPID: " << pi.dwProcessId << endl;
cout << "\nHello, " << str << endl;
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
char consoleapp3[180] = "C:\\Users\\NoI\\Desktop\\ConsoleApplication3\\x64\\Debug\\ConsoleApplication3.exe";
string strToSend;
cout << "\nPlease enter an integer, and it will turn into 4: ";
getline(cin, strToSend);
string commandLineArg = "\"" + strToSend + "\"";
STARTUPINFO si2;
PROCESS_INFORMATION pi2;
ZeroMemory(&si2, sizeof(si2));
si2.cb = sizeof(si2);
si2.dwFlags = STARTF_USESTDHANDLES;
si2.hStdInput = si2.hStdOutput = si2.hStdError = GetStdHandle(STD_OUTPUT_HANDLE);
string commandLine = string(consoleapp3) + " " + commandLineArg;
if (!CreateProcess(NULL, LPSTR(commandLine.c_str()), NULL, NULL, TRUE, 0, NULL, NULL, &si2, &pi2)) {
cout << "\nAn error occurred when starting a child process." << endl;
return 1;
}
cout << "\nChild process 2: " << endl << "The descriptor: " << pi2.hProcess << "\tPID: " << pi2.dwProcessId << endl;
WaitForSingleObject(pi2.hProcess, INFINITE);
CloseHandle(pi2.hProcess);
CloseHandle(pi2.hThread);
char consoleapp4[180] = "C:\\Users\\NoI\\Desktop\\ConsoleApplication4\\x64\\Debug\\ConsoleApplication4.exe ";
char lpszHandle[20];
STARTUPINFO si3;
PROCESS_INFORMATION pi3;
HANDLE hThread, hInheritThread;
DWORD IDThread;
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread, NULL,
0, &IDThread);
if (hThread == NULL)
return GetLastError();
if (!DuplicateHandle(GetCurrentProcess(), hThread, GetCurrentProcess(), &hInheritThread,0, TRUE, DUPLICATE_SAME_ACCESS))
{
_cputs("\nThe stream was not duplicated.");
_cputs("\nPress any button to complete.");
_getch();
return GetLastError();
}
ZeroMemory(&si3, sizeof(si3));
si3.cb = sizeof(si3);
si3.dwFlags = STARTF_USESTDHANDLES;
si3.hStdInput = si3.hStdOutput = si3.hStdError = GetStdHandle(STD_OUTPUT_HANDLE);
_itoa_s((int)hInheritThread, lpszHandle, 10);
strcat_s(consoleapp4, lpszHandle);
if (!CreateProcess(NULL, consoleapp4, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si3, &pi3))
{
_cputs("\nThe process was not created.");
_cputs("\nPress any button to complete.");
_getch();
return GetLastError();
}
cout << "\nChild process 3: " << endl << "The descriptor: " << pi3.hProcess << "\tPID: " << pi3.dwProcessId << endl << endl;
cout << "\nJingle Bells!" << endl;
_getch();
CloseHandle(pi3.hProcess);
CloseHandle(pi3.hThread);
CloseHandle(hThread);
return 0;
}