GetProcessesByName and Permissions

S

Steve Long

Hello,
I'm using the Process class to get all the current running processes on my
machine and looking for a particular process. If I find the process, I want
to kill it. However, I've recently started recieving and exception "Error
Access is denied" when I try to kill this process. However, I can open up
Task Manager and kill the process. I'm running my program under my user name
so I don't get why I can kill it in Task Manager but my program can't kill
the process. Below is the code I'm utilizing:

for(;;)
{
proc = Process.GetProcessesByName(CProcWatch.PROCNAME);
try
{
foreach (Process pr in proc)
{
if (String.Compare(pr.ProcessName, CProcWatch.PROCNAME, true) ==
0)
pr.Kill();
}
}
catch(Exception e)
{
errlog.Log("CProcWatch", "Error " + e.Message + " occured at: " +
System.DateTime.Now.TimeOfDay.ToString());
}
Thread.Sleep(10000);
proc = null;
}

Can anybody tell me why I should be getting the Access denied Exception?
Any help would be much appreciate. Thanks

Steve
 
W

Willy Denoyette [MVP]

You can't kill a process owned by another account unless you explicitly
enable the "SeDebugPrivilege" privilege for the calling process (that's what
Taskman does).
Note that caller needs the "Debug programs" account policy enabled (see
local policy - user right assignment).
You can use the System.Management and WMI's Win32_Process class to terminate
a process.
Or you have to enable the SeDebugPrivilege by calling Win32 API's
LookupPrivilegeValue and AdjustTokenPrivileges using PInvoke.
Willy.
 
S

Steve Long

I'm working on it. Thanks for your reply and I will post again once I can
get it working or if I have another question.

Steve
 
S

Steve Long

Willy, thanks much. I believe I have solved this little problem using
PInvoke with AdjustTokenPrivileges. At this point, I can adjust the token
privileges but I have to wait until that process runs again to see if that
cures the problem.
Thanks again.

Steve
 

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