Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
C#
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
            }
    }
  }
}



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:
Posted

1 solution

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

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:
C#
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

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[^]
 
Share this answer
 
v2
Comments
Joan M 6-Feb-15 15:26pm    
Wonderful!
Thank you!
both recommendations worked like charm!
Thomas Daniels 6-Feb-15 15:28pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900