In diesen beispiel wird auch die zugehörige SID des benutzer ausgewertet.
getestet auf Win 2000 Server/XP/Win7/2003.
Ciao
jpd
Code: Alles auswählen
; coded by jpd, july 2010
#WTS_CURRENT_SERVER_HANDLE = 0
#WTS_CURRENT_SESSION = -1
Enumeration ;WTS_INFO_CLASS
#WTSInitialProgram=0
#WTSApplicationName
#WTSWorkingDirectory
#WTSOEMId
#WTSSessionId
#WTSUserName
#WTSWinStationName
#WTSDomainName
#WTSConnectState
#WTSClientBuildNumber
#wtsclientname
#WTSClientDirectory
#WTSClientProductId
#WTSClientHardwareId
#WTSClientAddress
#WTSClientDisplay
#WTSClientProtocolType
EndEnumeration
Prototype WTSQuerySessionInformation(hServer.l,SessionId.l,WtsInfoClass.l,ppBuffer.l,pBytesReturned.l)
Global WTSQuerySessionInformation.WTSQuerySessionInformation
Prototype WTSFreeMemory(lpBuffer.l)
Global WTSFreeMemory.WTSFreeMemory
Prototype ConvertSidToStringSid(SidS.l,pSid.l)
Global ConvertSidToStringSid.ConvertSidToStringSid
Procedure LoadWTSAPI()
Protected Result
Result = OpenLibrary(#PB_Any, "wtsapi32.dll")
If Result
WTSFreeMemory=GetFunction(Result, "WTSFreeMemory")
CompilerIf #PB_Compiler_Unicode
WTSQuerySessionInformation = GetFunction(Result, "WTSQuerySessionInformationW")
CompilerElse
WTSQuerySessionInformation = GetFunction(Result, "WTSQuerySessionInformationA")
CompilerEndIf
If WTSQuerySessionInformation = 0
CloseLibrary(Result)
Result = 0
EndIf
EndIf
ProcedureReturn Result
EndProcedure
Procedure LoadADVApi()
Protected Result
Result = OpenLibrary(#PB_Any, "advapi32.dll")
If Result
CompilerIf #PB_Compiler_Unicode
ConvertSidToStringSid = GetFunction(Result, "ConvertSidToStringSidW")
CompilerElse
ConvertSidToStringSid = GetFunction(Result, "ConvertSidToStringSidA")
CompilerEndIf
If ConvertSidToStringSid = 0
CloseLibrary(Result)
Result = 0
EndIf
EndIf
ProcedureReturn Result
EndProcedure
Procedure.s CurrentSessionUserName()
Protected pBytesReturnedaddress.l
Protected UnameLength
Protected Username.s
Protected DomainName.s
*username=AllocateMemory(256)
*domainname=AllocateMemory(256)
WTSQuery= WTSQuerySessionInformation(#WTS_CURRENT_SERVER_HANDLE,#WTS_CURRENT_SESSION , #WTSUserName ,@*Username, @pBytesReturnedaddress)
UnameLength=MemoryStringLength(*Username)
Username=PeekS(*Username,UnameLength)
WTSFreeMemory(*Username)
WTSQuery= WTSQuerySessionInformation(#WTS_CURRENT_SERVER_HANDLE,#WTS_CURRENT_SESSION , #WTSDomainName,@*domainname, @pBytesReturnedaddress)
UnameLength=MemoryStringLength(*domainname)
DomainName=PeekS(*domainname,UnameLength)
WTSFreeMemory(*domainname)
If DomainName <> ""
ProcedureReturn DomainName+"\"+Username
Else
ProcedureReturn Username
EndIf
EndProcedure
Procedure.s UserNametoStringSID(UserName.s)
Protected SIDString.s
*username = AllocateMemory(256)
PokeS(*username, UserName)
rc.l = LookupAccountName_(#Null, *username, #Null, @sidbuflen, *domainname, @dnbuflen, @sidtype)
*domainname = AllocateMemory(dnbuflen)
*sid = AllocateMemory(sidbuflen)
rc = LookupAccountName_(#Null, *username, *sid, @sidbuflen, *domainname, @dnbuflen, @sidtype)
If IsValidSid_(*sid)
libAV=LoadADVApi()
If libAV
*psid=AllocateMemory(256)
ConvertSidToStringSid(*sid,@*psid)
lu=MemoryStringLength(*psid)
SIDString=PeekS(*psid,lu)
LocalFree_(*psid)
EndIf
Else
SIDString="<INVALID SID>"
EndIf
FreeMemory(*username)
FreeMemory(*domainname)
FreeMemory(*sid)
CloseLibrary(libAV)
ProcedureReturn SIDString
EndProcedure
libWTS=LoadWTSAPI()
If libWTS
Debug "loaded"
UName.s=CurrentSessionUserName()
SID.s=UserNametoStringSID(UName)
CloseLibrary(libWTS)
MessageRequester("UserName & SID",UName +#CRLF$+SID)
Else
Debug "libWTS not loaded"
EndIf