How to identify your own processes

B

Bill Nguyen

I ran into this problem in a Terminal Server environment

When trying to kill all processes having the same name (to avoid hanging
instances of this process), my users ran into permission problem (access
denied) because the routine tried to kill other users' process as well.
Is there away to idenfity process owner in a Terminal server environment so
that only user's own processes to be eliminated?

Thanks

Bill


Dim myProcessName As String = "ProcessName"

Dim myProcesses() As Process

Dim myProcess As Process



myProcesses = Process.GetProcessesByName(myProcessName)

For Each myProcess In myProcesses

Try

myProcess.Kill()

Catch ex As Exception

MsgBox(ex.ToString)

End Try

Next
 
J

Jay Parzych

I ran into this problem in a Terminal Server environment
When trying to kill all processes having the same name (to avoid
hanging
instances of this process), my users ran into permission problem
(access
denied) because the routine tried to kill other users' process as
well.
Is there away to idenfity process owner in a Terminal server
environment so
that only user's own processes to be eliminated?
Thanks

Bill

Dim myProcessName As String = "ProcessName"

Dim myProcesses() As Process

Dim myProcess As Process

myProcesses = Process.GetProcessesByName(myProcessName)

For Each myProcess In myProcesses

Try

myProcess.Kill()

Catch ex As Exception

MsgBox(ex.ToString)

End Try

Next

not sur eif this will help.

Its been a long time, but In VB6, i used to do code below at application
startup:

and when the user clicked Help About, i displayed that session id.

its been a few years sionce i was in that environment, so i have not had
to figure this out in dotnet



Private Const SmREMOTESESSION = &H1000

Private Declare Function ProcessIdToSessionId _
Lib "kernel32" (ByVal dwProcessId As Long, _
ByRef pSessionId As Long) As Long

Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long


Private Declare Function GetSystemMetrics _
Lib "user32" (ByVal nIndex As Long) As Long






Private Function GetTSSessionID() As Long

Dim lngSessionID As Long
Dim lngReturnCode As Long

If GetSystemMetrics(SmREMOTESESSION) <> 0 Then
'It's terminal services

lngReturnCode = ProcessIdToSessionId(GetCurrentProcessId(), lngSessionID)

If lngReturnCode <> 0 Then
'Success!
GetTSSessionID = lngSessionID
Else
'Error
GetTSSessionID = 0
End If

Else
'Not Terminal Services
GetTSSessionID = 0
End If

End Functio
 
M

Michel Posseth [MCP]

Well you could set a mutex and check the process if it has this mutex

i.ow. the mutex should be user specific ( username ?? )

regards

Michel
 
B

Bill Nguyen

Jay;

I'm on VS 2005, and I think they should have a way to identify process
owner. I don't really want to resort to using Win32 API.

Michel;
Can you please elaborate?

Thanks again

Bill
 
M

Michel Posseth [MCP]

well you could modify this

http://www.freevbcode.com/ShowCode.asp?ID=7654


instead of the guid you should create a string containing progname /
username
this should then be implemented in all progs you want to have the ability
to manipulate through the process lib

Now the application , that needs to kill the process can just check for the
existence of the mutex ( mutex is stored at kernel level )

but i guess there should be an easier way , to detect the user context in
wich the app is runing
 

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