Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / December 2004

Tip: Looking for answers? Try searching our database.

Problem with WinAPI

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nikolay Petrov - 22 Dec 2004 07:25 GMT
I use some of Windows Terminal Services APIs in my app.

The is an API function which enumerates sessions on a terminal server.

It uses a pointer from another API function to know on which exactly server
to operate.

But whatever I do the function always get sessions on my local  WinXP
machine, not the required server.

Can someone have a look?

TIA

Here is the code:

   <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
   Private Structure WTS_SESSION_INFO
       Dim SessionID As Int32 'DWORD integer
       Dim pWinStationName As String ' integer LPTSTR - Pointer to a
null-terminated string containing the name of the WinStation for this
session
       Dim State As WTS_CONNECTSTATE_CLASS
   End Structure

   Friend Structure strSessionsInfo
       Dim SessionID As Integer
       Dim StationName As String
       Dim ConnectionState As String
   End Structure

   Private Enum WTS_CONNECTSTATE_CLASS
       WTSActive
       WTSConnected
       WTSConnectQuery
       WTSShadow
       WTSDisconnected
       WTSIdle
       WTSListen
       WTSReset
       WTSDown
       WTSInit
   End Enum

  <DllImport("wtsapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Function WTSOpenServer(ByVal pServerName As String) As
IntPtr
   End Function

   <DllImport("wtsapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Sub WTSCloseServer(ByVal hServer As IntPtr)
   End Sub

   <DllImport("wtsapi32.dll")> _
   Private Shared Sub WTSFreeMemory(ByVal pMemory As IntPtr)
   End Sub

   <DllImport("wtsapi32.dll", _
       bestfitmapping:=True, _
       CallingConvention:=CallingConvention.StdCall, _
       CharSet:=CharSet.Auto, _
       EntryPoint:="WTSEnumerateSessions", _
       setlasterror:=True, _
       ThrowOnUnmappableChar:=True)> _
   Private Shared Function WTSEnumerateSessions( _
       ByVal hServer As IntPtr, _
       <MarshalAs(UnmanagedType.U4)> _
       ByVal Reserved As Int32, _
       <MarshalAs(UnmanagedType.U4)> _
       ByVal Vesrion As Int32, _
       ByRef ppSessionInfo As IntPtr, _
       <MarshalAs(UnmanagedType.U4)> _
       ByRef pCount As Int32) As Int32
   End Function

   Friend Function GetSessions(ByVal ServerName As String) As
strSessionsInfo()
       Dim ptrOpenedServer As IntPtr
       Dim RetVal As strSessionsInfo()
       Try
           ptrOpenedServer = WTSOpenServer(ServerName)
           Dim FRetVal As Int32
           Dim ppSessionInfo As IntPtr = IntPtr.Zero
           Dim Count As Int32 = 0
           Try
               FRetVal = WTSEnumerateSessions(ptrOpenedServer, 0, 1,
ppSessionInfo, Count)
               If FRetVal <> 0 Then
                   Dim sessionInfo() As WTS_SESSION_INFO = New
WTS_SESSION_INFO(Count) {}
                   Dim i As Integer
                   For i = 0 To Count - 1
                       sessionInfo(i) =
CType(Marshal.PtrToStructure(ppSessionInfo, GetType(WTS_SESSION_INFO)),
WTS_SESSION_INFO)
                   Next
                   WTSFreeMemory(ppSessionInfo)
                   Dim tmpArr(sessionInfo.GetUpperBound(0)) As
strSessionsInfo
                   For i = 0 To tmpArr.GetUpperBound(0)
                       tmpArr(i).SessionID = sessionInfo(i).SessionID
                       tmpArr(i).StationName =
sessionInfo(i).pWinStationName
                       tmpArr(i).ConnectionState =
GetConnectionState(sessionInfo(i).State)
                   Next
                   ReDim sessionInfo(-1)
                   RetVal = tmpArr
               Else
                   Throw New ApplicationException("No data retruned")
               End If
           Catch ex As Exception
               Throw New Exception(ex.Message & vbCrLf &
System.Runtime.InteropServices.Marshal.GetLastWin32Error)
           End Try
       Catch ex As Exception
           Throw New Exception(ex.Message)
           Exit Function
       Finally
            WTSCloseServer(ptrOpenedServer)
      End Try
       Return RetVal
   End Function

   Private Function GetConnectionState(ByVal State As
WTS_CONNECTSTATE_CLASS) As String
       Dim RetVal As String
       Select Case State
           Case WTS_CONNECTSTATE_CLASS.WTSActive
               RetVal = "Active"
           Case WTS_CONNECTSTATE_CLASS.WTSConnected
               RetVal = "Connected"
           Case WTS_CONNECTSTATE_CLASS.WTSConnectQuery
               RetVal = "Query"
           Case WTS_CONNECTSTATE_CLASS.WTSDisconnected
               RetVal = "Disconnected"
           Case WTS_CONNECTSTATE_CLASS.WTSDown
               RetVal = "Down"
           Case WTS_CONNECTSTATE_CLASS.WTSIdle
               RetVal = "Idle"
           Case WTS_CONNECTSTATE_CLASS.WTSInit
               RetVal = "Initializing."
           Case WTS_CONNECTSTATE_CLASS.WTSListen
               RetVal = "Listen"
           Case WTS_CONNECTSTATE_CLASS.WTSReset
               RetVal = "reset"
           Case WTS_CONNECTSTATE_CLASS.WTSShadow
               RetVal = "Shadowing"
           Case Else
               RetVal = "Unknown connect state"
       End Select
       Return RetVal
   End Function
Mattias Sj?gren - 27 Dec 2004 23:57 GMT
Nikolay,

>Can someone have a look?

You never check the return value of WTSOpenServer to verify that you
get a valid handle. Is it possible that the function fails? The docs
say that it returns NULL on failure, which happens to be the same
value as the WTS_CURRENT_SERVER_HANDLE constant used to refer to the
local server.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
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.