Solved

Mailslot.... application Crashes ???

Posted on 2001-07-19
2
470 Views
Last Modified: 2008-02-01
Hi all !!

could someone try out this code and help me correct it - i don't know what i am doing wrong.

Please recreate my application and try it out

i would like to know what it is i am dooing wrong
and why the applications crashed... - so please explain me.... I dont know much about pointers

kim



unit Unit2;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    ListBox1: TListBox;
    Edit1: TEdit;
    procedure CreateSlot(Sender: TObject);
    procedure WriteSlot(Sender: TObject);
    procedure ReadSlot(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
//Giant = Array[0..$FFFF] of Byte;
var
  Form1: TForm1;
  hslot1 : Thandle;
implementation

{$R *.DFM}

procedure TForm1.CreateSlot(Sender: TObject);
Var
  Slotname : String;
begin
   Slotname:= '\\.\mailslot\kwaslot';
   hSlot1 := CreateMailslot(pchar(Slotname),0,MAILSLOT_WAIT_FOREVER, nil);
   if hslot1 = INVALID_HANDLE_VALUE
   then begin;
     listbox1.Items.Add('Invalid Handle' + inttostr(getlasterror));
   end
   else Listbox1.Items.add('Handle Createt SuccesFull')
end;

procedure TForm1.WriteSlot(Sender: TObject);
var
  Mes       : String;
  fResult   : Boolean;
  hfile     : Cardinal;
  cbWritten : Cardinal;
begin
// adding the text of the editbox2 make this example crash
Mes := edit1.text + edit1.text  ;
//it seems to work if i dont get the value from the editbox ???????
//mes :='dette er en test';
hFile := CreateFile('\\.\mailslot\kwaslot',
    GENERIC_WRITE,
    FILE_SHARE_READ,//* required to write to a mailslot */
     nil,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    0);

if hFile = INVALID_HANDLE_VALUE then Showmessage('Invalid Handle');

fResult := WriteFile(hFile,
    mes,
    length(mes) , //* include terminat. null char. */
    cbWritten,
     nil);

     if fresult then listbox1.Items.Add('Success Writing to Mailslot')
end;


procedure TForm1.ReadSlot(Sender: TObject);
var
  BUFFER:pointer;
  lnextsize:dword;
  simplebytes:dword;
  mes : string;

begin
  if Windows.GetMailslotInfo(hslot1,0,lnextsize,0,nil) then
  begin;
    if lNextSize <> MAILSLOT_NO_MESSAGE then
    begin
      GetMem(Buffer, lNextSize);
      if ReadFile(hslot1, Buffer^, lNextSize, SimpleBytes, nil) then
      begin
        showmessage(string(buffer^));
       end ;
       FreeMem(Buffer, lNextSize);
     end;
   end;
end;
end.
0
Comment
Question by:kwang080897
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 200 total points
ID: 6297799
A quick fix:


unit Unit2;

interface

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

type
 TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Edit1: TEdit;
    ListBox1: TListBox;
   procedure CreateSlot(Sender: TObject);
   procedure WriteSlot(Sender: TObject);
   procedure ReadSlot(Sender: TObject);
 private
   { Private declarations }
   a : array[0..255] of Char;  // make sure this buffer is large enough
 public
   { Public declarations }
 end;
//Giant = Array[0..$FFFF] of Byte;
var
 Form1: TForm1;
 hslot1 : Thandle;
implementation

{$R *.DFM}

procedure TForm1.CreateSlot(Sender: TObject);
Var
 Slotname : String;
begin
  Slotname:= '\\.\mailslot\kwaslot';
  hSlot1 := CreateMailslot(pchar(Slotname),0,MAILSLOT_WAIT_FOREVER, nil);
  if hslot1 = INVALID_HANDLE_VALUE
  then begin;
    listbox1.Items.Add('Invalid Handle' + inttostr(getlasterror));
  end
  else Listbox1.Items.add('Handle Createt SuccesFull')
end;

procedure TForm1.WriteSlot(Sender: TObject);
var
 Mes       : String;
 fResult   : Boolean;
 hfile     : Cardinal;
 cbWritten : Cardinal;
begin
// adding the text of the editbox2 make this example crash
Mes := edit1.text + edit1.text  ;
//it seems to work if i dont get the value from the editbox ???????
//mes :='dette er en test';
hFile := CreateFile('\\.\mailslot\kwaslot',
   GENERIC_WRITE,
   FILE_SHARE_READ,//* required to write to a mailslot */
    nil,
   OPEN_EXISTING,
   FILE_ATTRIBUTE_NORMAL,
   0);

if hFile = INVALID_HANDLE_VALUE then Showmessage('Invalid Handle');

StrPCopy(a, mes);
fResult := WriteFile(hFile,
   a,
   Length(a),   //* include terminat. null char. */
   cbWritten,
    nil);

    if fresult then listbox1.Items.Add('Success Writing to Mailslot')
end;


procedure TForm1.ReadSlot(Sender: TObject);
var
 BUFFER: Pointer;
 lnextsize:dword;
 simplebytes:dword;
 mes : string;

begin
 if Windows.GetMailslotInfo(hslot1,nil,lnextsize,nil,nil) then
 begin;
   if lNextSize <> MAILSLOT_NO_MESSAGE then
   begin
     GetMem(Buffer, lNextSize);
     if ReadFile(hslot1, Buffer^, lNextSize, SimpleBytes, nil) then
     begin
       showmessage(PChar(buffer));
      end ;
      FreeMem(Buffer, lNextSize);
    end;
  end;
end;
end.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6297989
Hi, I forgot to change back

Length(a)

to

Length(mes) + 1
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question