By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,362 Members | 3,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,362 IT Pros & Developers. It's quick & easy.

Win32Exception while getting a process handle?!

P: n/a
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().Cont ains("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
Dec 11 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Tim
It should be line "07" and not line "XX" ;)
Dec 11 '07 #2

P: n/a
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).

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Tim" wrote:
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().Cont ains("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
Dec 12 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.