views:

1551

answers:

4

Hi! Here's my issue: I need to close a process, already running, from a C# program. The problem is that the process now runs as an icon (minimized to taskbar), and unless the user opens it at least once (which will never happen on unattended machines), it'll never have a main window.

The other requirement that I have is that the application be closed not killed. I need it to write it's memory buffers to disk - and killing it causes data loss.

Here's what I tried so far:

        foreach (Process proc in Process.GetProcesses())
        {
            if (proc.ProcessName.ToLower().StartsWith("myapp"))
            {
                if (proc.MainWindowHandle.ToInt32() != 0)
                {
                    proc.CloseMainWindow();
                    proc.Close();
                    //proc.Kill();  <--- not good!
                }
            }
        }

I've added the if clause, after discovering that MainWindowHandle == 0 when the window was minimized. Removig the if doesn't help. Neither the CloseMainWindow() nor the Close() work. The Kill() does, but as mentioned above - it's not what I need.

Any idea would be accepted, including the use of arcane Win32 API functions :)

A: 

If it's on the taskbar, it'll have a window. Or did you mean that it's in the taskbar notification area (aka the SysTray)? In which case, it'll still have a window.

Win32 applications don't really have a "main window", except by convention (the main window is the one that calls PostQuitMessage in response to WM_DESTROY, causing the message loop to exit).

With the program running, run Spy++. To find all of the windows owned by a process, you should select Spy -> Processes from the main menu. This will display a tree of processes. From there, you can drill down to threads, and then to windows. This will tell you which windows the process has. Note down the window class and caption. With these, you can use FindWindow (or EnumWindows) to find the window handle in future.

With the window handle, you can send a WM_CLOSE or WM_SYSCOMMAND/SC_CLOSE (equivalent to clicking on the 'X' on the window caption) message. This ought to cause the program to shut down nicely.

Note that I'm talking from a Win32 point-of-view here. You might need to use P/Invoke or other tricks to get this to work from a .NET program.

Roger Lipscombe
+2  A: 

This should work:

[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern IntPtr FindWindow(string className, string windowName);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

private const int WM_CLOSE = 0x10;
private const int WM_QUIT = 0x12;

public void SearchAndDestroy(string windowName) 
{
    IntPtr hWnd = FindWindow(null, windowName);
    if (hWnd == IntPtr.Zero)
        throw new Exception("Couldn't find window!");
    SendMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}

Since some windows don't respond to WM_CLOSE, WM_QUIT might have to be sent instead. These declarations should work on both 32bit and 64bit.

rpetrich
A: 

Question to clarify why you're attempting this: If the only user interface on the process is the system tray icon, why would you want to kill that and but leave the process running? How would the user access the process? And if the machine is "unattended", why concern yourself with the tray icon?

Craig Eddy
A: 

First, allow me to thank all of you who answered here, or even read this question. I believe Stack Overflow will become a great site for people like us. Here are some answers and clarifications:

rpetrich: Tried your method before and the problem is, I don't know the window name, it differs from user to user, version to version - just the exe name remains constant. All I have is the process name. And as you can see in the code above the MainWindowHandle of the process is 0.

Roger: Yes, I did mean the taskbar notification area - thanks for the clarification. I NEED to call PostQuitMessage. I just don't know how, given a processs only, and not a Window.

Craig: I'd be glad to explain the situation: the application has a command line interface, allowing you to specify parameters that dictate what it would do and where will it save the results. But once it's running, the only way to stop it and get the results is right-click it in the tray notification are and select 'exit'.

Now my users want to script/batch the app. They had absolutely no problem starting it from a batch (just specify the exe name and and a bunch of flags) but then got stuck with a running process. Assuming no one will change the process to provide an API to stop it while running (it's quite old), I need a way to artificially close it.

Similarly, on unattended computers, the script to start the process can be started by a task scheduling or operations control program, but there's no way to shut the process down.

Hope that clarifies my situation, and again, thanks everyone who's trying to help!

Traveling Tech Guy