Examples Delphi

Title: Building a Terminal Server Client-Application
Question: Even if a full Terminal Server Client is not installed on a user's computer or you want Server Apps embedded in Delphi applications, the ActiveX control (MSTSCAX) can be a great opportunity, let's see how it works.
Answer:
Since August 02 MS provides a new way to connect to a Terminal Server
called the MSTerminal Services Advanced Client (TSAC).
The TSAC is a Win32-based ActiveX control (COM object) that can be used
to run Terminal Services sessions within EXE's that interact with
applications running on a terminal server. The TSAC is an interim release of Terminal Services components and features. The purpose is to extend the functionality of Terminal Services, client side, so that system administrators, Web page designers, and Web administrators can implement Terminal Services client sessions in Web pages or Delphi applications without requiring the user to download or install the full Terminal Services client program.
These innovations greatly extend the usefulness of Terminal Services for remote administration of servers or server applications.
So how it works in Delphi?
---------------------------
When you install the ActiveX Client Control, a minimal set of sample Web pages is also installed on your Web server, but now we're interested only in a Delphi-application, which shows in a "bitmaplike" the Servers desktop or in our example an application that runs on a server!
Developers can use the TSAC to develop client-side applications that interact with applications running on a terminal server. MSTSCAX provides full control of Terminal Services user session.
The Client Control is an ActiveX control that provides virtually the same functionality as the full Terminal Services Client, but it is designed to deliver this functionality in a simple EXE or over the Web. When embedded in Delphi applications, the ActiveX control can host a client session with a Terminal server, even if the full Terminal Server Client is not installed on a user's computer.
Note:
In order to run the following application on another computer like NT or W98 you must copy "mstscax.dll" on the target and register it with regsvr32.
The Import of the Control
--------------------------
Further tests and coding with the control are made by Dmitry Arefiev and myself.
First of all we have to import the activeX control mstscax in Delphi
1. Download the TSMSISETUP.EXE (435kb) from
http://www.microsoft.com/windows2000/downloads/recommended/TSAC/tsmsi.asp?
2. run TSMSISETUP.EXE and it will install MSTSCAX into specified directory.
3. run REGSVR32.EXE \MSTSCAX.DLL and it will register the control on your machine, then run Delphi and import the control into Delphi (via ActiveX) and create a Unit.
4. Put the imported control from the palette to a form.
MSTSCAX is windowed control, which has 2 user session painting modes: Window mode. The window client region represents area, in which Terminal Server session will be painted.
Full Screen mode. Here will be created special window, which will cover all desktop area. And user session will be painted in this window, and not in MSTSCAX window client region.
The creating of the Client
-------------------------------
Now we create a Delphi project and first have a look at four interesting properties:
1. Server: type a Terminal server name or TCP\IP address or select a server from the list of Available servers.
Note: If you previously disconnected from a Terminal server without ending the session, the Terminal Services Client reconnects to that session (if the connection is configured for the reconnection of disconnected sessions).
2. Domain: Type your user name, password, and domain (if required), so it's the domain which user name belongs.
procedure TfrmMain.btnConnectClick(Sender: TObject);
var pathToRun: string[255];
begin
pathToRun:='C:\WINNT\system32\clock.exe';
with TfrmTerminal.Create(Application) do begin
MsTscAx1.Server := edtServer.Text;
MsTscAx1.UserName := edtUserName.Text;
MsTscAx1.Domain := edtDomain.Text;
MsTscAx1.SecuredSettings.StartProgram := pathToRun;
....
3. BitmapPeristence: if we want to cache bitmaps set it to 1 (bitmaps stored on your local hard drive)
4. Compress: if we want to compress data set it to 1
procedure TfrmTerminal.FormCreate(Sender: TObject);
begin
MsTscAx1.AdvancedSettings.BitmapPeristence := 1;
MsTscAx1.AdvancedSettings.Compress := 1;
end;

After all with the connect method we open the terminal emulation session:
MsTscAx1.Connect;
and with the disconnect method we close the connection-channel but session is not closed on the server. Therefore we must close the app on server by first closing the client application:
procedure TfrmTerminal.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
if not frmMain.btnConnect.enabled then begin
messageDlg('close server app', mtInformation, [mbOK], 0);
Action := caNone;
end;
end;
Then we receive onDisconnected so we can close the form:
procedure TfrmTerminal.MsTscAx1Disconnected(Sender: TObject;
DisconnectReason: Integer);
begin
Close;
end;
Benefits:
Full application is on the server. Then MSTSCAX may be used to setup and run user session on client computer. You can also split application into parts. Some parts will be located on server and some on client computer.
The MSTSCAX will be used to integrate parts into single application.
One last compile: the propertie BitmapPeristence, I mean it should written Persistence but a look at the wrapper shows BitmapPeristence ;):
//
*********************************************************************//
// DispIntf: IMsTscAdvancedSettingsDisp
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {809945CC-4B3B-4A92-A6B0-DBF9B5F2EF2D}
//
*********************************************************************//
IMsTscAdvancedSettingsDisp = dispinterface
['{809945CC-4B3B-4A92-A6B0-DBF9B5F2EF2D}']
property Compress: Integer dispid 121;
property BitmapPeristence: Integer dispid 122;
property allowBackgroundInput: Integer dispid 161;
property KeyBoardLayoutStr: WideString writeonly dispid 162;
property PluginDlls: WideString writeonly dispid 170;
property IconFile: WideString writeonly dispid 171;
property IconIndex: Integer writeonly dispid 172;
property ContainerHandledFullScreen: Integer dispid 173;
property DisableRdpdr: Integer dispid 174;
end;