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")]
private static extern int SendMessage(IntPtr hwnd, int Msg, int wParam, int lParam);
private int WM_CLOSE = 0x0010;
public LoaderService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
String applicationName = "\"c:\\MyProg.exe\"";
ApplicationLoader.PROCESS_INFORMATION procInfo;
ApplicationLoader.StartProcessAndBypassUAC(applicationName, out procInfo);
}
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);
p.Kill();
}
}
}
}
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: