Solved

Mailslot.... application Crashes ???

Posted on 2001-07-19
2
461 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
  • 2
2 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 200 total points
Comment Utility
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
Comment Utility
Hi, I forgot to change back

Length(a)

to

Length(mes) + 1
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now