Using SetSecurityInfo for TerminateProcess

I am trying to make a program that detects a key press like alt+f4 and terminates the current process(GetForegroundWindow()). The reason for making the program is because sometimes larger programs 'don't respond' when I alt+tab out of them. When they become unresponsive my alt+f4 doesnt seem to work and I'm unable to access task manager. This leads me to use ctrl+alt+del, sign out, and log back in. This is a very long process.

I haven't set up any any of key detection yet. The program waits 3 seconds so I can pull up another process to test on and terminate. So the only problem here (I think) is that I'm unable/don't know how to give permision to the process allowing me to use 'TerminateProcess()'.

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
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string>
#include <Psapi.h>
#include <Aclapi.h>
#include <AccCtrl.h>

#include "timer.h"

DWORD CurrentProcessId();
DWORD CurrentThreadId();

using namespace std;
using std::string;

int main(){
	timer time;
	HWND hwnd;

	time.timerSec(3);
	hwnd = GetForegroundWindow();
	


	if (TerminateProcess(hwnd, NULL) != 0) {
		cout << "Process Terminated!\n";
	}
	else {
		cout << "Process Termination Failed!\n";
	}

	system("pause");
	return 0;
}



DWORD CurrentProcessId() {
	DWORD pid;
	HWND hwnd;

	hwnd = GetForegroundWindow();
	GetWindowThreadProcessId(hwnd, &pid);
	return pid;
}

DWORD CurrentThreadId() {
	HWND hwnd;

	hwnd = GetForegroundWindow();
	return GetWindowThreadProcessId(hwnd, NULL);
}
TerminateProcess() expects a process handle as its first parameter, and you're passing a window handle. You need to obtain the process that owns the window returned by GetForegroundWindow(). As long as that process is running under the same user as the process that's trying to call TerminateProcess() you shouldn't need to do anything fancy with security to terminate it.
Last edited on
Topic archived. No new replies allowed.