How to create a new event or handle API functions when a user terminates a WPF process through the Task Manager

Reza Jaferi 331 Reputation points
2022-08-11T14:35:03.5+00:00

Hi,

Perhaps this question has been posed before, but I don't believe it has been properly answered.
I want to do something when a user closes my program using Task Manager.
It would be ideal if we could manage the necessary API functions. However, if no such function exists, we must create a function or event, even if we use C++, and use that event or function in the WPF project, in order to archive this question on learn.microsoft.com and ensure it is never asked again.

According to my knowledge, the CTRL_CLOSE_EVENT function is used to close Console Applications and the WM_CLOSE function is used to close Windows/GUI Applications. But, if I'm not mistaken, Windows 8.1 uses other functions to perform this action. Therefore, when you answer this question, assume that this application will run in all versions of Windows (I mean Windows 7 and after that).
If none of the API functions are suitable for answering this question, we should create an event or function.

Platforms that are allowed to be used in response to this question:

  • WPF

The answer must be compatible with one of the following .NET Framework versions (ideally .NET Framework 4.6.2):

  • .NET Framework 3.5 SP1
  • .NET Framework 4
  • .NET Framework 4.5
  • .NET Framework 4.5.1
  • .NET Framework 4.5.2
  • .NET Framework 4.6
  • .NET Framework 4.6.1
  • .NET Framework 4.6.2
  • .NET Framework 4.7
  • .NET Framework 4.7.1
  • .NET Framework 4.7.2
  • .NET Framework 4.8
  • .NET Framework 4.8.1

Please respond with a GIF result.

Best regards,

Reza Jaferi

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,422 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,527 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
765 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Mortius Maximus 6 Reputation points
    2022-08-11T15:38:20.653+00:00

    I am attempting something similar. This is how I'm doing it: I have a process running that is searching for cheats in a game I am hosting and will terminate the cheats when detected. Of course they can simply end task on that program. This is why I am attempting to make a service that is disguised with a generic sounding name that will relaunch the anti-cheat program as soon as it detects that it is missing. It also monitors the files in the game folder and will replace them back to their original if they are messed with. The only issue I'm having is getting the second program to run as a service as ClickOnce is preventing me from running the service as admin by not even letting me publish the file. Without the requestadministrator in the manifest it gives an "access denied".

    If you don't need the second program to be a service, you can simply have a second program that checks the processes and when it finds 0 of your process name it will relaunch it. You then have the first program do the same for the second one and if the timer ticks are low enough they won't have time to end task both of them. I was even thinking of adding a third program to check the other two...

    If only I could get my friends to just not cheat I could avoid all of this. Hope this helps in some way. I'm still quite the beginner... yet again.