How to stop a process

S

Steve Ricketts

I'm new to C# and converting a lot of VB6 programs to it. I have one sub
that stops a process and I'm having some difficulty getting it to build in
C#. The VB6 code looks like:

Public Sub stopProcess(ByVal hWindow As Long)
Call PostMessage(hWindow, WM_CLOSE, vbNull, vbNull)
Call WaitForSingleObject(hWindow, INFINITE)
End Sub

I've included the DllImports for PostMessage and WaitForSingleObject but
"hWinow" is a different type. PostMessages uses HandleRef for hWindow and
WaitForSingleObject uses IntPtr. How do I resolve the two so I can stop the
desired process?

Thanks,

sr
 
P

Peter Duniho

I'm new to C# and converting a lot of VB6 programs to it. I have one
sub that stops a process and I'm having some difficulty getting it to
build in C#. The VB6 code looks like:

Public Sub stopProcess(ByVal hWindow As Long)
Call PostMessage(hWindow, WM_CLOSE, vbNull, vbNull)
Call WaitForSingleObject(hWindow, INFINITE)
End Sub

I've included the DllImports for PostMessage and WaitForSingleObject but
"hWinow" is a different type. PostMessages uses HandleRef for hWindow
and WaitForSingleObject uses IntPtr. How do I resolve the two so I can
stop the desired process?

What type are you using for "HandleRef"? The
System.Runtime.InteropServices.HandleRef struct? If so, you can just use
the HandleRef.Handle property to get the IntPtr version of the handle.

That said, you would probably be better off using the .NET-pure approach
by getting a Process object associated with the process you want to close
(if you don't already have it), and calling Process.CloseMainWindow() on
that instance. It should accomplish the same thing, but without the
hassle of p/invoke.

Pete
 
S

Steve Ricketts

The CloseMainWindow approach sounds like a good idea. Problem for me is,
how do I get the Process object to represent the desired process. This
function is passed the window's handle. In looking at the GetProcesses
methods, I don't see where I can use the handle, just process id and name.
Looking around I found the following. Is this the correct approach?

GetWindowThreadProcessId(hWindow, out processId);
if (processId !=0) {
proc = System.Diagnostics.Process.GetProcessById(processId);
proc.CloseMainWindow();
}

sr
 
F

Family Tree Mike

Steve Ricketts said:
The CloseMainWindow approach sounds like a good idea. Problem for me is,
how do I get the Process object to represent the desired process. This
function is passed the window's handle. In looking at the GetProcesses
methods, I don't see where I can use the handle, just process id and name.
Looking around I found the following. Is this the correct approach?

GetWindowThreadProcessId(hWindow, out processId);
if (processId !=0) {
proc = System.Diagnostics.Process.GetProcessById(processId);
proc.CloseMainWindow();
}

sr

You would need to use Process.GetProcesses() to get all processes, and
compare the process.MainWindowHandle property to the one you are looking for
in a loop.

// something like this...
foreach (Process p in Process.GetProcesses())
{
if (p.MainWindowHandle == hWindow) p.Kill();
}

Mike
 
P

Peter Duniho

The CloseMainWindow approach sounds like a good idea. Problem for me
is, how do I get the Process object to represent the desired process.
This function is passed the window's handle. In looking at the
GetProcesses methods, I don't see where I can use the handle, just
process id and name. Looking around I found the following. Is this the
correct approach?

GetWindowThreadProcessId(hWindow, out processId);
if (processId !=0) {
proc = System.Diagnostics.Process.GetProcessById(processId);
proc.CloseMainWindow();
}

Without knowing more about where you get the window handle in the first
place, I'd say the above should be fine. But look at Mike's alternative
as well. His version is entirely managed, rather than relying on yet
another p/invoked unmanaged function call. (Of course, you wouldn't call
the Kill() method as he shows, but instead the CloseMainWindow() method I
mentioned before.

It's entirely possible that somewhere earlier in your program, you might
have gotten a process ID or the Process itself rather than the window
handle, but if you start with the requirement that you have to do this
based only on the window handle, then of course you'll have to map from
the window handle to the Process instance. A loop on all the system
processes or a call to the GetWindowThreadProcessId() function would
accomplish that.

Pete
 
S

Steve Ricketts

Great information from both Pete and Mike. I'll work both approaches into
some common functions. Thanks for taking the time to clarify this.

sr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top