how to fill out the structure?

Just starting out? Need help? Post your questions and find answers here.
mitsumi
New User
New User
Posts: 2
Joined: Sun Jul 15, 2018 12:57 pm

how to fill out the structure?

Post by mitsumi »

Hello, I'm new to purebasic, how to fill out the structure?
why do I just get a part of the session?

Code: Select all

Structure WTS_SESSION_INFO Align #PB_Structure_AlignC
   SessionId.l
   pWinStationName.s
   State.l
 EndStructure
Prototype  WTSEnumerateSessions(hServer, Reserved, Version, *ppSessionInfo.WTS_SESSION_INFO, *pCount)
Prototype  WTSFreeMemory(pMemory)
Prototype  WTSOpenServer(pServerName$)
Prototype  WTSCloseServer(hServer)

Global WTSEnumerateSessions.WTSEnumerateSessions
Global WTSFreeMemory.WTSFreeMemory
Global WTSOpenServer.WTSOpenServer
Global WTSCloseServer.WTSCloseServer


Procedure.i wtsapi32_LoadDLL()
  Protected.i hDLL
 
  hDLL = OpenLibrary(#PB_Any, "wtsapi32.dll")
  If hDLL <> 0
   
    WTSCloseServer = GetFunction(hDLL, "WTSCloseServer")
    WTSFreeMemory = GetFunction(hDLL, "WTSFreeMemory")
   
    CompilerIf #PB_Compiler_Unicode
     
      WTSEnumerateSessions = GetFunction(hDLL, "WTSEnumerateSessionsW")
      WTSOpenServer = GetFunction(hDLL, "WTSOpenServerW")
     
    CompilerElse
     
      WTSEnumerateSessions = GetFunction(hDLL, "WTSEnumerateSessionsA")
      WTSOpenServer = GetFunction(hDLL, "WTSOpenServerA")
     
    CompilerEndIf
   
    ProcedureReturn hDLL
  EndIf
 
  ProcedureReturn #False
EndProcedure


Procedure IsUser()
 
  
  Protected WTSHandle.i
  Protected *SessionInfo
  Protected pCount
  Protected Ret, i
  Protected lpComputername$ = ComputerName()
  Protected Offset = 0
  
  WTSHandle=WTSOpenServer(lpComputername$)
  
  If WTSHandle
   
    Ret=WTSEnumerateSessions(WTSHandle, #Null, 1, @*SessionInfo, @pCount)

    If Ret <> 0
      For i = 0 To pCount-1
        
        
        Debug PeekS(*SessionInfo+Offset*i)

        Offset+SizeOf(WTS_SESSION_INFO)
        
      Next

      WTSFreeMemory(*SessionInfo)
    EndIf
   
    WTSCloseServer(WTSHandle)
  EndIf
 
 
EndProcedure


Define.i hDLL


    
  hDLL=wtsapi32_LoadDLL()
  
  If hDLL=0
  End
  EndIf
  
  
  Delay(150)
  IsUser()
  
  CloseLibrary(hDll)
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: how to fill out the structure?

Post by Mijikai »

Try this:

Code: Select all

Structure WTS_SESSION_INFO
  SessionId.i
  *pWinStationName
  State.i
EndStructure

Prototype  WTSEnumerateSessions(hServer, Reserved, Version, *ppSessionInfo.WTS_SESSION_INFO, *pCount)
Prototype  WTSFreeMemory(pMemory)
Prototype  WTSOpenServer(pServerName$)
Prototype  WTSCloseServer(hServer)

Global WTSEnumerateSessions.WTSEnumerateSessions
Global WTSFreeMemory.WTSFreeMemory
Global WTSOpenServer.WTSOpenServer
Global WTSCloseServer.WTSCloseServer


Procedure.i wtsapi32_LoadDLL()
  Protected.i hDLL
  
  hDLL = OpenLibrary(#PB_Any, "wtsapi32.dll")
  If hDLL <> 0
    
    WTSCloseServer = GetFunction(hDLL, "WTSCloseServer")
    WTSFreeMemory = GetFunction(hDLL, "WTSFreeMemory")
    
    CompilerIf #PB_Compiler_Unicode
      
      WTSEnumerateSessions = GetFunction(hDLL, "WTSEnumerateSessionsW")
      WTSOpenServer = GetFunction(hDLL, "WTSOpenServerW")
      
    CompilerElse
      
      WTSEnumerateSessions = GetFunction(hDLL, "WTSEnumerateSessionsA")
      WTSOpenServer = GetFunction(hDLL, "WTSOpenServerA")
      
    CompilerEndIf
    
    ProcedureReturn hDLL
  EndIf
  
  ProcedureReturn #False
EndProcedure


Procedure IsUser()
  
  
  Protected WTSHandle.i
  Protected *SessionInfo.WTS_SESSION_INFO
  Protected pCount
  Protected Ret, i
  Protected lpComputername$ = ComputerName()
  Protected Offset = 0
  
  WTSHandle=WTSOpenServer(lpComputername$)
  
  If WTSHandle
    
    Ret=WTSEnumerateSessions(WTSHandle, #Null, 1, @*SessionInfo, @pCount)
    
    If Ret <> 0
      For i = 0 To pCount-1
        

        Debug PeekS(*SessionInfo\pWinStationName)
        
        *SessionInfo + SizeOf(WTS_SESSION_INFO)
        
      Next
      
      WTSFreeMemory(*SessionInfo)
    EndIf
    
    WTSCloseServer(WTSHandle)
  EndIf
  
  
EndProcedure


Define.i hDLL



hDLL=wtsapi32_LoadDLL()

If hDLL=0
  End
EndIf


Delay(150)
IsUser()

CloseLibrary(hDll)
The structure was off and i changed the enumeration slightly.
mitsumi
New User
New User
Posts: 2
Joined: Sun Jul 15, 2018 12:57 pm

Re: how to fill out the structure?

Post by mitsumi »

Thank you!
fryquez
Enthusiast
Enthusiast
Posts: 369
Joined: Mon Dec 21, 2015 8:12 pm

Re: how to fill out the structure?

Post by fryquez »

You should also ave the address of *SessionInfo that you later pass to WTSFreeMemory()

Code: Select all

Structure WTS_SESSION_INFO
  SessionId.i
  *pWinStationName
  State.i
EndStructure

Prototype  WTSEnumerateSessions(hServer, Reserved, Version, *ppSessionInfo.WTS_SESSION_INFO, *pCount)
Prototype  WTSFreeMemory(pMemory)
Prototype  WTSOpenServer(pServerName$)
Prototype  WTSCloseServer(hServer)

Global WTSEnumerateSessions.WTSEnumerateSessions
Global WTSFreeMemory.WTSFreeMemory
Global WTSOpenServer.WTSOpenServer
Global WTSCloseServer.WTSCloseServer


Procedure.i wtsapi32_LoadDLL()
  Protected.i hDLL
  
  hDLL = OpenLibrary(#PB_Any, "wtsapi32.dll")
  If hDLL <> 0
    
    WTSCloseServer = GetFunction(hDLL, "WTSCloseServer")
    WTSFreeMemory = GetFunction(hDLL, "WTSFreeMemory")
    
    CompilerIf #PB_Compiler_Unicode
      
      WTSEnumerateSessions = GetFunction(hDLL, "WTSEnumerateSessionsW")
      WTSOpenServer = GetFunction(hDLL, "WTSOpenServerW")
      
    CompilerElse
      
      WTSEnumerateSessions = GetFunction(hDLL, "WTSEnumerateSessionsA")
      WTSOpenServer = GetFunction(hDLL, "WTSOpenServerA")
      
    CompilerEndIf
    
    ProcedureReturn hDLL
  EndIf
  
  ProcedureReturn #False
EndProcedure


Procedure IsUser()
  
  
  Protected WTSHandle.i
  Protected *SessionInfo.WTS_SESSION_INFO, *SessionInfo_free
  Protected pCount
  Protected Ret, i
  Protected lpComputername$ = ComputerName()
  Protected Offset = 0
  
  WTSHandle=WTSOpenServer(lpComputername$)
  
  If WTSHandle
    
    Ret=WTSEnumerateSessions(WTSHandle, #Null, 1, @*SessionInfo, @pCount)
    
    If Ret <> 0
      
      *SessionInfo_free = *SessionInfo
      
      For i = 0 To pCount-1
        

        Debug PeekS(*SessionInfo\pWinStationName)
        
        *SessionInfo + SizeOf(WTS_SESSION_INFO)
        
      Next
      
      WTSFreeMemory(*SessionInfo_free)
    EndIf
    
    WTSCloseServer(WTSHandle)
  EndIf
  
  
EndProcedure


Define.i hDLL



hDLL=wtsapi32_LoadDLL()

If hDLL=0
  End
EndIf


Delay(150)
IsUser()

CloseLibrary(hDll)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: how to fill out the structure?

Post by Kwai chang caine »

Hello at all 8)
I obtain
Services
Console
What is it ? :oops:
ImageThe happiness is a road...
Not a destination
Post Reply