封装了一个 Windows 的 命名管道的类

本文介绍了一个使用C语言实现的命名管道类,包括创建、读取、写入和销毁管道的功能。详细展示了如何通过不同的模式(服务端或客户端)创建命名管道,并提供了关键方法的实现逻辑。

//CNamedPipe.h

#ifndef __CNAMEDPIPE_H__
#define __CNAMEDPIPE_H__

#define PIPE_UNDF -1
#define PIPE_SERV  0
#define PIPE_CLIN  1

class CNamedPipe
{
public:
    CNamedPipe();
    ~CNamedPipe();
    
    bool Create(char* szPipName, int nMode);
    bool Read(void* pBuf, int size, int& nRet);
    bool Write(void *pBuf, int size, int& nRet);
    void Destroy();
    int GetMode();

private:
    void* m_hPipe;
    int m_nMode;
};

#endif

//CNamedPipe.cpp

#include "CNamedPipe.h"

#include <stdio.h>
#include <string.h>
#include <windows.h>

const DWORD BUFSIZE = 1024;
const DWORD PIPE_TIMEOUT = 5000;

CNamedPipe::CNamedPipe()
{
    m_hPipe = INVALID_HANDLE_VALUE;
    m_nMode = PIPE_UNDF;
}

CNamedPipe::~CNamedPipe()
{

}

bool CNamedPipe::Create(char* szPipName, int nMode)
{
    bool bRet = false;
    char szTmp[64];
   
    sprintf(szTmp, "\\\\.\\pipe\\%s", szPipName);
    switch (nMode)
    {
    case PIPE_SERV:
        m_hPipe = CreateNamedPipe(szTmp,
            PIPE_ACCESS_DUPLEX,
            PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
            PIPE_UNLIMITED_INSTANCES,
            BUFSIZE,
            BUFSIZE,
            PIPE_TIMEOUT,
            NULL);
        
        if (m_hPipe == INVALID_HANDLE_VALUE)
        {
            bRet = false;
        }
        else
        {
            bRet = true;
        }
        break;

    case PIPE_CLIN:
        m_hPipe = CreateFile(szTmp,
            GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
        
        if(m_hPipe == INVALID_HANDLE_VALUE) 
        {
            bRet = false;
        }
        else
        {
            bRet = true;
        }
        break;

    default: bRet = false;
    }

    return bRet;
}


bool CNamedPipe::Read(void* pBuf, int nSize, int& nRet)
{
    if (ConnectNamedPipe(m_hPipe, NULL) || (GetLastError() == ERROR_PIPE_CONNECTED))
    {
        DWORD cbBytesRead;
        bool fSuccess = (0 != ReadFile(m_hPipe, pBuf, nSize, &cbBytesRead, NULL));
        
        ((char *)pBuf)[cbBytesRead] = '\0';       
        nRet = (int)cbBytesRead;
        if (!fSuccess || cbBytesRead == 0)
        {
            return false;
        }
        
    }
    return true;
}

bool CNamedPipe::Write(void* pBuf, int nSize, int& nRet)
{
    return (0 != WriteFile(m_hPipe, pBuf, nSize, (unsigned long *)&nRet, NULL));
}

void CNamedPipe::Destroy()
{
    DisconnectNamedPipe(m_hPipe);
    CloseHandle(m_hPipe);
    m_hPipe = INVALID_HANDLE_VALUE;
    m_nMode = PIPE_UNDF;
}

int CNamedPipe::GetMode()
{
    return m_nMode;
}


使用C++代码封装的win32操作, 与MFC相似,对于学习SDK与C++是巨好的参考 Tutorials Menu of tutorials Tutorial 1: The Simplest Window Tutorial 2: Using Classes and Inheritance Tutorial 3: Using Messages to Create a Scribble Window Tutorial 4: Repainting the Window Tutorial 5: Wrapping a Frame around our Scribble Window Tutorial 6: Customising Window Creation Tutorial 7: Customising the Toolbar Tutorial 8: Loading and Saving Files Tutorial 9: Printing Tutorial 10: Finishing Touches Tutorial 1: The Simplest Window The following code uses Win32++ to create a window. This is all the code you need (in combination with Win32++) to create and display a simple window. Note that in order to add the Win32++ code to our program, we use an #include statement as shown below. #include "../Win32++/Wincore.h" INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPTSTR, int) { //Start Win32++ CWinApp MyApp; //Create a CWnd object CWnd MyWindow; //Create (and display) the window MyWindow.Create(); //Run the application return MyApp.Run(); } This program has four key steps: Start Win32++. We do this here by creating a CWinApp object called MyApp. Create a CWnd object called MyWindow. Create a default window by calling the Create function. Start the message loop, by calling the Run function. If you compile and run this program, you'll find that the application doesn't end when the window is closed. This is behaviour is normal. An illustration of how to use messages to control the windows behaviour (including closing the application) will be left until tutorial 3.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值