打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
进程之间管道通信
Delphi 简单命名管道在两个进程间通


服务器端代码:
----------------------------------------------------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
WM_MYMSG=WM_USER 1024;
type
TForm1 = class(TForm)
Memo1: TMemo;
private
{ Private declarations }
public
constructor Create(AOwner: TComponent); override;
procedure display(var msg:TMessage);message WM_MYMSG;
{ Public declarations }
end;
var
Form1: TForm1;

implementation
var
pipeHandle:HWND;
byteRead:DWORD;
buffer:array[0..255] of char;
threadId:Cardinal;
threadHandle:HWND;
{$R *.dfm}
{ TForm1 }
function fun(p:Pointer):DWORD;stdcall;
begin
if ConnectNamedPipe(pipeHandle,nil)=FALSE then
begin
ShowMessage(format('ConnectNamedPipe failed with error %d',[GetLastError()]));
Application.Destroy;
end;
//注意,下面的 ReadFile中,其buffer只能用字符数组
//无论是string,或者pchar,都会让客户端程序报 233错误-管道的另一端上无任何进程。
//在msdn中 ReadFile中的buffer是个LPVOID,delphi中则是一个var(引用)参数
//这个问题目前暂时无解决办法,是一个值得深入研究的话题。
if ReadFile(pipeHandle,buffer,sizeof(buffer),byteRead,nil)=FALSE then
begin
ShowMessage(format('ReadFile failed with error %d',[GetLastError()]));
Application.Destroy;
end;
SendMessage(integer(p),WM_MYMSG,1,1);
if DisconnectNamedPipe(pipeHandle)=FALSE then
begin
ShowMessage(format('DisconnectNamedPipe failed with error %d',[GetLastError()]));
Application.Destroy;
end;
CloseHandle(pipeHandle);
Result:=0;
end;
constructor TForm1.Create(AOwner: TComponent);
begin
inherited;
pipeHandle:= CreateNamedPipe('\\.\Pipe\Jim',PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE or PIPE_READMODE_BYTE,1,0,0,1000,nil);
if pipeHandle=INVALID_HANDLE_VALUE then
begin
ShowMessage(format('CreateNamePipe failed with error %d',[GetLastError()]));
Application.Destroy;
end;
memo1.Clear;
memo1.Lines.Add('Server is now running');
threadHandle:=createThread(nil,0,@fun,Ptr(form1.Handle),0,threadId);

end;
procedure TForm1.display(var msg: TMessage);
begin
memo1.Text:=buffer;
end;
end.
客户端代码:
----------------------------------------------------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
constructor Create(AOwner: TComponent); override;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var
pipeHandle:HWND;
bytesWrite:DWORD;
pipeNameStr:string;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if WaitNamedPipe(pchar(pipeNameStr),NMPWAIT_WAIT_FOREVER)=FALSE then
begin
ShowMessage(format('WaitNamedPipe faile with error %d',[GetLastError()]));
exit;
end;
pipeHandle:= CreateFile(pchar(pipeNameStr),GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_WRITE,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if pipeHandle=INVALID_HANDLE_VALUE then
begin
ShowMessage(format('CreateFile faile with error %d',[GetLastError()]));
exit;
end;
if WriteFile(pipeHandle,pchar(memo1.text)^,length(memo1.text),bytesWrite,nil)=
FALSE then
begin
ShowMessage(format('WriteFile faile with error %d',[GetLastError()]));
exit;
end;
ShowMessage(format('write %d Bytes',[bytesWrite]));
CloseHandle(pipeHandle);
end;
constructor TForm1.Create(AOwner: TComponent);
begin
inherited;
memo1.Clear;
memo1.Text:='猪悟能到此一游,通过命名管道!hello';
pipeNameStr:='\\.\Pipe\Jim';
end;
end.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
TClientDataSet[2]: Data、XMLData
delphi十个小技巧
delphi基础开发技巧
获得及设置本机的ip地址,子网掩码,网关,dns服务器信息
delphi 递归遍历TreeView树节点
delphi中当月第一天最后一天的函数等函数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服