473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with WinAPI

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.GetLastWin3 2Error)
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


Nov 21 '05 #1
1 2113
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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: NJSG | last post by:
In english ========== It exists any way to copy directly the content of a file to ano- ther file using a Win32 (WinAPI) C application, that doesn't uses ms-dos "copy" and doesn't needs to ...
3
by: Thomas Mobley | last post by:
I'm having a problem getting the name of my card readers using winscard. The same function works in vb6, but I think I must be missing something here. Anybody see what I'm doing wrong? ...
0
by: James | last post by:
I have a Dll written in C. And I want to Import that Dll into my C# program and use a function in the Dll. In a previous newsgroup article I saw that I may need to wrap the Dll in a C++ .NET class...
1
by: James | last post by:
I have a problem with a header file which was written in C++ which I need to implement in C#. The header file in C++ IS something like this, ........ #ifdef __cplusplus extern "C" { void...
3
by: Yura Tigiev | last post by:
Hello! Is it possible to write (classical windows with My computers, My Desctop, My networks Places end etc ) FolderBrowseDialog without use WinApi? If yes, can you send source code ? Yuri
1
by: MuZZy | last post by:
HI, I would really appreciate some hwlp with this: Here is the WinAPI struct used in call to winapi function wavInGetDevCaps: typedef struct { WORD wMid; WORD wPid; MMVERSION...
7
by: jg | last post by:
or would I have to use C# or C++?
0
by: sslaughter | last post by:
Hello everybody! i've got new problem (which , as i suppose , shouldn;t be a problem for you) Here is the code: protected void notify_click(Object sender, System.EventArgs e) {
3
by: =?Utf-8?B?dGhlamFja29mYWxs?= | last post by:
I have a few global variables and a function that gets called multiple times to complete a single transaction and uses the variables. The function gets different notifications. When the function...
6
by: ÓÚÑó | last post by:
Hi all! I have a Dll like this: #include <windows.h> #include <stdio.h> #include <stdlib.h> __declspec (dllexport) int Add (int n) { int x;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.