Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
479 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Programming a Mail-SlotGo to Christian Kuttler's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Communication via Mail-Slot
Product:
Delphi all versions
Category:
Communication
Skill Level:
Scoring:
Last Update:
11/25/2001
Search Keys:
delphi delphi3000 article borland vcl code-snippet Communication; Mail-Slot;Chat-Programm
Times Scored:
5
Visits:
5410
Uploader: Christian Kuttler
Company: Centerparcs Europe Germany
Reference: N/A
 
Question/Problem/Abstract:
How can two applications communicate with each other if they are running on different machines?
Answer:



unit Chris;

interface


uses  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, ExtCtrls, Menus, IniFiles, Buttons, ComCtrls,
  Spin, Registry, FileCtrl, ShellAPI, WinSvc;

// -----------------------------------------------------------------------
// First of all the Type-Definitions

Type
  TChMailSlot = class
    LastError: String;
    function CreateLocalServer(MailSlotName: String): Boolean;
    function CreateRemoteServer(Server, MailSlotName: String): Boolean;
    function CreateLocalClient(MailSlotName: String): Boolean;
    function CreateRemoteClient(Server, MailSlotName: String): Boolean;
    function ReadMail : String;
    function WriteMail(Text: String) : Boolean;
    procedure CloseServer;
    procedure CloseClient;
  private
    { Private declarations }
    FMailslotServer : THandle;
    FMailslotClient : THandle;
  public
    { Public declarations }
  end;

Var ChMailSlot : TChMailSlot;


implementation


{ -----------------------------------------------------------------------------}
{ --------------------   Begin of TChMailSlot-Declarations   ----------------- }
{ -----------------------------------------------------------------------------}

function TChMailSlot.CreateLocalServer(MailSlotName: String): Boolean;
const cERRNOCREATE   = 'The Mailslot %s could not be created';
      cSUCCESSCREATE = 'The Mailslotserver %s has been created';
