Как узнать приложение работает в терминальной сессии или на рабочей станции.
Конечно-же заранее не знаем, ни имени сервера, ни рабочей станции
Private Declare Function WTSEnumerateProcesses Lib "wtsapi32.dll" Alias _
"WTSEnumerateProcessesA" (ByVal hServer As Long, ByVal Reserved As Long, _
ByVal Version As Long, ByRef ppProcessInfo As Long, ByRef pCount As Long) As Long
Private Declare Sub WTSFreeMemory Lib "wtsapi32.dll" (ByVal pMemory As Long)
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Private Type WTS_PROCESS_INFO
SessionID As Long
ProcessId As Long
pProcessName As Long
pUserSid As Long
End Type
Public Sub Main()
MsgBox TerminalServerSessionId
End Sub
'Purpose : Returns a terminal server session ID
'Inputs : N/A
'Outputs : Returns "0" if not a terminal server, else returns
' the terminal server session ID.
'Author : Andrew Baker
'Date : 25/05/2001
'Notes :
Function TerminalServerSessionId() As String
Dim lRetVal As Long, lCount As Long, lThisProcess As Long, lThisProcessId As Long
Dim lpBuffer As Long, lp As Long, udtProcessInfo As WTS_PROCESS_INFO
Const WTS_CURRENT_SERVER_HANDLE = 0&
On Error GoTo ErrNotTerminalServer
'Set Default Value
TerminalServerSessionId = "0"
lThisProcessId = GetCurrentProcessId
lRetVal = WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0&, 1, lpBuffer, lCount)
If lRetVal Then
'Successful
lp = lpBuffer
For lThisProcess = 1 To lCount
CopyMemory udtProcessInfo, ByVal lp, LenB(udtProcessInfo)
If lThisProcessId = udtProcessInfo.ProcessId Then
TerminalServerSessionId = CStr(udtProcessInfo.SessionID)
Exit For
End If
lp = lp + LenB(udtProcessInfo)
Next
'Free memory buffer
WTSFreeMemory lpBuffer
End If
Exit Function
ErrNotTerminalServer:
'The machine is not a Terminal Server
On Error GoTo 0
End Function
Environ("clientname")
tyomitch писал(а):Andrey, твой код совсем-совсем бредовый.
Фактически, вся эта страница кода заменяет один вызов ProcessIdToSessionId.
Andrey Fedorov писал(а):P.S Иногда проще и быстрей взять чужой кусок работающего кода чем тратить время на разборки с целью сэкономить пару строк...
Сейчас этот форум просматривают: Majestic-12 [Bot] и гости: 94