WTStek.com Windows Terminal Server, Virtualization, Application Delivery, Windows Software Development, Market Analysis, Training Classes and more...
Navigation

Virtualization Solutions

... Articles and Whitepapers
... Downloads
... Internet Resources

Books

... Windows Server 2008 Terminal Services
... Windows Server 2003 Terminal Services

My Profile

... About this Web Site
... Benny's Biography
... Presentations 2008, 2007, 2006, 2005, 2004 and earlier

 


Awards

 

Microsoft MVP

 

CTP

 

Provision Networks / Quest VIP

 

An Introduction into Terminal Services API Programming

Posted by Benny Tritsch on July 4, 2007 – updated on November 30, 2007

[Introduction] [TS API] [Programming Basics] [More Sample Code]

TS API Programming Basics

A very simple program shows the good old sandwich programming style that is required if using the TS API. Those of you who did some Win32 API programming in the old days should be familiar with it. Sandwich programming means that you need a function to open a handle as one slice of toast and functions to close this handle and clean up the memory as another slice of toast. All the other functions between the two slices are the beef.

Most Terminal Services programming is done according to the following principles:

  1. Retrieve the names of the required terminal servers
  2. Open a handle to a specific terminal server
  3. Perform the required tasks, such as enumerating and managing user sessions or processes
  4. Free allocated resources, such as buffers
  5. Close the open handle to the terminal server

Here is some sample code that demonstrates these rather simple principles.

 

#include <stdio.h>
#include <windows.h>
#include <wtsapi32.h>

int main(int argc, char* argv[])
{
  char* pszServerName = NULL;
  HANDLE serverHandle;

  serverHandle = WTSOpenServer(pszServerName);
  // more TS specific code
  // WTSFreeMemory(SomeAllocatedResources);
  WTSCloseServer(serverHandle);
}

 

In the next example, the beef is represented by the WTSEnumerateProcesses function call which returns the number of processes and a pointer to a process structure—PWTS_PROCESS_INFO.

pProcessName is an element (or member) of the process structure. It includes the pointer to a null-terminated string containing the executable name of the process. If you want to find out more about the details of the Terminal Server API, looking into Wtsapi32.h is very helpful.

 

#include <stdio.h>
#include <windows.h>
#include <wtsapi32.h>

int main(int argc, char* argv[])
 {
   char*  pszServerName = NULL;
   HANDLE  serverHandle;
   PWTS_PROCESS_INFO  pProcessInfo;
   DWORD  count;

   serverHandle = WTSOpenServer(pszServerName);
   WTSEnumerateProcesses(serverHandle, 0, 1, &pProcessInfo, &count)
   // more TS specific code
   WTSFreeMemory(pProcessInfo);
   WTSCloseServer(serverHandle);
 }

 

Next