Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Mailslot.... application Crashes ???

Posted on 2001-07-19
2
Medium Priority
?
480 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 800 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

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…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

886 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