unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Memo1: TMemo;
Timer1: TTimer;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
Edit3: TEdit;
StatusBar1: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
ServerSlot : THandle;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
slotName : PChar;
begin
slotName:=PChar(edit1.Text);
ServerSlot:=CreateMailSlot(slotName,0,MAILSLOT_WAIT_FOREVER,nil); if(ServerSlot=INVALID_HANDLE_VALUE)then
ShowMessage('创建邮槽失败!')
else
begin
StatusBar1.Panels[0].Text:='邮槽正常运行.';
Timer1.Enabled:=true;
end;
end;procedure TForm1.Timer1Timer(Sender: TObject);
var
lpNextSize,lpMsgCount, lpTimeout, cbRead : dword;
buffer : PChar;
msg : string;
begin
lpNextSize:=0;
lpMsgCount:=0;
lpTimeout:=0;
GetMailslotInfo(ServerSlot, nil, lpNextSize,@lpMsgCount, @lpTimeout);
if lpNextSize<>MAILSLOT_NO_MESSAGE then
begin
cbRead:=1;
try
Buffer:=PChar(GlobalAlloc(GPTR,lpNextSize+1));
buffer[0]:=#0;
ReadFile(ServerSlot, buffer, lpNextSize,cbRead,nil);
msg:=StrPas(buffer);
Memo1.Lines.Add(TimeToStr(Time)+':'+msg);
finally
GlobalFree(HGLOBAL(buffer));
end;
end;
end;procedure TForm1.Button3Click(Sender: TObject);
var
hFile : THandle;
cbWritten : dword;
hResult : boolean;
dsize : dword;
msg : string;
tmp:string;
begin
tmp:=edit2.Text;
hFile:=CreateFile(pchar(tmp),GENERIC_WRITE ,
FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
0);
if(hFile<>INVALID_HANDLE_VALUE)then
begin
dsize:=sizeof(edit3.Text)+1;
msg:=edit3.Text;
hResult:=WriteFile(hFile, msg,dsize,
cbWritten, nil);
if(hResult=false)then
begin
Memo1.Lines.Add('发送消息失败!');
StatusBar1.Panels[1].Text:='错误......';
end
else
begin
Memo1.Lines.Add('发送消息成功!');
StatusBar1.Panels[1].Text:='成功......';
end;
CloseHandle(hFile);
end;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Timer1.Enabled:=false;
CloseHandle(ServerSlot);
StatusBar1.Panels[0].Text:='邮槽被释放.';
close;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
timer1.Enabled:=false;
StatusBar1.Panels[0].Text:='等待邮槽创建.';
end;end.
开两个,exe文件时不能收到,而且给自己发信息有时也有问题!
大家帮忙改一下!
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Memo1: TMemo;
Timer1: TTimer;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
Edit3: TEdit;
StatusBar1: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
ServerSlot : THandle;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
slotName : PChar;
begin
slotName:=PChar(edit1.Text);
ServerSlot:=CreateMailSlot(slotName,0,MAILSLOT_WAIT_FOREVER,nil); if(ServerSlot=INVALID_HANDLE_VALUE)then
ShowMessage('创建邮槽失败!')
else
begin
StatusBar1.Panels[0].Text:='邮槽正常运行.';
Timer1.Enabled:=true;
end;
end;procedure TForm1.Timer1Timer(Sender: TObject);
var
lpNextSize,lpMsgCount, lpTimeout, cbRead : dword;
buffer : PChar;
msg : string;
begin
lpNextSize:=0;
lpMsgCount:=0;
lpTimeout:=0;
GetMailslotInfo(ServerSlot, nil, lpNextSize,@lpMsgCount, @lpTimeout);
if lpNextSize<>MAILSLOT_NO_MESSAGE then
begin
cbRead:=1;
try
Buffer:=PChar(GlobalAlloc(GPTR,lpNextSize+1));
buffer[0]:=#0;
ReadFile(ServerSlot, buffer, lpNextSize,cbRead,nil);
msg:=StrPas(buffer);
Memo1.Lines.Add(TimeToStr(Time)+':'+msg);
finally
GlobalFree(HGLOBAL(buffer));
end;
end;
end;procedure TForm1.Button3Click(Sender: TObject);
var
hFile : THandle;
cbWritten : dword;
hResult : boolean;
dsize : dword;
msg : string;
tmp:string;
begin
tmp:=edit2.Text;
hFile:=CreateFile(pchar(tmp),GENERIC_WRITE ,
FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
0);
if(hFile<>INVALID_HANDLE_VALUE)then
begin
dsize:=sizeof(edit3.Text)+1;
msg:=edit3.Text;
hResult:=WriteFile(hFile, msg,dsize,
cbWritten, nil);
if(hResult=false)then
begin
Memo1.Lines.Add('发送消息失败!');
StatusBar1.Panels[1].Text:='错误......';
end
else
begin
Memo1.Lines.Add('发送消息成功!');
StatusBar1.Panels[1].Text:='成功......';
end;
CloseHandle(hFile);
end;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Timer1.Enabled:=false;
CloseHandle(ServerSlot);
StatusBar1.Panels[0].Text:='邮槽被释放.';
close;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
timer1.Enabled:=false;
StatusBar1.Panels[0].Text:='等待邮槽创建.';
end;end.
开两个,exe文件时不能收到,而且给自己发信息有时也有问题!
大家帮忙改一下!
你可以说说程序运行到哪里出错了吗?
看我能否帮你