从创建它的同一程序中杀死与winlogon具有相同安全属性的进程 [英] Kill a process that has the same security attributes than winlogon from the same program that has created it

查看:56
本文介绍了从创建它的同一程序中杀死与winlogon具有相同安全属性的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



介绍

我正在尝试安装启动钩子的服务在用户登录并将其安装到用户会话之前。这是专用的机器电脑。



这是我第一次使用C#。



我使用过来自codeproject的这篇文章 [ ^ ]知道在登录之前如何在一个特定用户和用户会话中创建进程。所有这些都发生在服务的OnStart上。

我明白回答这个问题并不容易除非我犯了一个超级愚蠢的错误,因为这是我第一次看到C#...所以请看一下代码,如果你觉得它需要看一下之前提到的文章,看看我在这里粘贴的代码片段的最后一部分,你可以看到我是如何试图杀死创建的进程。



问题

我想要做的是使用OnStop事件处理程序来终止创建的进程,但看起来它不起作用(至少不使用我用过的小代码片段)。



我做了什么

Hello all,

INTRODUCTION
I'm trying to install a service that start a hook before the user logs in and install it into the user session. This is for a dedicated machine computer.

This is the first time I'm using C#.

I've used this article from codeproject[^] to know how to create a process as one specific user and in the user session before logging in. All this happens in the OnStart of the service.
I understand that answering this question is not easy unless I've made a ultra-stupid-terrible-mistake which is more than possible as it is the first time I see C#... So please, take a look at the code, if you feel like it take a look at the article referred previously and a look at the last part of the code snippet I've pasted here where you can see how I'm trying to kill the created process.

QUESTION
What I would like to do is to use the OnStop event handler to kill that created process, but it looks like it is not working (at least not using the small code fragment I've used).

WHAT I'VE TRIED

using System;
using System.ServiceProcess;
using System.Runtime.InteropServices;

namespace Toolkit
{
  public partial class LoaderService : ServiceBase
  {
        [DllImport("user32.dll")] // This has been added by me in order to be able to use SendMessage
        private static extern int SendMessage(IntPtr hwnd, int Msg, int wParam, int lParam);// This has been added by me in order to be able to use SendMessage

        private int WM_CLOSE = 0x0010;// This has been added by me in order to be able to use the WM_CLOSE message

    public LoaderService()
    {
        InitializeComponent();
    }

// HERE IS WHERE I DO PROCESS THE START OF THE SERVICE (ORIGINAL CODE)
    protected override void OnStart(string[] args)
    {
        String applicationName = "\"c:\\MyProg.exe\"";
        ApplicationLoader.PROCESS_INFORMATION procInfo;
        ApplicationLoader.StartProcessAndBypassUAC(applicationName, out procInfo);  // HERE THE SERVICE LAUNCHES A PROGRAM COPYING THE WINLOGON SAFETY DETAILS/STATE.
    }

// HERE IS WHERE I DO WANT TO KILL THE PREVIOUSLY CREATED APPLICATION...
    protected override void OnStop()
    {
            System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("MyProg.exe");
            foreach (System.Diagnostics.Process p in process)
            {
                SendMessage(p.MainWindowHandle, WM_CLOSE, 0, 0);  // strategy 1 :: sendmessage to kill/close the application

                p.Kill();  // strategy 2 :: kill the process
            }
    }
  }
}







结束

我不知道如何调试它,因为它是一个服务并用C#编写。

你能帮助我一点吗?我做错了什么?

你会如何杀死这个过程?

另一种选择是继续从程序中读取服务本身的状态......因此我不需要杀死这个过程,我会开始尝试实现它,但是,即使知道我可以做到这一点,我想知道应该怎么做...



一如既往地感谢您的时间,帮助和耐心...可能这个是一个可怕的问题,因为我不能给你任何技术背景,因为这是我第一次用#事情......:叹息:




END
I don't know how to debug that as it is a service and written in C#.
Can you help me a little? what I'm doing wrong?
How would you kill that process?
Another option would be to keep reading for the state of the service itself from the program... and therefore I would not need to kill the process, I'll start trying to implement that, but, even knowing I can make this, I would like to know how this should be done...

As always thank you in advance for your time, help and patience... probably this one is a terrible question as I can't give you any technical background as it is my first time with that # thing... :sigh:

推荐答案

在这一行:

In this line:
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("MyProg.exe");



您使用 MyProg.exe 作为进程名称,但这不是进程名称。请尝试 MyProg

此外,您不需要使用 SendMessage 。您可以使用 p.CloseMainWindow()



实际上,没有必要同时使用两者CloseMainWindow和Kill。你应该只在CloseMainWindow没有成功时使用Kill:


You use MyProg.exe as process name, but that's not the process name. Try MyProg instead.
Also, you don't need to use SendMessage. You can use p.CloseMainWindow() instead.

And actually, it isn't necessary to use both CloseMainWindow and Kill. You should only use Kill when CloseMainWindow didn't succeed:

bool mainWindowClosed = p.CloseMainWindow();
p.WaitForExit(10000); // wait max. 10 seconds for the process to exit
if (!mainWindowClosed || !p.HasExited)
{
    p.Kill();
}
p.Dispose(); // free resources associated with the Process



上面的代码试图关闭主窗口。然后,它等待最大。该进程退出10秒。然后,如果 mainWindowClosed 为false,或者尽管CloseMainWindow没有退出进程,它将终止进程。最后,它释放了与流程相关的资源。

更多信息:

Process.CloseMainWindow [ ^ ]

Process.WaitForExit [ ^ ]

流程。杀死 [ ^ ]

Component.Dispose [ ^ ]


The above piece of code tries to close the main window. Then, it waits max. 10 seconds for the process to exit. Then, if mainWindowClosed is false or if the process hasn't exited despite CloseMainWindow, it kills the process. At the end, it frees resources associated with the process.
More information about:
Process.CloseMainWindow[^]
Process.WaitForExit[^]
Process.Kill[^]
Component.Dispose[^]


这篇关于从创建它的同一程序中杀死与winlogon具有相同安全属性的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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