Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get printer names in Terminal Services session

473 views
Skip to first unread message

Mkjo

unread,
Jul 8, 2008, 9:50:05 AM7/8/08
to
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

unread,
Jul 8, 2008, 11:50:25 AM7/8/08
to
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.

--

regards

Ian

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


"Mkjo" <Mk...@discussions.microsoft.com> wrote in message
news:1791E414-7572-4773...@microsoft.com...

Mkjo

unread,
Jul 8, 2008, 3:30:45 PM7/8/08
to
That works great.

Thanks Ian.

0 new messages