469,327 Members | 1,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

How to identify your own processes

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
May 16 '07 #1
4 3041
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 Function
May 17 '07 #2
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

"Jay Parzych" <ji*****@cox.netschreef in bericht
news:2c**************************@news.east.cox.ne t...
>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 Function


May 17 '07 #3
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

"Michel Posseth [MCP]" <MS**@posseth.comwrote in message
news:e1*************@TK2MSFTNGP03.phx.gbl...
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

"Jay Parzych" <ji*****@cox.netschreef in bericht
news:2c**************************@news.east.cox.ne t...
>>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 Function



May 17 '07 #4

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


"Bill Nguyen" <bi*****************@jaco.comschreef in bericht
news:%2****************@TK2MSFTNGP02.phx.gbl...
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

"Michel Posseth [MCP]" <MS**@posseth.comwrote in message
news:e1*************@TK2MSFTNGP03.phx.gbl...
>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

"Jay Parzych" <ji*****@cox.netschreef in bericht
news:2c**************************@news.east.cox.n et...
>>>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 Function




May 18 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jeff Roughgarden | last post: by
1 post views Thread by Christopher Attard | last post: by
6 posts views Thread by Pieter | last post: by
1 post views Thread by 01423481d | last post: by
1 post views Thread by nano2k | last post: by
1 post views Thread by =?Utf-8?B?RFdhdHNvbkNyeXBLZXk=?= | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.