createnamedpipeevent.cpp

来自「IO函数调用测试」· C++ 代码 · 共 232 行

CPP
232
字号
#include "stdafx.h"
#include "idcombo.h"
#include "IOExplorer.h"
#include "TraceEvent.h"
  #include "CreateNamedPipeEvent.h"
#include "orstring.h"
#include "strings.h"


/****************************************************************************
*                       CreateNamedPipeEvent::getPipeModeString
* Inputs:
*       DWORD pipemode
* Result: CString
*       Text of share mode
* Effect: 
*       Constructs a string
****************************************************************************/

static IDData pipemodes[] = {
	{IDS_PIPE_NOWAIT, PIPE_NOWAIT},
	{IDS_PIPE_READMODE_MESSAGE, PIPE_READMODE_MESSAGE},
	{IDS_PIPE_TYPE_MESSAGE, PIPE_TYPE_MESSAGE},
	{0, 0}};

CString CreateNamedPipeEvent::getPipeModeString(DWORD pipemode)
    {
     CString bits = CORString(pipemodes, pipemode);

     // That takes care of all the 1-bits, but some of the constants are
     // zero-bits values.  We have to hack those in here...

     CORString::ORzero(bits, pipemode, PIPE_NOWAIT, IDS_PIPE_WAIT);
     CORString::ORzero(bits, pipemode, PIPE_READMODE_MESSAGE, IDS_PIPE_READMODE_BYTE);
     CORString::ORzero(bits, pipemode, PIPE_TYPE_MESSAGE, IDS_PIPE_TYPE_BYTE);

     return bits;
    }


/****************************************************************************
*                     CreateNamedPipeEvent::getMaxInstancesString
* Inputs:
*       DWORD maxinstances
* Result: CString
*       %d formatting or PIPE_UNLIMITED_INSTANCES
* Effect: 
*       Constructs the appropriate display string
****************************************************************************/

CString CreateNamedPipeEvent::getMaxInstancesString(DWORD maxinstances)
    {
     CString s;
     if(maxinstances == PIPE_UNLIMITED_INSTANCES)
	s.LoadString(IDS_PIPE_UNLIMITED_INSTANCES);
     else
	s.Format(_T("%d"), maxinstances);
     return s;
    }

/****************************************************************************
*                 CreateNamedPipeEvent::getOpenModeString
* Inputs:
*       DWORD flags
* Result: CString
*       String representing the open mode
* Effect: 
*       Creates a string giving flags and attributes
****************************************************************************/

static IDData pipeaccess[] = {
	{IDS_PIPE_ACCESS_DUPLEX, PIPE_ACCESS_DUPLEX},
	{IDS_PIPE_ACCESS_INBOUND, PIPE_ACCESS_INBOUND},
	{IDS_PIPE_ACCESS_OUTBOUND, PIPE_ACCESS_OUTBOUND},
	{0, 0} // EOT
			     };

static IDData openmodes[] = {
	{IDS_FILE_FLAG_WRITE_THROUGH, FILE_FLAG_WRITE_THROUGH},
	{IDS_FILE_FLAG_OVERLAPPED, FILE_FLAG_OVERLAPPED},
	{IDS_WRITE_DAC, WRITE_DAC},
	{IDS_WRITE_OWNER, WRITE_OWNER},
        {IDS_ACCESS_SYSTEM_SECURITY, ACCESS_SYSTEM_SECURITY},
	{0, 0} // EOT
			};

#define PIPE_ACCESS_MASK (PIPE_ACCESS_DUPLEX | PIPE_ACCESS_INBOUND | PIPE_ACCESS_OUTBOUND)

