Solved

Mailslot.... application Crashes ???

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help on project with Soap 10 61
Correct Component for Shopping Cart. 2 120
Run video youtube webbrowse 10 73
Delphi, TImage, Png 2 17
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 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