在C#中在OS级别启动应用程序的通知 [英] notification of the start of an application at the OS level in c#

查看:59
本文介绍了在C#中在OS级别启动应用程序的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够通知应用程序启动,并能够在启动后几毫秒内终止该进程,或者如果已经运行则终止该进程.

我需要从Windows窗体C#应用程序执行此操作.我该怎么办?

示例代码表示赞赏.我全亏了.

示例:


1.操作系统正在运行.
2.我启动我的表格应用程序(以某种方式监视系统上其他地方的独立应用程序的启动)一旦该过程启动,就需要将其终止. (在计时器上执行此操作是不可行的,因为可以让进程在终止之前运行一段时间)
4.我的一个应用程序收到另一应用程序启动的通知,应采取措施将其终止.

I want to be able to notified of the start of an application and be able to kill that process within milliseconds of it''s start, or terminate it if it''s already running.

I need to do this from a windows forms c# app. How would I go about this?

Sample code appreciated. I''m at a total loss.

Example:


1. OS is running.
2. I start my forms app ( which somehow moniters for the start of a seperate application somewhere else on the system ) Once that process starts, it needs to be killed. ( doing this on a timer is not feasible, since process could be allowed to run for some time before being killed )
4. one my app is notified of the start of the other application, it should take steps to kill it.


我该怎么做?

Windows消息?钩子? IPC?

How can I do this?

Windows messages? Hooks? IPC?

推荐答案

我将连接到PowerShell.它可以获取正在运行的应用程序,然后返回到C#.可以看到以下有关与PowerShell进行接口连接的信息:如何从C#运行PowerShell脚本 [ ^ ]
I would interface to PowerShell. It can get running applications, and return to C#. Can see the following about interfacing to PowerShell: How to run PowerShell scripts from C#[^]


整个想法意味着您在做坏事.不,我不是说您正在编写恶意软件.如果这是您的应用程序,则可以随时对其进行编程以使其正确终止;如果您只是想通过某个外部进程同步杀死它,那将是非常不安全的,不利于支持等.

无论如何,您可以使用WMI处理流程创建事件.请参阅此CodeProject文章:
使用WMI处理信息和通知 [
好吧,好多了?是的,没有.至少这样做,您不会立即犯罪.但是,通过同一进程在同一台机器上控制某个进程的生命周期的整个想法……我不知道您要牢记的体系结构,但是我会完全重新考虑它.不要做恶.

更重要的是,您提出问题的方法也是错误的.您只是问如何实现某种技术效果,无理地假设您想做的是正确的.您应该从解释您的最终目标开始.没有它,进一步的细节将是没有意义的.可能的解决方案将取决于这些目标.

—SA
The whole idea means you are doing something bad. No, I don''t mean that you are writing malicious software. If this is your application, you can always program it to terminate properly; and if your want to just kill it synchronously by some external process, this is extremely unsafe, bad for support, etc.

Anyway, you can handle the process creation events with WMI. Please see this CodeProject article:
Process Information and Notifications using WMI[^].

Does it worth it? No, by the reasons I explained above. I don''t think it even worth discussing. Again, it its your application, even it you want to cause its termination by external process, you can always program it to communicate with such process and synchronize the termination with its internal state. So, the communication means IPC, and the best way to organize such IPC would be some remoting, either "classical" .NET remoting or self-hosted WCF. As the problem is not tricky at all, it can be one or another.

Well, is it much better? Yes and no. At least doing so you would not commit a crime you are about to commit right now. But the whole idea of controlling the lifetime of one process by some other process… on the same machine… I have no idea of the architecture you keep in mind, but I would totally rethink it. Don''t do evil.

More importantly, your approach to asking questions is also wrong. You just ask how to implement some technical effect, unreasonably assuming that what you want to do is right. You should start the question with explanation of your ultimate goal. Without it, going in further detail just would not make sense; the possible solutions would depend on those goals.

—SA


Sergey,

我认为您有点误解了我的意图.我想做的就是能够监视并主动终止某个进程,即使该进程试图在系统上启动也是如此.没有任何恶意意图:-)我正在开发一个反间谍软件.
Sergey,

I think you are misunderstanding my intent a little bit. What I want to do is to be able to monitor for and actively terminate a process if it even tries to start on the system. No malicious intent involved by any means :-) Just more an anti-spyware app I''m developing.


这篇关于在C#中在OS级别启动应用程序的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