|
hi all
I wanted a way to kill a spesific proccess for example winword.exe for a spesific user ( the user that runs the program)
there is more than one user logged on to the server so i cant have it kill all winword.exe's
any idea on how to do this?
|
|
|
|
|
You may have to P/Invoke OpenProcessToken (advapi32)[^]. Get the token information and check if the username and the process name are the one you want to kill. Get the handle and using a win32 method kill the application.
|
|
|
|
|
I was thinking more something like
For Each proc As Process In Process.GetProcessesByName("process name here") proc.CloseMainWindow() 'ask the process to exit.
proc.WaitForExit(10000) 'wait up to 10 seconds.
If Not proc.HasExited Then
proc.Kill() 'force the process to exit.
End If
Next proc
But this is in VB
|
|
|
|
|
Well, that's trivial to convert to C#.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I don't think you can get the user running the process by using the System.Diagnostics.Process class. You will have to either use P/Invoke as I suggested before or you could use WMI to get the user name of the process.
System.Management.ManagementObjectSearcher moSearch;
string sQuery = "";
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("winword");
foreach (System.Diagnostics.Process p in processes)
{
sQuery = "Select * From Win32_Process Where ProcessID = " + p.Id;
moSearch = new System.Management.ManagementObjectSearcher(sQuery);
string[] args = new string[5];
foreach (System.Management.ManagementObject mo in moSearch.Get())
{
mo.InvokeMethod("GetOwner", args);
if (args[0] == strNameToSearch)
System.Diagnostics.Process.GetProcessById(p.Id).Kill();
}
}
|
|
|
|
|
I have a small problem with System.Management
do I need to import something to make this work?
|
|
|
|
|
i found this
foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcessesByName(textBox1.Text))
{
proc.CloseMainWindow();
proc.WaitForExit(10000);
if (!proc.HasExited)
{
proc.Kill();
}
}
but im having trouble inputing the proccess i want to kill
it seems like no mater what i enter it allways returns a null value to proc
|
|
|
|
|
Hello,
Have you checked the name of the process in the taskmanager?
Have you debugged the foreach, are you entering it?
You should also dispose your process instances, even if you don't kill the instance.
proc.Dispose();
Hope it helps!
All the best,
Martin
|
|
|
|
|
yes its "WINWORD.EXE"
yes i am entering it only once
im not sure what you mean by dispose():
where would i put that and why. im not trying to dispose a proccess that has been created by me
|
|
|
|
|
Hello,
OK, So you are entering the loop, that means he found an instance.
So whats the problem now?
crash893 wrote: im not sure what you mean by dispose():
where would i put that and why. im not trying to dispose a proccess that has been created by me
Disposing frees the memory from any connections, so that the GarbadgeCollector (GC) can free the memory.
With the GetProcessesByName method you created an instance (or more) of the process class (Has nothing to to with creating an new winword instance).
You would also have to (or should) dispose the instances if you don't kill the process.
In your code this would mean:
foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcessesByName(textBox1.Text))
{
proc.CloseMainWindow();
proc.WaitForExit(10000);
if (!proc.HasExited)
{
proc.Kill();
}
proc.Dispose();
}
All the best,
Martin
|
|
|
|
|
everything looked like its working right but i ran into a small snag
I ran it on a terminal server ( where many people log on to run apps) and the proccess killed EVERY proccess with the name i wanted (not good)
Here is an example of what i see in taskman
Red is the scope of what it killed I am hopeing to narrow it down to who ever runs it ( in this case me rbarbrow) in BLUE
[IMG]http://i13.tinypic.com/6bcvou9.png[/IMG]
|
|
|
|
|
Hello,
Sorry for the late response!
Maybe the process member "MachineName" helps you to find the wright process.
All the best,
Martin
|
|
|
|
|
1.
int ThisSession = System.Diagnostics.Process.GetCurrentProcess().SessionId;
2.
int thisPID = System.Diagnostics.Process.GetCurrentProcess().Id;
3.
foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcessesByName(application))
4.
{
5.
if (proc.SessionId == ThisSession)
6.
{
7.
proc.CloseMainWindow();
8.
proc.WaitForExit(Wait);
9.
if (!proc.HasExited)
10.
{
11.
proc.Kill();
12.
}
13.
proc.Dispose();
14.
}
|
|
|
|
|
Does this mean you are happy with the "SessionId" property?
|
|
|
|
|
it does what i need
namely doesnt kill all the winword.exe's but just he on of the user who is running my prgram
my other thing was to find out if winword.exe is respoinding
proc.responding
and
what the current cpu load is ( im still not sure how to do that)
|
|
|
|
|
Hello,
crash893 wrote: what the current cpu load is ( im still not sure how to do that)
If you whant it for overall system you could use System.Diagnostics.PerformanceCounter like this:
System.Diagnostics.PerformanceCounter cpuCounter = new System.Diagnostics.PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
string actusage =cpuCounter.NextValue()+"%";
If you need it for a special process look at this article: http://www.codeproject.com/csharp/processescpuusage.asp[^]
Hope it helps!
All the best,
Martin
|
|
|
|
|
that kinda of works
ill post code once i get it working spesificly
|
|
|
|
|
This may sound a like a silly question, but wouldn't it make sense if one was not allowed to kill a process programaticaly?
I mean for security and also "moral" reasons. If we a are to compare software process with human individual, that would mean murder.
|
|
|
|
|
blackjack2150 wrote: This may sound a like a silly question, but wouldn't it make sense if one was not allowed to kill a process programaticaly?
I mean for security and also "moral" reasons. If we a are to compare software process with human individual, that would mean murder.
Interesting!?
But I hope people never go that far, treating programs same as humans.
I think we should start with animals first!
|
|
|
|
|
i think we have a few more things to worry about before this conversation can be taken seriously
like killing actually people and animals
|
|
|
|
|
|
i miss spoke
i think there is more to worry about before i take blackjack2150 post seriously
|
|
|
|
|
My appliction uses Microsoft's Indexing Service, i need to ensure that when my software installs it ensures that either the service is already installed or it installs the indexing service.
Thank you!
"Mess with the Best, Die like the rest"
|
|
|
|
|
Hello,
I have a simple question. Suppose you have a class X. Now you have a Class Y, which derives from Class X. Now in Class Y can I create a member which is a object of type X, let the object name be ob.
Now Suppose Class X has a public member int a, would this be a valid statement?
[Object of type Y].ob.a = 10
Thanks
|
|
|
|
|
Ummmm, yeah? But it sounds like a poor design.
|
|
|
|