begin
  LastError:= '';
  FMailslotServer := CreateMailslot(PChar('\\.\mailslot\'+MailSlotName), 0, 0, nil);
  if FMailslotServer = Invalid_Handle_Value then begin
    Result:= False;
    LastError:= Format(cERRNOCREATE, [MailSlotName]);
  end else begin
    Result:= True;
    LastError:= Format(cSUCCESSCREATE, [MailSlotName]);
  end;
end;

function TChMailSlot.CreateRemoteServer(Server, MailSlotName: String): Boolean;
const cERRNOCREATE   = 'The Mailslot %s could not be created';
      cSUCCESSCREATE = 'The Mailslotserver %s has been created';
begin
  LastError:= '';
  FMailslotServer := CreateMailslot(PChar('\\'+Server+'\mailslot\'+MailSlotName), 0, 0, nil);
  if FMailslotServer = Invalid_Handle_Value then begin
    Result:= False;
    LastError:= Format(cERRNOCREATE, [MailSlotName]);
  end else begin
    Result:= True;
    LastError:= Format(cSUCCESSCREATE, [MailSlotName]);
  end;
end;

function TChMailSlot.CreateLocalClient(MailSlotName: String): Boolean;
Const cERRNOOPEN     = 'The Mailslot %s could not be opened.';
      cSUCCESSCREATE = 'The Mailslotclient %s could be created';
begin
  FMailslotClient := CreateFile(PChar('\\.\mailslot\'+MailSlotName), Generic_Write, File_Share_Read,
                          nil, Open_Existing, File_Attribute_Normal, 0);
  if FMailslotClient = INVALID_HANDLE_VALUE then begin
    Result:= False;
    LastError:= Format(cERRNOOPEN, [MailSlotName]);
  end else begin
    Result:= True;
    LastError:= Format(cSUCCESSCREATE, [MailSlotName]);
  end;
end;

function TChMailSlot.CreateRemoteClient(Server, MailSlotName: String): Boolean;
Const cERRNOOPEN     = 'The Mailslot %s could not be opened.';
      cSUCCESSCREATE = 'The Mailslotclient %s was be created successfully';
begin
  FMailslotClient := CreateFile(PChar('\\'+Server+'\mailslot\'+MailSlotName), Generic_Write, File_Share_Read,
                          nil, Open_Existing, File_Attribute_Normal, 0);
  if FMailslotClient = INVALID_HANDLE_VALUE then begin
    Result:= False;
    LastError:= Format(cERRNOOPEN, [MailSlotName]);
  end else begin
    Result:= True;
    LastError:= Format(cSUCCESSCREATE, [MailSlotName]);
  end;
end;

function TChMailSlot.ReadMail : String;
const cERRNOINFO     = 'Reading Mailslot information not possible.';
      cERRNOREAD     = 'It was not possible to read the information from the Mailslot.';
Var  iMsgSize : DWord;
     iRead    : DWord;
     sMsg     : String;
begin
  Result:= '';
  LastError:= '';
  if not GetMailslotInfo(FMailslotServer, nil, iMsgSize, nil, nil) then
  begin
    LastError:= cERRNOINFO;
  end;
  if iMsgSize <> MAILSLOT_NO_MESSAGE then
    begin
      SetLength(sMsg, iMsgSize);
      if not ReadFile(FMailslotServer, sMsg[1], iMsgSize, iRead, nil) then
        begin
          LastError:=cERRNOREAD;
        end;
      Result:= sMsg;
    end;
end;

function TChMailSlot.WriteMail(Text: String) : Boolean;
const cERRNOWRITE = 'It was not possible to send the information to the Mailslot.';
var
  iBytes : DWord;
  sMsg   : String;
begin
  sMsg := Text;
  LastError:= cERRNOWRITE;
  Try
    WriteFile(FMailslotClient, sMsg[1], Length(sMsg), iBytes, nil);
    Result:= True;
    LastError:='';
  except
    LastError:= cERRNOWRITE;
    Result:= False;
  end;
end;


procedure TChMailSlot.CloseServer;
begin
  CloseHandle(FMailslotServer);
end;

procedure TChMailSlot.CloseClient;
begin
  CloseHandle(FMailslotClient);
end;

begin  { ----------------  Initialisation of the Unit ---------------- }

ChMailSlot:= TChMailSlot.Create;
END.


{ -----------------------------------------------------------------------------}
{ ---------------------- End of TChMailSlot-Declarations --------------------- }
{ -----------------------------------------------------------------------------}





{ And now a simple client-programm using the Mailslot definitions above defined in the unit Chris }

unit MainClient;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,Chris;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Messages: TEdit;
    Text: TEdit;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ChMailSlot.CreateLocalClient('TestMailSlot');
  Messages.Text:= ChMailSlot.LastError;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ChMailSlot.CloseClient;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  If not ChMailSlot.WriteMail(Text.Text) then begin
    MessageBeep(0);
  end;
  Messages.Text:= ChMailSlot.LastError;
  Text.SelectAll;
end;

end.




{ And the corresponding server-programm using the Mailslot definitions above defined in the unit Chris }


unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,Chris, ExtCtrls;

type
  TForm1 = class(TForm)
    Messages: TEdit;
    Label1: TLabel;
    Text: TEdit;
    Label2: TLabel;
    Button1: TButton;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ChMailSlot.CreateLocalServer('TestMailSlot');
  Messages.Text:= ChMailSlot.LastError;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ChMailSlot.CloseServer;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Text.Text:=ChMailSlot.ReadMail;
  Messages.Text:= ChMailSlot.LastError;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
Var Mail: String;
begin
  Mail:= ChMailSlot.ReadMail;
  If Mail <> '' then begin
    Text.Text:= Mail;
    Messages.Text:= ChMailSlot.LastError;
  end;
end;

end.







Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Using Mailslot in Serice
    vijesh nair (Jul 16 2008 9:23AM)

Sir, thanks for your article.
It helps me to create  applications that communicate with Mail Slot.
But if fails when I try this in Communicating with NT service.
Please Help

Regards,
Vijesh V.Nair
Respond

Why not WinSock
    Bjarne Winkler (Nov 26 2001 2:00AM)

Hi
Why not use the WinSock it comes with Delphi and gives you more up today TCP/IP connection.  Yes you can also use it on a local connection.  For more information look at either the TPowerSock or the Delphi demo called Chat.

Bjarne \v/

Here are some articles I created for Delphi3000.com

Please cleanup your space!

ShortFileName, LongFileName, and Mince functions.

TCheckListBox LoadFromFile/SaveToFile Method, included checked state.

How to create an ActiveX and link it to JavaScript in an HTML document.

Why TFrames.


Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
E. Irigoyen
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)