Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Enterprise Development / July 2008



Tip: Looking for answers? Try searching our database.

How to get printer names in Terminal Services session

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mkjo - 08 Jul 2008 14:50 GMT
I writting a VB6 program that will run when a user logs into a terminal
server. I would like to be able to get all the printer names that come over
to the users session. I would also like to be able to identify the users
default printer.

The usual methods for getting the printer names will give me all printers in
all active user sessions:

Public Sub DiscoverPrinters()
   Dim strDefaultPrinter As String
   Dim objPrinter As Object
   For Each objPrinter In Printers
       If objPrinter.DeviceName = Printer.DeviceName Then
           strDefaultPrinter = objPrinter.DeviceName
       End If
       mcol_Printers.Add Item:=CStr(objPrinter.DeviceName),
Key:=CStr(objPrinter.DeviceName)
   Next
End Sub

Any ideas on how to grab just the printers specific to the user?
Ian Williams - 08 Jul 2008 16:50 GMT
I assume you mean the client mapped printers from the user's workstation, if
so then you need to use the WTSQuerySessionInformation() api function to get
the session ID number, as shown here

***********************
Option Explicit

Private Declare Function WTSQuerySessionInformation Lib "wtsapi32" _
   Alias "WTSQuerySessionInformationA" (ByVal hServer As Long, _
   ByVal SessionID As Long, ByVal WTSInfoClass As Long, _
   ByRef ppBuffer As Long, ByRef lLen As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
          hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Private Const WTS_CURRENT_SERVER_HANDLE     As Long = 0
Private Const WTS_CURRENT_SESSION           As Long = -1

Public Function GetTSSessionID() As Long
  Dim sVal        As String
  Dim lVal        As Long
  Dim intVal      As Integer
  Dim lRet        As Long
  Dim lLen        As Long
  Dim lErr        As Long
  Dim I As Long
  Dim sIP As String

  Dim lBufferAddress As Long

  lRet = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, _
                                  WTS_CURRENT_SESSION, _
                                  4, _
                                  lBufferAddress, _
                                  lLen)

  ' copying the buffer to a VB string

  If lLen > 0 Then
          lVal = 0
          CopyMemory lVal, ByVal lBufferAddress, lLen
          GetTSSessionID = lVal
  End If

  If lRet = 0 Then
      lErr = Err.LastDllError
      GetTSSessionID = -1
  End If

End Function
**********************************

In your DiscoverPrinters add

Dim strSession As String
Dim lngSession As Long

lngSession = GetTSSesssionID()
If lngSession > 0 Then strSession = "/Session " & lngSession

Then you test the devicename to see if it has a double underscore at the
beginning, if it has test to see if the devicename contains the strSession
value.

Signature

regards

Ian

*** inavlid email address - change country code to full country name

> I writting a VB6 program that will run when a user logs into a terminal
> server. I would like to be able to get all the printer names that come over
[quoted text clipped - 17 lines]
>
> Any ideas on how to grab just the printers specific to the user?
Mkjo - 08 Jul 2008 20:30 GMT
That works great.

Thanks Ian.

> I assume you mean the client mapped printers from the user's workstation, if
> so then you need to use the WTSQuerySessionInformation() api function to get
[quoted text clipped - 83 lines]
> >
> > Any ideas on how to grab just the printers specific to the user?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.