内容发布更新时间 : 2024/12/24 9:33:53星期一 下面是文章的全部内容请认真阅读。
if(!ReadFile(hRead,buf,100,&dwRead,NULL)) {
MessageBox(\读取数据失败!\return; }
MessageBox(buf); }
void CParentView::OnPipeWrite() 菜单“写入数据”代码 {
// TOD Add your command handler code here char buf[]=\DWORD dwWrite;
if(!WriteFile(hWrite,buf,strlen(buf)+1,&dwWrite,NULL)) {
MessageBox(\写入数据失败!\return; } }
d.再建一个Child的单文档,在View中增加两个成员hRead和hWrite.在OnInitialUpdate()中得到句柄的值。
void CChildView::OnInitialUpdate() {
CView::OnInitialUpdate();
// TOD Add your specialized code here and/or call the base class hRead=GetStdHandle(STD_INPUT_HANDLE);注意这句代码! hWrite=GetStdHandle(STD_OUTPUT_HANDLE); }
e.加菜单“读取数据”“写入数据”其代码如下: void CChildView::OnPipeRead() {
// TOD Add your command handler code here char buf[100]; DWORD dwRead;
if(!ReadFile(hRead,buf,100,&dwRead,NULL)) {
MessageBox(\读取数据失败!\return; }
MessageBox(buf); }
void CChildView::OnPipeWrite() {
// TOD Add your command handler code here char buf[]=\匿名管道测试程序\DWORD dwWrite;
if(!WriteFile(hWrite,buf,strlen(buf)+1,&dwWrite,NULL)) {
MessageBox(\写入数据失败!\return; } }
3.命名管道:还可以跨网络通信,服务器只能在win2000和NT下运行!而客户端可以在95下运行。关键函数CreateNamedPipe
a.先建一个NamedPipeSRV单文档应用程序,加菜单“创建管道”“读取数据”“写入数据” b.在View中增加Handle变量hPipe,注意在析构函数中释放它! c.响应菜单,创建命名管道
void CNamedPipeSrvView::OnPipeCreate() {
// TOD Add your command handler code here hPipe=CreateNamedPipe(\
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, 0,1,1024,1024,0,NULL);
if(INVALID_HANDLE_VALUE==hPipe) {
MessageBox(\创建命名管道失败!\hPipe=NULL; return; }
HANDLE hEvent;
hEvent=CreateEvent(NULL,TRUE,FALSE,NULL); if(!hEvent) {
MessageBox(\创建事件对象失败!\CloseHandle(hPipe); hPipe=NULL; return;
}
OVERLAPPED ovlap;
ZeroMemory(&ovlap,sizeof(OVERLAPPED)); ovlap.hEvent=hEvent;
if(!ConnectNamedPipe(hPipe,&ovlap)) {
if(ERROR_IO_PENDING!=GetLastError()) {
MessageBox(\等待客户端连接失败!\CloseHandle(hPipe); CloseHandle(hEvent); hPipe=NULL; return; } }
if(WAIT_FAILED==WaitForSingleObject(hEvent,INFINITE)) {
MessageBox(\等待对象失败!\CloseHandle(hPipe); CloseHandle(hEvent); hPipe=NULL; return; }
CloseHandle(hEvent); }
void CNamedPipeSrvView::OnPipeRead() {
// TOD Add your command handler code here char buf[100]; DWORD dwRead;
if(!ReadFile(hPipe,buf,100,&dwRead,NULL)) {
MessageBox(\读取数据失败!\return; }
MessageBox(buf); }
void CNamedPipeSrvView::OnPipeWrite()
{
// TOD Add your command handler code here char buf[]=\DWORD dwWrite;
if(!WriteFile(hPipe,buf,strlen(buf)+1,&dwWrite,NULL)) {
MessageBox(\写入数据失败!\return; } }
d.再建一个NamedPipeCLT单文档工程,加菜单“连接管道”“读取数据”“写入数据”,当然别忘记成员变量hPipe的定义和初始化 e.响应菜单代码
void CNamedPipeCltView::OnPipeConnect() 连接管道 {
// TOD Add your command handler code here
if(!WaitNamedPipe(\{
MessageBox(\当前没有可利用的命名管道实例!\return; }
hPipe=CreateFile(\| GENERIC_WRITE, 0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if(INVALID_HANDLE_VALUE==hPipe) {
MessageBox(\打开命名管道失败!\hPipe=NULL; return; } }
void CNamedPipeCltView::OnPipeRead() 读取数据 {
// TOD Add your command handler code here char buf[100]; DWORD dwRead;
if(!ReadFile(hPipe,buf,100,&dwRead,NULL)) {
MessageBox(\读取数据失败!\
return; }
MessageBox(buf); }
void CNamedPipeCltView::OnPipeWrite() 写入数据 {
// TOD Add your command handler code here char buf[]=\命名管道测试程序\DWORD dwWrite;
if(!WriteFile(hPipe,buf,strlen(buf)+1,&dwWrite,NULL)) {
MessageBox(\写入数据失败!\return; } }
老孙的教学笔记续(转) -|落魂 发表于 2006-4-25 18:28:00
4.邮槽,使用时应将消息长度限制在424字节以下,关键函数CreateMailSlot() a.先建一个MailSlotSRV工程,加菜单“接收数据” b.消息响应代码:
void CMailslotSrvView::OnMailslotRecv() 菜单“接收数据”的代码 {
// TOD Add your command handler code here HANDLE hMailslot;
hMailslot=CreateMailslot(\MAILSLOT_WAIT_FOREVER,NULL); if(INVALID_HANDLE_VALUE==hMailslot) {
MessageBox(\创建油槽失败!\return; }
char buf[100]; DWORD dwRead;
if(!ReadFile(hMailslot,buf,100,&dwRead,NULL)) {
MessageBox(\读取数据失败!\