Click here to Skip to main content
15,895,557 members
Articles / Programming Languages / C++

GUI-Based RunAsEx

Rate me:
Please Sign up or sign in to vote.
4.97/5 (61 votes)
24 Oct 2006CPOL42 min read 401.3K   10.8K   200  
An ultimate tool that lets you RunAs... (With support for non-Pwd, WTS, fake privilege, fake user groups, etc...)
/******************************************************************************
Module:  AutoBuf.h
Notices: Copyright (c) 2000 Jeffrey Richter
Purpose: This class manages an auto-sizing data buffer.
         See Appendix B.
******************************************************************************/


#pragma once   // Include this header file once per compilation unit


///////////////////////////////////////////////////////////////////////////////


#include "CmnHdr.h"              // See Appendix A.


/////////////////// CAutoBuf Template C++ Class Description ///////////////////


/*
The CAutoBuf template C++ class implements type safe buffers that
automatically grow to meet the needs of your code.  Memory is also
automatically freed when the object is destroyed (typically when your
code goes out of frame and it is popped off of the stack).

Examples of use:

   // Create a buffer with no explicit data type, 
   // the buffer grown in increments of a byte
   CAutoBuf<PVOID> buf;

   // Create a buffer of TCHARs, 
   // the buffer grows in increments of sizeof(TCHAR)
   CAutoBuf<PTSTR, sizeof(TCHAR)> buf; 

   // Force the buffer to be 10 bytes big
   buf = 10;

*/


///////////////////////////////////////////////////////////////////////////////


// This class is only ever used as a base class of the CAutoBuf template class.
// The base class exists so that all instances of the template class share
// a single instance of the common code.
class CAutoBufBase {
public:
   UINT  Size() { return(* (PDWORD) PSize()); }
   UINT  Size(UINT uSize);
   
   PUINT PSize() { 
      AdjustBuffer(); 
      m_uNewSize = m_uCurrentSize; 
      return(&m_uNewSize); 
   }
   void  Free() { Reconstruct(); }
   
protected:
   CAutoBufBase(PBYTE *ppbData, int nMult) {
      m_nMult = nMult;
      m_ppbBuffer = ppbData; // Derived class holds address of buffer to allow
                             // debugger's Quick Watch to work with typed data.
      Reconstruct(TRUE);
   }

   virtual ~CAutoBufBase() { Free(); }

   void Reconstruct(BOOL fFirstTime = FALSE);

   PBYTE Buffer() { 
      AdjustBuffer(); 
      return(*m_ppbBuffer); 
   }

private:
   void AdjustBuffer();   

private:
   PBYTE* m_ppbBuffer;    // Address of address of data buffer
   int    m_nMult;        // Multiplier (in bytes) used for buffer growth
   UINT   m_uNewSize;     // Requested buffer size (in m_nMult units)
   UINT   m_uCurrentSize; // Actual size (in m_nMult units)
};


///////////////////////////////////////////////////////////////////////////////


template <class TYPE, int MULT = 1> 
class CAutoBuf : private CAutoBufBase {
public:
   CAutoBuf() : CAutoBufBase((PBYTE*) &m_pData, MULT) {}
   void Free() { CAutoBufBase::Free(); }

public:
   operator TYPE*()  { return(Buffer()); }
   
   UINT operator=(UINT uSize) { return(CAutoBufBase::Size(uSize)); }
   operator UINT()   { return( Size()); }
   operator ULONG()  { return( Size()); }

   operator PUINT()  { return( PSize()); }
   operator PLONG()  { return((PLONG) PSize()); }
   operator PULONG() { return((PULONG) PSize()); }

   operator PBYTE()  { return((PBYTE) Buffer()); }
   operator PVOID()  { return((PVOID) Buffer()); }

   TYPE& operator[](int nIndex) { return(*(Buffer() + nIndex)); }

private:
   TYPE* Buffer() { return((TYPE*) CAutoBufBase::Buffer()); }

private:
   TYPE* m_pData;
};


///////////////////////////////////////////////////////////////////////////////


#define GROWUNTIL(fail, func)                        \
   do {                                              \
      if ((func) != (fail))                          \
         break;                                      \
   } while ((GetLastError() == ERROR_MORE_DATA) ||   \
            (GetLastError() == ERROR_INSUFFICIENT_BUFFER));



///////////////////////////////////////////////////////////////////////////////





///////////////////////////////// End of File /////////////////////////////////

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Other
United States United States
fdefewtr534554yutki8op09;[pio';l.n,kbnmcvbxcvzxaqW876876UIYIUJUGHJGFHYFGHRDTR4564QWEDASASFDXCBVCBNGHNMJHMJN,NJKL;O[P-0=-]'[P';L/L,M.NM,BNMCGNGFXDGDFGTYU76TRYW34TR5AWERFASDVGfdsxbvfbvnvnm,jkl.k

Comments and Discussions