Hi,
I'm not really sure that I have right understand your purpose
you running this program on standalone machine ?
you running the program with system account?
with GetUserName:
ex. on computer in a workgroup, if the computername is mycomp and username is admin then the returned username is "admin".
If running with system account the returned username is "system"
with GetUserNameEx:
ex. on computer in a workgroup, if the computername is mycomp and username is admin then returned username is
"mycomp\admin".
If running with system accout the returned username is "system"
With WTSQuerySessionInformation:
ex. on computer in a workgroup, if the computername is mycomp and username is admin then the returned username is
"admin", with system accout return admin
you can set the param to #true
the the name returned is
mycomp\admin in both cases.
here the new example.
Code: Select all
; coded by jpd, july 2012
EnableExplicit
#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
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.s CurrentSessionUserName(domain.b=#False)
Protected pBytesReturnedaddress.l
Protected UnameLength
Protected Username.s
Protected DomainName.s
Protected libWTS
Protected *username, *domainname
Protected WTSQuery
libWTS=LoadWTSAPI()
If libWTS
*username=AllocateMemory(256)
WTSQuery= WTSQuerySessionInformation(#WTS_CURRENT_SERVER_HANDLE,#WTS_CURRENT_SESSION , #WTSUserName ,@*Username, @pBytesReturnedaddress)
UnameLength=MemoryStringLength(*Username)
Username=PeekS(*Username,UnameLength)
WTSFreeMemory(*Username)
If domain
*domainname=AllocateMemory(256)
WTSQuery= WTSQuerySessionInformation(#WTS_CURRENT_SERVER_HANDLE,#WTS_CURRENT_SESSION , #WTSDomainName,@*domainname, @pBytesReturnedaddress)
UnameLength=MemoryStringLength(*domainname)
DomainName=PeekS(*domainname,UnameLength)
WTSFreeMemory(*domainname)
EndIf
CloseLibrary(libWTS)
EndIf
If Len(DomainName) > 0 And domain = #True
ProcedureReturn DomainName+"\"+Username
Else
ProcedureReturn Username
EndIf
EndProcedure
Debug CurrentSessionUserName()
I'm not know what the return value if more as 1 user logged in and this is running with system right.
Sorry for my English, hope my comments are readable.