Win32Exception while getting a process handle?!

T

Tim

Hey guys,
I tried to get a process handle with the following code snippet:

00 Process[] processes = Process.GetProcesses();
01 if (null != processes && processes.Length > 0)
02 {
03 foreach (Process currentProcess in processes)
04 {
05 if (null != currentProcess)
06 {
07 if (currentProcess.Handle !=
Process.GetCurrentProcess().Handle)
08 {
09 if
(currentProcess.MainModule.FileName.ToLower().Contains("processname"))
10 {
11 currentProcess.Kill();
12 }
13 }
14 }
15 }
16 }

As soon as I reach line XX the framework will throw a Win32Exception
with the exception message "Access denied". The application should
have all needed user privileges because it runs as a service
application with the SYSTEM account permissions. As of this, I think
that my application will need additional .NET rights. Has somebody any
idea how to get the required permission? Thanks a lot.

Best regards,
Tim
 
P

Peter Ritchie [C# MVP]

What is Process.ProcessName for the process you are trying to retrieve the
handle for?

If it's a system process, you'll be unlikely to have been granted all access
to open the process. When Process gets the handle it tries to open the
process with PROCESS_ALL_ACCESS, if you've been denied any process permission
for that particular process you will get an access denied error (via
Win32Exception).
 

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