Skip to content

Commit cf86f7d

Browse files
authored
Update SetProcess.cpp
1 parent da1aa0d commit cf86f7d

File tree

1 file changed

+58
-57
lines changed

1 file changed

+58
-57
lines changed

SetProcess/SetProcess.cpp

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include "SetProcess.h"
22

3-
// Definir una estructura para asociar opciones con funciones
3+
44
typedef struct {
55
const char* option;
66
void (*action)(int argc, char* argv[]);
77
} OptionAction;
88

9-
// Funciones para cada acción
9+
1010
void help_action(int argc, char* argv[]) {
1111
printf("use: sp.exe <Argument> <ImageName or PID> <Argument>\n");
1212
printf("\n -c <IM/PID> <Cores> <-r> | CpuSets");
@@ -24,7 +24,7 @@ void help_action(int argc, char* argv[]) {
2424

2525
void cpu_set_action(int argc, char* argv[]) {
2626

27-
// Obtener PID, Obtener Privilegio
27+
2828
DWORD dwProcessId = GetPID(argv[2]);
2929
if (dwProcessId == 0)
3030
{
@@ -36,11 +36,11 @@ void cpu_set_action(int argc, char* argv[]) {
3636
DWORD IdCount;
3737
ULONG* Ids = ConvertToCpuSetIds(argv[3], &IdCount);
3838

39-
// Establecer Conjuntos de CPU
39+
4040
if (SetProcessCpuSetID(dwProcessId, Ids, IdCount)){
41-
// Establecerlo en todos los procesos hijos
41+
4242
if (argc == 5 && strcmp(argv[4], "-r") == 0){
43-
DWORD dwChildProcessId[64] = {0}; // Inicializar el array a 0
43+
DWORD dwChildProcessId[64] = {0};
4444
DWORD NumProcesses = GetChildProcesses(dwProcessId, dwChildProcessId);
4545

4646
for (DWORD i = 0; i < NumProcesses; i++) {
@@ -55,26 +55,26 @@ void cpu_set_action(int argc, char* argv[]) {
5555

5656
void ideal_processor_action(int argc, char* argv[]) {
5757

58-
// Obtener PID, Obtener Privilegio
58+
5959
DWORD dwProcessId = GetPID(argv[2]);
6060
if (dwProcessId == 0)
6161
{
6262
dwProcessId = atoi(argv[2]);
6363
}
6464
EnablePrivilege(dwProcessId, SE_DEBUG_NAME);
6565

66-
// Numero de procesadores ideales
66+
6767
DWORD dwIdealProcessor = atoi(argv[3]);
6868

69-
// Establecer procesador ideal
69+
7070
SetIdealProcessor(dwProcessId, dwIdealProcessor);
71-
// Establecerlo en todos los procesos hijos
71+
7272
if (argc == 5 && strcmp(argv[4], "-r") == 0){
73-
DWORD dwChildProcessId[64] = {0}; // Inicializar el array a 0
73+
DWORD dwChildProcessId[64] = {0};
7474
DWORD NumProcesses = GetChildProcesses(dwProcessId, dwChildProcessId);
7575

7676
for (DWORD i = 0; i < NumProcesses; i++) {
77-
// Establecer procesador ideal
77+
7878
SetIdealProcessor(dwChildProcessId[i], dwIdealProcessor);
7979
}
8080

@@ -84,7 +84,7 @@ void ideal_processor_action(int argc, char* argv[]) {
8484

8585
void affinity_action(int argc, char* argv[]) {
8686

87-
// Obtener PID, Obtener Privilegio
87+
8888
DWORD dwProcessId = GetPID(argv[2]);
8989
if (dwProcessId == 0)
9090
{
@@ -94,14 +94,14 @@ void affinity_action(int argc, char* argv[]) {
9494

9595
DWORD dwProcessAffinityMask = ConvertToBitMask(argv[3]);
9696

97-
// Obtener HANDLE
97+
9898
HANDLE hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId);
9999

100-
// Establecer Afinidad
100+
101101
if (SetProcessAffinityMask(hProcess, dwProcessAffinityMask)){
102-
// Establecerlo en todos los procesos hijos
102+
103103
if (argc == 5 && strcmp(argv[4], "-r") == 0){
104-
DWORD dwChildProcessId[64] = {0}; // Inicializar el array a 0
104+
DWORD dwChildProcessId[64] = {0};
105105
DWORD NumProcesses = GetChildProcesses(dwProcessId, dwChildProcessId);
106106

107107
for (DWORD i = 0; i < NumProcesses; i++) {
@@ -113,26 +113,26 @@ void affinity_action(int argc, char* argv[]) {
113113
}
114114
}
115115

116-
// Cerrar Handle
116+
117117
CloseHandle(hProcess);
118118

119119
return;
120120
}
121121

122122
void cpu_priority_action(int argc, char* argv[]) {
123123

124-
// Obtener PID, Obtener Privilegio
124+
125125
DWORD dwProcessId = GetPID(argv[2]);
126126
if (dwProcessId == 0)
127127
{
128128
dwProcessId = atoi(argv[2]);
129129
}
130130
EnablePrivilege(dwProcessId, SE_DEBUG_NAME);
131131

132-
// Obtener HANDLE
132+
133133
HANDLE hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId);
134134

135-
// Convertir char a dword
135+
136136
DWORD dwPriorityClass;
137137
switch (atoi(argv[3]))
138138
{
@@ -175,10 +175,10 @@ void cpu_priority_action(int argc, char* argv[]) {
175175

176176
}
177177

178-
// Establecer prioridad
178+
179179
if (SetPriorityClass(hProcess, dwPriorityClass)){
180180

181-
// Establecerlo en todos los procesos hijos
181+
182182
if (argc == 5 && strcmp(argv[4], "-r") == 0){
183183
DWORD dwChildProcessId[64] = {0};
184184
DWORD NumProcesses = GetChildProcesses(dwProcessId, dwChildProcessId);
@@ -192,15 +192,15 @@ void cpu_priority_action(int argc, char* argv[]) {
192192
}
193193
}
194194

195-
// Cerrar Handle
195+
196196
CloseHandle(hProcess);
197197

198198
return;
199199
}
200200

201201
void cpu_priority_boost_action(int argc, char* argv[]) {
202202

203-
// Obtener PID, Obtener Privilegio
203+
204204
DWORD dwProcessId = GetPID(argv[2]);
205205
if (dwProcessId == 0)
206206
{
@@ -209,11 +209,11 @@ void cpu_priority_boost_action(int argc, char* argv[]) {
209209
EnablePrivilege(dwProcessId, SE_DEBUG_NAME);
210210
EnablePrivilege(dwProcessId, SE_INC_BASE_PRIORITY_NAME);
211211

212-
// Obtener HANDLE
212+
213213
HANDLE hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId);
214214

215215
if (SetProcessPriorityBoost(hProcess, FALSE)){
216-
// Establecerlo en todos los procesos hijos
216+
217217
if (argc == 4 && strcmp(argv[3], "-r") == 0){
218218
DWORD dwChildProcessId[64] = {0};
219219
DWORD NumProcesses = GetChildProcesses(dwProcessId, dwChildProcessId);
@@ -233,24 +233,24 @@ void cpu_priority_boost_action(int argc, char* argv[]) {
233233

234234
void memory_priority_action(int argc, char* argv[]) {
235235

236-
// Obtener PID, Obtener Privilegio
236+
237237
DWORD dwProcessId = GetPID(argv[2]);
238238
if (dwProcessId == 0)
239239
{
240240
dwProcessId = atoi(argv[2]);
241241
}
242242
EnablePrivilege(dwProcessId, SE_DEBUG_NAME);
243243

244-
// Obtener HANDLE
244+
245245
HANDLE hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId);
246246

247-
// Establecer prioridad de memoria
247+
248248
MEMORY_PRIORITY_INFORMATION MemPrio;
249249
SecureZeroMemory(&MemPrio, sizeof(MemPrio));
250250
MemPrio.MemoryPriority = atoi(argv[3]);
251251

252252
if (SetProcessInformation(hProcess, ProcessMemoryPriority, &MemPrio, sizeof(MemPrio))){
253-
// Establecerlo en todos los procesos hijos
253+
254254
if (argc == 5 && strcmp(argv[4], "-r") == 0){
255255
DWORD dwChildProcessId[64] = {0};
256256
DWORD NumProcesses = GetChildProcesses(dwProcessId, dwChildProcessId);
@@ -271,24 +271,24 @@ void memory_priority_action(int argc, char* argv[]) {
271271

272272
void io_priority_action(int argc, char* argv[]) {
273273

274-
// Obtener PID, Obtener Privilegio
274+
275275
DWORD dwProcessId = GetPID(argv[2]);
276276
if (dwProcessId == 0)
277277
{
278278
dwProcessId = atoi(argv[2]);
279279
}
280280
EnablePrivilege(dwProcessId, SE_DEBUG_NAME);
281281

282-
// Obtener HANDLE
282+
283283
HANDLE hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId);
284284

285-
// Establecer prioridad E/S
285+
286286
IO_PRIORITY_INFORMATION IoPrio;
287287
SecureZeroMemory(&IoPrio, sizeof(IoPrio));
288288
IoPrio.IoPriority = atoi(argv[3]);
289289

290290
NtSetInformationProcess(hProcess, ProcessIoPriority, &IoPrio, sizeof(IoPrio));
291-
// Establecerlo en todos los procesos hijos
291+
292292
if (argc == 5 && strcmp(argv[4], "-r") == 0){
293293
DWORD dwChildProcessId[64] = {0};
294294
DWORD NumProcesses = GetChildProcesses(dwProcessId, dwChildProcessId);
@@ -308,18 +308,18 @@ void io_priority_action(int argc, char* argv[]) {
308308

309309
void eco_qos_action(int argc, char* argv[]) {
310310

311-
// Obtener PID, Obtener Privilegio
311+
312312
DWORD dwProcessId = GetPID(argv[2]);
313313
if (dwProcessId == 0)
314314
{
315315
dwProcessId = atoi(argv[2]);
316316
}
317317
EnablePrivilege(dwProcessId, SE_DEBUG_NAME);
318318

319-
// Obtener HANDLE
319+
320320
HANDLE hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId);
321321

322-
// Establecer estado de eficiencia (EcoQos)
322+
323323
PROCESS_POWER_THROTTLING_STATE PowerThrottling;
324324

325325
SecureZeroMemory(&PowerThrottling, sizeof(PowerThrottling));
@@ -328,7 +328,7 @@ void eco_qos_action(int argc, char* argv[]) {
328328
PowerThrottling.StateMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
329329

330330
if (SetProcessInformation(hProcess, ProcessPowerThrottling, &PowerThrottling, sizeof(PowerThrottling))){
331-
// Establecerlo en todos los procesos hijos
331+
332332
if (argc == 4 && strcmp(argv[3], "-r") == 0){
333333
DWORD dwChildProcessId[64] = {0};
334334
DWORD NumProcesses = GetChildProcesses(dwProcessId, dwChildProcessId);
@@ -347,18 +347,18 @@ void eco_qos_action(int argc, char* argv[]) {
347347

348348
void high_qos_action(int argc, char* argv[]) {
349349

350-
// Obtener PID, Obtener Privilegio
350+
351351
DWORD dwProcessId = GetPID(argv[2]);
352352
if (dwProcessId == 0)
353353
{
354354
dwProcessId = atoi(argv[2]);
355355
}
356356
EnablePrivilege(dwProcessId, SE_DEBUG_NAME);
357357

358-
// Obtener HANDLE
358+
359359
HANDLE hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId);
360360

361-
// Establecer estado de maximo rendimiento (HighQos)
361+
362362
PROCESS_POWER_THROTTLING_STATE PowerThrottling;
363363

364364
SecureZeroMemory(&PowerThrottling, sizeof(PowerThrottling));
@@ -367,7 +367,7 @@ void high_qos_action(int argc, char* argv[]) {
367367
PowerThrottling.StateMask = 0;
368368

369369
if (SetProcessInformation(hProcess, ProcessPowerThrottling, &PowerThrottling, sizeof(PowerThrottling))){
370-
// Establecerlo en todos los procesos hijos
370+
371371
if (argc == 4 && strcmp(argv[3], "-r") == 0){
372372
DWORD dwChildProcessId[64] = {0};
373373
DWORD NumProcesses = GetChildProcesses(dwProcessId, dwChildProcessId);
@@ -386,7 +386,7 @@ void high_qos_action(int argc, char* argv[]) {
386386

387387
void suspend_action(int argc, char* argv[]) {
388388

389-
// Obtener PID, Obtener Privilegio
389+
390390
DWORD dwProcessId = GetPID(argv[2]);
391391
if (dwProcessId == 0)
392392
{
@@ -396,13 +396,13 @@ void suspend_action(int argc, char* argv[]) {
396396

397397
HANDLE hProcess = OpenProcess(PROCESS_SUSPEND_RESUME, FALSE, dwProcessId);
398398

399-
// Obtener privilegio de depuracion (por si falla)
399+
400400
EnablePrivilege(dwProcessId, SE_DEBUG_NAME);
401401

402-
// Suspender
402+
403403
NtSuspendProcess(hProcess);
404404

405-
// Establecerlo en todos los procesos hijos
405+
406406
if (argc == 4 && strcmp(argv[3], "-r") == 0){
407407
DWORD dwChildProcessId[64] = {0};
408408
DWORD NumProcesses = GetChildProcesses(dwProcessId, dwChildProcessId);
@@ -421,7 +421,7 @@ void suspend_action(int argc, char* argv[]) {
421421

422422
void resume_action(int argc, char* argv[]) {
423423

424-
// Obtener PID, Obtener Privilegio
424+
425425
DWORD dwProcessId = GetPID(argv[2]);
426426
if (dwProcessId == 0)
427427
{
@@ -431,13 +431,13 @@ void resume_action(int argc, char* argv[]) {
431431

432432
HANDLE hProcess = OpenProcess(PROCESS_SUSPEND_RESUME, FALSE, dwProcessId);
433433

434-
// Obtener privilegio de depuracion (por si falla)
434+
435435
EnablePrivilege(dwProcessId, SE_DEBUG_NAME);
436436

437-
// Reanudar
437+
438438
NtResumeProcess(hProcess);
439439

440-
// Establecerlo en todos los procesos hijos
440+
441441
if (argc == 4 && strcmp(argv[3], "-r") == 0){
442442
DWORD dwChildProcessId[64] = {0};
443443
DWORD NumProcesses = GetChildProcesses(dwProcessId, dwChildProcessId);
@@ -454,7 +454,7 @@ void resume_action(int argc, char* argv[]) {
454454
return;
455455
}
456456

457-
// Definir las opciones y las funciones asociadas
457+
458458
OptionAction option_actions[] = {
459459
{"help", help_action},
460460
{"-c", cpu_set_action},
@@ -468,22 +468,22 @@ OptionAction option_actions[] = {
468468
{"-hq", high_qos_action},
469469
{"-s", suspend_action},
470470
{"-r", resume_action},
471-
{NULL, NULL} // ultimo elemento
471+
{NULL, NULL}
472472
};
473473

474-
//buscar parametro que coincida
474+
475475
int main(int argc, char* argv[]) {
476476

477477
if (argc < 2) {
478478
help_action(argc, argv);
479479
return 0;
480480
}
481481

482-
// obtener privilegio de depuracion para evitar el descriptor de seguridad de otros procesos
482+
483483
EnablePrivilege(GetCurrentProcessId(), SE_DEBUG_NAME);
484484
EnablePrivilege(GetCurrentProcessId(), SE_INC_BASE_PRIORITY_NAME);
485485

486-
// Hacerse pasar por el hilo del sistema
486+
487487
ImpersonateSystem();
488488

489489
for (int i = 0; option_actions[i].option != NULL; i++) {
@@ -492,7 +492,8 @@ int main(int argc, char* argv[]) {
492492
return 0;
493493
}
494494
}
495-
// Si la opción no coincide con ninguna acción conocida, mostrar ayuda
495+
496496
help_action(argc, argv);
497497
return 0;
498498
}
499+

0 commit comments

Comments
 (0)