PDA

View Full Version : [C++] Useful Code Snippets



Mr. Hasselhoff
03-27-2011, 01:50 PM
Credit to SEGnosis @ UC-forum (http://www.uc-forum.com/forum/c-and-c/64284-qts-quick-tip-series.html) for all of these

Note: A lot of these functions are inter-dependent meaning that further ones down the list may use functions earlier in the list.


Append a file name onto a file location - main()
Prerequisite: N/A

#include <iostream>
int main()
{
char* szFileLocation = "C:\\Foo\\Faa\\Fee.exe"; // Example file location string.

char* dwLetterAddress = strrchr( szFileLocation, '\\' ); // strrchr returns the address of the first instance of the backslash character inside of the szFileLocation string starting from the right side.

*( dwLetterAddress + 1 ) = 0; // De-Referencing the address returned plus the 1 to effect the first letter after the backslash. Setting it to zero ends the string.

strcat( szFileLocation, "MyFileName.txt" ); // Since the string now does not contain the original file name. We can use strcat to append the string we want onto the file location.

return 0;
}

Attain the injected dll file location - DllMain()
Prerequisite: N/A

BOOL WINAPI DllMain( HMODULE hModule, DWORD dwReason, LPVOID lpvReserved )
{
switch( dwReason )
{
case DLL_PROCESS_ATTACH:
char szString[ MAX_PATH ]; // Create a character array buffer with the maximum path length ( besides uniPHP )

GetModuleFileName( hModule, szString, sizeof( szString ) ); // This function uses the module handle to input the module file path into the string array ie. "C:\\foo\\fee\\module.exe".

// The file path will be that of the injector, not the current process.
// If you wish to get the file location of the executable; you can set the parameter for HMODULE to null and it will use the module handle of the current executable. This can save time if you aren't looking into getting the file path from within DllMain.

break;
}

return TRUE;
}

Primitive line drawing function - DrawLine()
Prerequisite: N/A


LPD3DXLINE g_pLine; // global

void DrawLine ( long Xa, long Ya, long Xb, long Yb, DWORD dwWidth, D3DCOLOR Color )
{
D3DXVECTOR2 vLine[ 2 ]; // Two points
g_pLine->SetAntialias( 0 ); // To smooth edges

g_pLine->SetWidth( dwWidth ); // Width of the line
g_pLine->Begin();

vLine[ 0 ][ 0 ] = Xa; // Set points into array
vLine[ 0 ][ 1 ] = Ya;
vLine[ 1 ][ 0 ] = Xb;
vLine[ 1 ][ 1 ] = Yb;

g_pLine->Draw( vLine, 2, Color ); // Draw with Line, number of lines, and color
g_pLine->End(); // finish
}
//----------------------------------//

//----------------------------------//
if( !g_pLine )
D3DXCreateLine( pDevice, &g_pLine ); // Make sure to create before calling DrawLine, use in endscene, present, etc
//----------------------------------//
// Make sure to reset as well to avoid crashes


Primitive rectangle drawing function - DrawRect()
Prerequisite: Line variable from #3


// Better than Clear() because it supports alpha transparency.

void DrawRect ( long X, long Y, long W, long H, D3DCOLOR Color )
{
D3DXVECTOR2 vLine[ 2 ];
g_pLine->SetWidth( W );
g_pLine->SetAntialias( 0 );
g_pLine->SetGLLines( 1 );

g_pLine->SetWidth( W );
g_pLine->Begin();

vLine[ 0 ][ 0 ] = X+W/2;
vLine[ 0 ][ 1 ] = Y;
vLine[ 1 ][ 0 ] = X+W/2;
vLine[ 1 ][ 1 ] = Y+H;

g_pLine->Draw( vLine, 2, Color );
g_pLine->End();
}


Primitive rectangle outline drawing function (Box ESP) - DrawLineRect()
Prerequisite: #4 DrawRect()

#define LR_WIDTH 1 // Controls width of lines

void DrawLineRect( long X, long Y, long W, long H, D3DCOLOR Color )
{

DrawRect( X, Y, W, LR_WIDTH, Color );
DrawRect( X, Y, LR_WIDTH, H, Color );

DrawRect( X + W, Y, LR_WIDTH, H, Color );
DrawRect( X, Y + H, W, LR_WIDTH, Color );
}

Primitive shape drawing function - DrawShape()
Prerequisite: N/A

#define PI 3.14159265

enum ShapeSides
{
LINE = 2,
TRIANGLE = 3,
SQUARE = 4,
PENTAGON = 5,
HEXAGON = 6,
HEPTAGON = 7,
OCTAGON = 8,
NONAGON = 9,
DECAGON = 11
};

//----------------------------------//
void DrawShape( long x, long y, float fDegrees, DWORD dwSides, DWORD dwSize, DWORD dwLineWidth, D3DCOLOR Color )
{
POINT* pt = new POINT[ dwSides + 1 ]; // allocate points

fDegrees = ( fDegrees * PI ) / 180.0f; // Convert degrees to radians

float k = 0.000000f;

for( int i = 0; i < dwSides; i++, k += ( TWO_PI/dwSides ) )
{
float fDeg = k + fDegrees; // add degrees

while( fDeg > TWO_PI )
fDeg -= TWO_PI; // limit

pt[ i ].x = ( cos( fDeg ) * dwSize ) + x; // set points
pt[ i ].y = ( sin( fDeg ) * dwSize ) + y;
}

pt[ dwSides ].x = pt[ 0 ].x; // set last point to first
pt[ dwSides ].y = pt[ 0 ].y;

for( int i = 0; i < dwSides; i++ )
CDraw.Line( pt[ i ].x, pt[ i ].y, pt[ i + 1 ].x, pt[ i + 1 ].y, dwLineWidth, Color ); // draw through all points

delete[] pt; // release points allocated
}

Dynamic format string class - C_Strings
Prerequisite: N/A

class C_Strings
{
public:
C_Strings(); // Constructor
~C_Strings(); // De-constructor

char* _sprintf( char* szString, ... ); // Prototype

void AppendSprintfQueue( char* szNewSpace ); // Prototype
void ClearSprintfQueue( void ); // Prototype

private:
DWORD* m_pdwSprintfQueue; // Pointer that will hold the address of the area that will hold the addresses of the allocated char strings
DWORD m_dwSizeofSprintfQueue; // The number of allocated char strings
}Strings; // Instance
//----------------------------------//

//----------------------------------//
C_Strings::C_Strings()
{
m_pdwSprintfQueue = 0; // Default
m_dwSizeofSprintfQueue = 0; // Default
}
//----------------------------------//
C_Strings::~C_Strings()
{

}
//----------------------------------//

//----------------------------------//
char* C_Strings::_sprintf( char* szString, ... )
{
char* szBuffer = new char[ 1024 ]; // Allocate a new char array of 1mb ( 1024 bytes )
AppendSprintfQueue( szBuffer ); // Store the address of the string in the queue

va_list va_alist; // Make a new char*
va_start( va_alist, szString );
vsprintf( szBuffer, szString, va_alist); // Place string with parameters embedded into szBuffer
va_end ( va_alist );

return szBuffer; // return char string
}
//----------------------------------//
void C_Strings::AppendSprintfQueue( char* szNewSpace )
{
DWORD* pdwTemp = new DWORD[ ++m_dwSizeofSprintfQueue ]; // Create a new dword pointer of the m_dwSizeofSprintfQueue += 1

if( m_pdwSprintfQueue ) // If m_pdwSprintfQueue has more than one index
{
memcpy( pdwTemp, m_pdwSprintfQueue, m_dwSizeofSprintfQueue * sizeof( DWORD ) ); // Save all the data from m_pdwSprintfQueue into pdwTemp
delete[] m_pdwSprintfQueue; // Free the data/memory in m_pdwSprintfQueue
}

pdwTemp[ m_dwSizeofSprintfQueue - 1 ] = ( DWORD )szNewSpace; // Inside the last index, save the address of the new string

m_pdwSprintfQueue = pdwTemp; // Give the pointer to m_pdwSprintfQueue
}
//----------------------------------//
void C_Strings::ClearSprintfQueue( void )
{
for( int i = 0; i < m_dwSizeofSprintfQueue; i++ )// Go through all the indexes of m_pdwSprintfQueue
{
char* pDel = ( char* )m_pdwSprintfQueue[ i ]; // Make a pointer that refrences the string

delete[] pDel; // Free the memory in that pointer
}

delete[] m_pdwSprintfQueue; // Clear the queue of addresses
m_pdwSprintfQueue = 0; // Default
m_dwSizeofSprintfQueue = 0; // Default
}

Text log class - C_Log
Prerequisite: #7 Dynamic format string class

class C_Log
{
public:
C_Log(); // Constructor
~C_Log(); // De-constructor
void Reset( void ); // Prototype
void LoadLogLocation( char* szString ); // Prototype
void Log( char* szString, ... ); // Prototype

private:
char szLogLocation[ MAX_PATH ]; // Path of text file
}CLog;
//----------------------------------//

//----------------------------------//
C_Log::C_Log()
{

}
//----------------------------------//
C_Log::~C_Log()
{

}
//----------------------------------//

//----------------------------------//
void C_Log::Reset( void )
{
fstream pFile( szLogLocation, ios::out ); // Open the text file for output only ( eraces everything )
pFile.close(); // Closes file
}
//----------------------------------//
void C_Log::LoadLogLocation( char *szString )
{
strcpy( szLogLocation, szString ); // Moves file path into szLogLocation

char* pszTemp = strrchr( szLogLocation, '\\' ); // Gets last instance of '\' character in file path
*( pszTemp + 1 ) = 0; // Null out character after dash to append path
strcat( pszTemp, "\\Hook Log.txt" ); // Append file log name
}
//----------------------------------//
void C_Log::Log( char* szString, ... )
{
char* szBuffer = new char[ 2048 ]; // Create format holding char array
char szTime[ MAX_PATH ]; // Create time hold char array
time_t tmCurrentTime; // create time_t structure instance

va_list va_alist;
va_start( va_alist, szString );
vsprintf( szBuffer, szString, va_alist); // fill szBuffer with string and filled format
va_end ( va_alist );

time( &tmCurrentTime ); // Get local time
strcpy( szTime, ctime( &tmCurrentTime ) ); // load time string into szTime
szTime[ strlen( szTime ) - 1 ] = 0; // Remove newline character

strcpy( szBuffer, Strings._sprintf( "[ %s ] %s", szTime, szBuffer ) ); // Format the string ( for _sprintf view [Snippet] Dynamic format strings class )

fstream pFile( szLogLocation, ios::app ); // open file for appending

pFile << szBuffer << endl; // output string

pFile.close(); // close file

delete[] szBuffer; // free string memory
Strings.ClearSprintfQueue(); // clear queue
}
//----------------------------------//

BOOL WINAPI DllMain( HMODULE hModule, DWORD dwReason, LPVOID lpvReserved )
{
switch( dwReason )
{
case DLL_PROCESS_ATTACH:
char szString[ MAX_PATH ];
GetModuleFileName( hModule, szString, sizeof( szString ) ); // Gets dll full path and name

CLog.LoadLogLocation( szString ); // Sets up the text log location
CLog.Log( "// Library injected //" ); // Example on how its used
break;

case DLL_PROCESS_DETACH:
CLog.Log( "// Library released %d//\n\n\n", 0 ); // Example on how its used
break;
}

return TRUE;
}

Primitive arrow drawing function - DrawArrow()
Prerequisite: N/A

#define PI 3.14159265
#define TWO_PI ( PI * 2 )

#define DegreeToRadian(a)( ( ( a * PI ) / 180.0f ) )
#define LimitTwoPI(a)while( a > TWO_PI )a -= TWO_PI
#define Limit360(a)while( a > 360.0f )a -= 360.0f

//----------------------------------//
void DrawArrow( long x, long y, float fDegrees, DWORD dwLength, DWORD dwAngleDivisor, DWORD dwLineWidth, D3DCOLOR Color )
{
Limit360( fDegrees );

DWORD dwAngleLength = dwLength/dwAngleDivisor;

float fRightAngle = 135.0f + fDegrees,
fLeftAngle = 225.0f + fDegrees;

Limit360(fRightAngle);
Limit360(fLeftAngle);

fRightAngle = DegreeToRadian( fRightAngle );
fLeftAngle = DegreeToRadian( fLeftAngle );
fDegrees = DegreeToRadian( fDegrees );

POINT pt = { cos( fDegrees ) * dwLength + x, sin( fDegrees ) * dwLength + y };

CDraw.Line( x, y, pt.x, pt.y, dwLineWidth, Color );

CDraw.Line( pt.x, pt.y, cos( fRightAngle ) * dwAngleLength + pt.x,
sin( fRightAngle ) * dwAngleLength + pt.y, dwLineWidth, Color );

CDraw.Line( pt.x, pt.y, cos( fLeftAngle ) * dwAngleLength + pt.x,
sin( fLeftAngle ) * dwAngleLength + pt.y, dwLineWidth, Color );
}

World to screen - World2Screen()
Prerequisite: N/A

#define IsW2SValid(a)( ( a.x == -1000 && a.y == -1000 ) ? false : true )


struct FVect3
{
float x,y,z;
};


//----------------------------------//
POINT World2Screen( FVect3 vPos )
{
POINT ptRet = { -1000, -1000 };

D3DXVECTOR3 vScreen,
vWorld( vPos.x, vPos.y, vPos.z );

static D3DXMATRIX m_mxProjection,
m_mxView,
m_mxWorld;

pDevice->GetTransform( D3DTS_VIEW, &m_mxView );

pDevice->GetTransform( D3DTS_PROJECTION, &m_mxProjection );

pDevice->GetTransform( D3DTS_WORLD, &m_mxWorld );

pDevice->GetViewport( &m_ViewPort );

D3DXVec3Project( &vScreen, &vWorld, &m_ViewPort, &m_mxProjection, &m_mxView, &m_mxWorld );

if( vScreen.z <= 1 )
{
ptRet.x = vScreen.x;
ptRet.y = vScreen.y;
}

return ptRet;
}



*This is under construction

TACO_HATER
03-27-2011, 08:20 PM
Note, the w2s will only work if the game uses that method to do the w2s not their own..

Also, for the shapes it's much better to use primitives rather than the lines, if more than lets say 10 rectangles are drawn you will notice a significant performance drop. I will try to draw up some code that uses DIP instead.