WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Terminal Server

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Terminal Server WTSQuerySessionInformation

 Keywords: Windows Terminal Server WTS UDF 


Update: Use the wtsQuerySessionInfo function in the Terminial Services Extender.


Sample from user:

As I seemed to always be writing another DLLCall to WTSQuerySessionInformation, I've created a more generic UDF to call any of its entrypoints, from one to all at once.

I'm not certain about the values returned by WTSClientAddress, but I get the IP address, and that's all that I care about.

;UDF to use WTSQuerySessionInformation more generically
#DefineFunction evsWTSQuerySession(session_ID, query_str)
   ;query with bitwise or:
   ;
   ;Value  Function               i Returns
   ;-----  --------------------- -- --------
   ;    1  WTSInitialProgram      0 string
   ;    2  WTSApplicationName     1 string
   ;    4  WTSWorkingDirectory    2 string
   ;    8  WTSOEMId               3 string
   ;   16  WTSSessionId           4 LONG
   ;   32  WTSUserName            5 String 
   ;   64  WTSWinStationName      6 String
   ;  128  WTSDomainName          7 String
   ;  256  WTSConnectState        8 LONG
   ;  512  WTSClientBuildNumber   9 LONG
   ; 1024  WTSClientName         10 String
   ; 2048  WTSClientDirectory    11 String
   ; 4096  WTSClientProductId    12 LONG
   ; 8192  WTSClientHardwareId   13 LONG
   ;16384  WTSClientAddress      14 Structure: , , 
   ;32768  WTSClientDisplay      15 Structure: , 
   ;65536  WTSClientProtocolType 16 LONG
   ;
   ;NULLs returned as "NULL"
   
   query = (%query_str%)
   dll_name = "M:\WTSRV\system32\WTSAPI32.DLL"
   ep_name = "WTSQuerySessionInformationA"
   WTSserver = 0  ;always user current server
   dll_handle = DllLoad(dll_name)
   str_return = ""

   for xx = 0 to 16
      if query & (2 ** xx)
         ;get value
         if str_return != "" then str_return = StrCat(str_return, "|")
         WTSInfoClass = xx
         if (WTSINfoClass == 4) || (WTSINfoClass == 8) || (WTSINfoClass == 9) || (WTSINfoClass == 12) || (WTSINfoClass == 13) || (WTSINfoClass == 16)
            type = 2 ;LONG
         else
            if (WTSINfoClass == 14) || (WTSINfoClass == 15)
               type = 0 ;structure
            else
               type = 1 ;string
            endif
         endif
         ppBuffer = BinaryAlloc(4)             ;Allocate buffer for return pointer
         BinaryEODSet(ppBuffer, 4)             ;tell WinBatch all data is valid
         pBytesReturned = BinaryAlloc(4)       ;Allocate buffer for return count
         BinaryEODSet(pBytesReturned, 4)       ;Tell WInbatch all data is valid
         flag = DllCall(dll_handle, long:ep_name, LONG:WTSServer, long:Session_ID, LONG:WTSInfoClass, lpbinary:ppBuffer,lpbinary:pBytesReturned)
         if flag
            BytesReturned = BinaryPeek4(pBytesReturned, 0)
            Pointer2Buffer = BinaryPeek4(ppBuffer, 0)
            if type == 2
               this_value = IntControl(32, Pointer2Buffer, "LONG", 0, 0)
            else
               if type == 1
                  this_value = evsMemToStr(Pointer2Buffer, BytesReturned)
               else
                  if WTSINfoClass == 14
                     this_value1 = IntControl(32, Pointer2Buffer, "LONG", 0, 0)
                     this_value2 = ""
                     for yy = 2 to 5
                        if this_value2 != "" then this_value2 = StrCat(this_value2, ".")
                        temp_value = IntControl(32, (Pointer2Buffer + 4 + yy), "BYTE", 0, 0)
                        if temp_value < 0 then temp_value = (256 + temp_value)
                        this_value2 = StrCat(this_value2, temp_value)
                     next
                     this_value = StrCat(this_value1, ",", this_value2)
                  endif
                  if WTSINfoClass == 15
                     this_value1 = IntControl(32, Pointer2Buffer, "LONG", 0, 0)
                     this_value2 = IntControl(32, (Pointer2Buffer + 4), "LONG", 0, 0)
                     this_value3 = IntControl(32, (Pointer2Buffer + 8), "LONG", 0, 0)
                     this_value = StrCat(this_value1, ",", this_value2, ",", this_value3)
                  endif
               endif
            endif
            if this_value == "" then this_value = "NULL"
            str_return = StrCat(str_return, this_value)
            DllCall(dll_handle, void:"WTSFreeMemory", LONG:Pointer2Buffer)
         endif
         BinaryFree(pBytesReturned)
         BinaryFree(ppBuffer)
      endif
   next
   DllFree(dll_handle)
   return str_return
#EndFunction

#DefineFunction evsMemToStr(p2Buffer, max_bytes)
   mystring = ""
   str_end = p2Buffer + max_bytes
   while p2Buffer < str_end
      byte_val = IntControl(32, p2Buffer, "BYTE", 0, 0)
      if byte_val == 0 then break
      char = Num2Char(byte_val)
      mystring = strcat(mystring, char)
      p2Buffer = p2Buffer + 1
   endwhile
   return mystring
#EndFunction


session = AskLine("Test", "Session ID", 0)
function = AskLine("Test", "Bitwise OR'd Functions", "32 | 64")
value = evsWTSQuerySession(session, function)
Message("Test", value)
      


Article ID:   W14998
File Created: 2011:11:09:09:23:56
Last Updated: 2011:11:09:09:23:56