Solved

Mailslot.... application Crashes ???

Posted on 2001-07-19
2
465 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 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