📄 pipe.cpp
字号:
/*** Disclaimer ******************************************************************/
/* */
/* Win32 Named Pipe Wrapper (C) Clemens Fischer 2001 */
/* */
/* THE CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, */
/* EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO WARRANTIES */
/* OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. */
/* IN NO EVENT WILL THE AUTHOR OR AUTHORS BE LIABLE TO YOU FOR ANY DAMAGES, */
/* INCLUDING INCIDENTAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF THE USE */
/* OF THE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. */
/* YOU ACKNOWLEDGE THAT YOU HAVE READ THIS LICENSE, UNDERSTAND IT AND AGREE */
/* TO BE BOUND BY ITS TERMS AS THE COMPLETE AND EXCLUSIVE STATEMENT OF */
/* THE AGREEMENT BETWEEN US, SUPERSEDING ANY PROPOSAL OR PRIOR AGREEMENT, */
/* ORAL OR WRITTEN, AND ANY OTHER COMMUNICATIONS BETWEEN US RELATING */
/* TO THE SUBJECT MATTER OF THIS LICENSE. */
/* */
/*********************************************************************************/
/*** History *********************************************************************/
/* */
/* 02-08-2001 Initial version. */
/* */
/*********************************************************************************/
#include <stdafx.h>
#include "Pipe.h"
/*** Constructor *****************************************************************/
/* */
/* Return Value: none */
/* */
/* Parameters: none */
/* */
/*********************************************************************************/
cPipe::cPipe ( void )
{
// Init
m_LastError = PIPE_ERROR_SUCCESS;
m_GUID = "{E91885F1-84E4-11d5-898D-0008C725AC74}"; // { 0xe91885f1, 0x84e4, 0x11d5, { 0x89, 0x8d, 0x0, 0x8, 0xc7, 0x25, 0xac, 0x74 } }
m_SubKey = "";
m_bCreateCalled = FALSE;
m_bCloseCalled = FALSE;
m_bPipeRegistered = FALSE;
m_hReadPipe = NULL;
m_hWritePipe = NULL;
m_hClientReadPipe = NULL;
m_hClientWritePipe = NULL;
m_hMutex = NULL;
m_bVaildThread = FALSE;
m_bRunThread = TRUE;
}
/*** Destructor ******************************************************************/
/* */
/* Return Value: none. */
/* */
/* Parameters: none. */
/* */
/*********************************************************************************/
cPipe::~cPipe ( void )
{
Close();
}
/*** Create **********************************************************************/
/* */
/* Return Value: true if successful. */
/* */
/* Parameters: PipeName CString containing the pipe name. */
/* dwDesiredAccess Specifies the type of access. */
/* bServerSide Specifies server/client side. */
/* CallBackFunc Pointer to callback functions. */
/* pCallBackParam Pointer to a single parameter value */
/* passed to the callback function. */
/* */
/* Description: Creates and registers the neccessary pipe(s). */
/* */
/*********************************************************************************/
BOOL cPipe::Create ( CString PipeName , DWORD dwDesiredAccess , BOOL bServerSide , PIPE_CALLBACK CallBackFunc , LPVOID pCallBackParam )
{
BOOL bCreationSucceeded;
SECURITY_ATTRIBUTES SecAttrib;
if ( m_bCreateCalled )
{
SetLastError ( PIPE_CREATE_ALREADY_CALLED );
// Create pipe failed
return FALSE;
}
m_PipeName = PipeName;
m_dwDesiredAccess = dwDesiredAccess;
m_bServerSide = bServerSide;
m_CallBackFunc = CallBackFunc;
m_pCallBackParam = pCallBackParam;
m_bCreateCalled = TRUE;
// Check if server side
if ( m_bServerSide )
{
if ( RegisterPipe() )
{
// Pipe registration succeeded
bCreationSucceeded = FALSE;
SecAttrib.nLength = sizeof ( SECURITY_ATTRIBUTES );
SecAttrib.lpSecurityDescriptor = NULL;
SecAttrib.bInheritHandle = TRUE;
if ( dwDesiredAccess & PIPE_READ )
{
bCreationSucceeded = ( CreatePipe ( &m_hClientReadPipe , &m_hWritePipe , &SecAttrib ,0 ) )?TRUE:FALSE;
}
if ( dwDesiredAccess & PIPE_WRITE )
{
bCreationSucceeded = ( CreatePipe ( &m_hReadPipe , &m_hClientWritePipe , &SecAttrib ,0 ) )?TRUE:FALSE;
}
if ( bCreationSucceeded && UpdateRegistration () && CallBack() )
{
return TRUE;
}
else
{
SetLastError ( PIPE_ERROR_CREATION_FAILED );
UnRegisterPipe ();
}
}
}
else
{
if ( RetrievePipe () && CallBack () )
{
return TRUE;
}
}
// Pipe creation failed
return FALSE;
}
/*** Close ***********************************************************************/
/* */
/* Return Value: none. */
/* */
/* Parameters: none. */
/* */
/* Description: Closes the pipe(s). */
/* */
/*********************************************************************************/
void cPipe::Close ( void )
{
if ( m_bCloseCalled ) return;
m_bCloseCalled = TRUE;
m_bRunThread = FALSE;
UnRegisterPipe ();
CloseHandle ( m_hClientWritePipe );
CloseHandle ( m_hClientReadPipe );
CloseHandle ( m_hWritePipe );
CloseHandle ( m_hReadPipe );
CloseHandle ( m_hMutex );
if ( m_bVaildThread ) *m_pbVaildPtr = FALSE;
}
/*** ReadPipe ********************************************************************/
/* */
/* Return Value: true if successful. */
/* */
/* Parameters: Buffer Pointer to the buffer that receives */
/* the data. */
/* nNumberOfBytesToRead Specifies the number of bytes to be */
/* read. */
/* lpNumberOfBytesRead Pointer to the variable that */
/* receives the number of bytes read. */
/* */
/* Description: Performs read operation on the pipe. */
/* */
/*********************************************************************************/
BOOL cPipe::ReadPipe ( LPVOID Buffer , DWORD nNumberOfBytesToRead , DWORD* lpNumberOfBytesRead )
{
if ( ReadFile ( m_hReadPipe , Buffer , nNumberOfBytesToRead , lpNumberOfBytesRead , NULL ) )
{
// Read pipe succeeded
return TRUE;
}
SetLastError ( PIPE_ERROR_READ_FAILED );
// Read pipe failed
return FALSE;
}
/*** WritePipe *******************************************************************/
/* */
/* Return Value: true if successful. */
/* */
/* Parameters: Buffer Pointer to the buffer containing the */
/* data to be written. */
/* nNumberOfBytesToWrite Specifies the number of bytes to */
/* write. */
/* */
/* Description: Performs write operation on the pipe. */
/* */
/*********************************************************************************/
BOOL cPipe::WritePipe ( LPVOID Buffer , DWORD nNumberOfBytesToWrite )
{
DWORD nNumberOfBytesWritten;
if ( WriteFile ( m_hWritePipe , Buffer , nNumberOfBytesToWrite , &nNumberOfBytesWritten , NULL ) )
{
if ( nNumberOfBytesWritten == nNumberOfBytesToWrite )
{
// Read write succeeded
return TRUE;
}
}
SetLastError ( PIPE_ERROR_WRITE_FAILED );
// Write pipe failed
return FALSE;
}
/*** GetLastError ****************************************************************/
/* */
/* Return Value: The return value is the last-error code value. */
/* */
/* Parameters: none. */
/* */
/* Description: Returns the latest cPipe error code. Inside cPipe call */
/* call ::GetLastError() to retrieve the calling thread's last */
/* error code value. */
/* */
/*********************************************************************************/
int cPipe::GetLastError ( void )
{
// Return latest error value
return m_LastError;
}
/*** RegisterPipe ****************************************************************/
/* */
/* Return Value: true if successful. */
/* */
/* Parameters: none. */
/* */
/*********************************************************************************/
BOOL cPipe::RegisterPipe ( void )
{
HKEY hRegKey;
DWORD dwDisposition;
DWORD dwValuePId;
DWORD dwValueDef;
DWORD dwDataLen;
HANDLE hProcess;
HANDLE hMutex;
// Create registry key
m_SubKey = "SOFTWARE\\" + m_GUID + "\\" + m_PipeName;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -