Delphi 简单命名管道在两个进程间通讯
服务器端代码:
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; const
WM_MYMSG=WM_USER+; 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[..] 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,都会让客户端程序报 错误-管道的另一端上无任何进程。
//在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,,); if DisconnectNamedPipe(pipeHandle)=FALSE then
begin
ShowMessage(format('DisconnectNamedPipe failed with error %d',[GetLastError()]));
Application.Destroy;
end; CloseHandle(pipeHandle);
Result:=; end; constructor TForm1.Create(AOwner: TComponent);
begin
inherited;
pipeHandle:= CreateNamedPipe('\\.\Pipe\Jim',PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE or PIPE_READMODE_BYTE,,,,,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,,@fun,Ptr(form1.Handle),,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,); 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.
Delphi 简单命名管道在两个进程间通讯的更多相关文章
- Windows进程间通讯(IPC)----管道
管道的分类 管道其实际就是一段共享内存,只不过Windows规定需要使用I/O的形式类访问这块共享内存,管道可以分为匿名管道和命名管道. 匿名管道就是没有名字的管道,其支持单向传输数据,如果需要双向传 ...
- Android AIDL 进行进程间通讯(IPC)
编写AIDL文件时,需要注意: 1.接口名和aidl文件名相同. 2.接口和方法前不用加访问权限修饰符 (public.private.protected等,也不能用final.static). 3. ...
- Unix系统中,两个进程间的通信
进程之间通常需要进行数据的传输或者共享资源等,因此进程间需要通讯. 可以通过管道,信号,消息队列,共享内存,信号量和套接字等方式 FIFO表示命名管道,这种管道的操作是基于先进先出原理. PIPE 表 ...
- 管道实现进程间通讯 、WaitNamedPipe
一.管道实现进程间通讯 主要的理论知识 1.什么是管道以及分类 管道是两个头的东西,每一个头各连接一个进程或者同一个进程的不同代码,依照管道的类别分有两种管道,匿名的和命名的:依照管道的传输方向分也能 ...
- C# 调用Windows API实现两个进程间的通信
使用Windows API实现两个进程间(含窗体)的通信http://blog.csdn.net/huangxinfeng/article/details/5513608 从C#下使用WM_COPYD ...
- C#进程间通讯或同步的框架引荐
这篇文章主要介绍了一个进程间通讯同步的C#框架,代码具有相当的稳定性和可维护性,随着.NET的开源也会被注入更多活力,推荐!需要的朋友可以参考下 0.背景简介 微软在 .NET 框架中提供了多种实用 ...
- High Performance Networking in Google Chrome 进程间通讯(IPC) 多进程资源加载
小结: 1. 小文件存储于一个文件中: 在内部,磁盘缓存(disk cache)实现了它自己的一组数据结构, 它们被存储在一个单独的缓存目录里.其中有索引文件(在浏览器启动时加载到内存中),数据文件( ...
- 服务 进程间通讯 IPC AIDL Parcelable 简介
1.IBinder和Binder是什么鬼? 我们来看看官方文档怎么说: 中文翻译: IBinder是远程对象的基本接口,是为了高性能而设计的轻量级远程调用机制的核心部分. 但他不仅用于远程调用,也用 ...
- C#进程间通讯技术-整理。
原文:C#进程间通讯技术-整理. 扩展阅读:http://www.cnblogs.com/joye-shen/archive/2012/06/16/2551864.html 一.进程间通讯的方式 1) ...
随机推荐
- Expm 1_2 实现快速排序的算法,并尝试采用不同的方法实现线性的划分过程.
package org.xiu68.exp.exp1; public class Exp1_2 { //实现快速排序算法,采用不同的方法实现线性划分的过程 public static void mai ...
- ANN算法总结
kd-tree kd-tree works poorly in high dimensions (k<30) 自己实验的时候差不多20到30左右吧,超过之后,就真的很慢了 faiss suppo ...
- 【mysql】一个很小但很影响速度的地方
如果要插入一大批数据,千万不要一条一条的execute, commit.而应该是先全部execute,最后统一commit!!! 千万注意,时间差距还是很大的!! 正确示范:快 ): sql = &q ...
- java 格式化
一. 可以之际像c语言一样用System.out.printf()格式化输出 二. System.out.format 1. format()方法模仿自printf(), 可用于PrintStream ...
- poj2763树链剖分边权+区间和
自己写的比原来的板子常数小了不少嘻嘻,边权处理起来比点权要复杂一下 由于根节点没有被映射的边,其被访问到的顺序是0,直接排除在线段树外 #include<iostream> #includ ...
- 性能测试十七:liunx下jmeter结果报表、html报表
控制台日志,只能看出整体的数据,若只是测单接口,这个最好用,但若有多个接口时,无法分别展示,所以需要换另外一种报表 四种方式来获取Jmeter的结果报表 一.在GUI模式下跑Jmeter的脚本,用tp ...
- Oracle学习笔记--第2章 oracle 数据库体系结构
第2章 oracle 数据库体系结构 目录: ————————————— 2.1物理存储结构 2.1.1数据文件 2.2.2控制文件 2.1.3重做日志文件 2.1.4其他文件 2.2逻辑存储结构 2 ...
- 《转》String,StringBuffer与StringBuilder的区别??
String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主要性能 ...
- hdu 1027 输出第m个全排列(next_permutation)
Sample Input6 4 //输出第4个全排列11 8 Sample Output1 2 3 5 6 41 2 3 4 5 6 7 9 8 11 10 # include <cstdio& ...
- LYK loves graph(graph)
题目: LYK loves graph(graph) Time Limit:2000ms Memory Limit:128MB LYK喜欢花花绿绿的图片,有一天它得到了一张彩色图片,这张图片可以看 ...