下面是网上找的一个事例,但在本机报错.请教下错误原因,或者给另外的事例更好!谢谢
//Server
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TServer = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Server: TServer;
const
pipename:string='\\.\pipe\Server';
implementation{$R *.dfm}procedure TServer.Button1Click(Sender: TObject); var
SPipeHandle:THandle;
Se:TSecurityAttributes;
WriteBuffer:DWORD;
Buffer:pchar;
begin
Se.nLength:=Sizeof(TSecurityAttributes);
Se.lpSecurityDescriptor:=nil;
Se.bInheritHandle:=True;
SPipeHandle:=CreateNamedPipe(pchar(pipename),PIPE_ACCESS_OUTBOUND OR FILE_FLAG_WRITE_THROUGH, PIPE_TYPE_BYTE or PIPE_WAIT,2,512,0,1000,@Se);//此处建立并放回句柄
if SPipeHandle=-1 then
raise Exception.Create('Create pipe Failed');
try
if not ConnectNamedPipe(SPipeHandle,nil) then//此处会判断连接出错 begin
CloseHandle(SPipeHandle);
Raise Exception.Create(IntToStr(GetLastError)+'fail con');
end;
Buffer:=StrAlloc(512);
Buffer:=Pchar(Memo1.Text);
WriteFile(SPipeHandle,Buffer,512,WriteBuffer,nil);
finally
DisConnectNamedPipe(SPipeHandle);
CloseHandle(SPipeHandle);
end;
end;end.
//client
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TClient = class(TForm)
Memo1: TMemo;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations } end;var
Client: TClient;
Buffer:array[0..511] of char;
ReadSize:DWORD;
hh:Thandle;
const
PipeName:string='\\.\pipe\Server';
implementation{$R *.dfm}
procedure TClient.Timer1Timer(Sender: TObject);
begin
if WaitNamedPipe(pchar(PipeName),NMPWAIT_USE_DEFAULT_WAIT) then
begin // raise Exception.Create('failed wait');
hh:=CreateFile(pchar(pipename),GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE,NiL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE or FILE_FLAG_WRITE_THROUGH,0);
if hh=INVALID_HANDLE_VALUE then
showmessage('error createfile')
else
begin
readsize:=0;
fillchar(buffer,512,0);
readfile(hh,buffer,512,readsize,nil);
if readsize >0 then
Memo1.Text:=Buffer;
end;
end;
end;end.
//Server
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TServer = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Server: TServer;
const
pipename:string='\\.\pipe\Server';
implementation{$R *.dfm}procedure TServer.Button1Click(Sender: TObject); var
SPipeHandle:THandle;
Se:TSecurityAttributes;
WriteBuffer:DWORD;
Buffer:pchar;
begin
Se.nLength:=Sizeof(TSecurityAttributes);
Se.lpSecurityDescriptor:=nil;
Se.bInheritHandle:=True;
SPipeHandle:=CreateNamedPipe(pchar(pipename),PIPE_ACCESS_OUTBOUND OR FILE_FLAG_WRITE_THROUGH, PIPE_TYPE_BYTE or PIPE_WAIT,2,512,0,1000,@Se);//此处建立并放回句柄
if SPipeHandle=-1 then
raise Exception.Create('Create pipe Failed');
try
if not ConnectNamedPipe(SPipeHandle,nil) then//此处会判断连接出错 begin
CloseHandle(SPipeHandle);
Raise Exception.Create(IntToStr(GetLastError)+'fail con');
end;
Buffer:=StrAlloc(512);
Buffer:=Pchar(Memo1.Text);
WriteFile(SPipeHandle,Buffer,512,WriteBuffer,nil);
finally
DisConnectNamedPipe(SPipeHandle);
CloseHandle(SPipeHandle);
end;
end;end.
//client
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TClient = class(TForm)
Memo1: TMemo;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations } end;var
Client: TClient;
Buffer:array[0..511] of char;
ReadSize:DWORD;
hh:Thandle;
const
PipeName:string='\\.\pipe\Server';
implementation{$R *.dfm}
procedure TClient.Timer1Timer(Sender: TObject);
begin
if WaitNamedPipe(pchar(PipeName),NMPWAIT_USE_DEFAULT_WAIT) then
begin // raise Exception.Create('failed wait');
hh:=CreateFile(pchar(pipename),GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE,NiL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE or FILE_FLAG_WRITE_THROUGH,0);
if hh=INVALID_HANDLE_VALUE then
showmessage('error createfile')
else
begin
readsize:=0;
fillchar(buffer,512,0);
readfile(hh,buffer,512,readsize,nil);
if readsize >0 then
Memo1.Text:=Buffer;
end;
end;
end;end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货