CString CreateNamedPipeEvent::getOpenModeString(DWORD openmode)
    {
     CString pipeaccessString;

     for(int i = 0; pipeaccess[i].id != 0; i++)
        { /* pipe access */
	 if(pipeaccess[i].val == (openmode & PIPE_ACCESS_MASK))
	    { /* found it */
	     pipeaccessString.LoadString(pipeaccess[i].id);
	     break;
	    } /* found it */
	} /* pipe access */

     CString flagstring = CORString(openmodes, openmode);
     CORString::OR(pipeaccessString, flagstring);

     return pipeaccessString;
    }

/****************************************************************************
*			      CreateNamedPipeEvent::display
* Result: CString
*       A display string for CreateFile operations
* Effect: 
*       Creates the string
****************************************************************************/

CString CreateNamedPipeEvent::display()
    {
     CString result;

     CString openmode = getOpenModeString(OpenMode);
     CString pipemode = getPipeModeString(PipeMode);
     CString maxinstances = getMaxInstancesString(MaxInstances);

     CString security;
     CString sa;
     
     if(this->inherit)
        { /* make security */
	 CString fmt;
	 security.LoadString(IDS_SA_FMT);
	 // SECURITY_ATTRIBUTES sa = { sizeof(sa), NULL, TRUE};\n
	 sa.LoadString(IDS_SA);
	} /* make security */
     else
        { /* no security */
	 sa.LoadString(IDS_NULL);
	} /* no security */

     CString filename = quoteEscape(FileName);

     result.Format(_T("%sCreateNamedPipe( \"%s\", %s, %s, %s, %d, %d, %d, %s)"), 
     			security,
     			(LPCTSTR)filename, 
     			(LPCTSTR)openmode,
			(LPCTSTR)pipemode,
			maxinstances,
			OutBufferSize,
			InBufferSize,
			DefaultTimeout,
			(LPCTSTR)sa);

     return result;
     
    }

/****************************************************************************
*                          CreateNamedPipeEvent::execute
* Result: LRESULT
*       (LRESULT)CreateFile(...)
* Effect: 
*       Creates a file.  Returns the handle or NULL if error
****************************************************************************/

LRESULT CreateNamedPipeEvent::execute()
    {
     HANDLE h = NULL;
     SECURITY_ATTRIBUTES sa = {sizeof(sa), NULL, TRUE};
     CWaitCursor wait;

     h = CreateNamedPipe(this->FileName,
     		    this->OpenMode,
		    this->PipeMode,
		    this->MaxInstances,
		    this->OutBufferSize,
		    this->InBufferSize,
		    this->DefaultTimeout,
		    (this->inherit ? &sa : NULL));

     result = (LRESULT)h;  // save in TraceEvent standard place

     return result;
    }

/****************************************************************************
*                       CreateNamedPipeEvent::display_result
* Result: CString
*       Displayable result based on result field
*		result			string
*		----------------------------------------------
*		NULL			""
*		INVALID_HANDLE_VALUE	"INVALID_HANDLE_VALUE"
*		other			%08x
* Effect: 
*       Constructs a result
****************************************************************************/

CString CreateNamedPipeEvent::display_result()
    {
     if(result == NULL)
	return CString(_T(""));
     CString s;
     if((HANDLE)result == INVALID_HANDLE_VALUE)
	s.LoadString(IDS_INVALID_HANDLE_VALUE);
     else
	s.Format(_T("%08x"), result);
     return s;
    }


/****************************************************************************
*                         CreateNamedPipeEvent::copy
* Result: TraceEvent *
*       A complete copy of the named pipe event
* Effect: 
*       Creates a new named pipe event
****************************************************************************/

TraceEvent * CreateNamedPipeEvent::copy()
    {
     CreateNamedPipeEvent * e = new CreateNamedPipeEvent;
     e->FileName = FileName;
     e->OpenMode = OpenMode;
     e->PipeMode = PipeMode;
     e->MaxInstances = MaxInstances;
     e->OutBufferSize = OutBufferSize;
     e->InBufferSize = InBufferSize;
     e->DefaultTimeout = DefaultTimeout;
     e->inherit = inherit;
     copyTraceData(e);
     return e;
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?