Results 1 to 1 of 1
  1. #1
    Veteran Dwar is on a distinguished road Dwar's Avatar
    Join Date
    2010 Mar
    Thanked 2,220 Times in 286 Posts
    Rep Power

    [C++] Memory-writing functions

    Snippets for writing bytes to specified memory address

    WriteMemory - write an byte-array to memory-address.
    WriteJump - both arguments are addresses, where to jump from and where to jump to.
    WriteNop - address and how many to write.
     #ifndef MEMUTILS_H
    #define MEMUTILS_H

    BOOL WriteMemory( DWORD dwAddress, const void* cpvPatch, DWORD dwSize )
    DWORD dwProtect;

    if( VirtualProtect( (void*)dwAddress, dwSize, PAGE_READWRITE, &dwProtect ) ) //Unprotect the memory
    memcpy( (void*)dwAddress, cpvPatch, dwSize ); //Write our patch
    return false; //Failed to unprotect, so return false..

    return VirtualProtect( (void*)dwAddress, dwSize, dwProtect, new DWORD ); //Reprotect the memory

    BOOL WriteJump( DWORD From, DWORD To )
    if( To < From + 128 && To > From - 128 ) //Short jump
    BYTE bpJump[2] = { 0xEB, ( To - From ) - 2 }; //Calculate opcode

    return WriteMemory( From, bpJump, 2 );
    else //Far jump
    BYTE bpJump[5] = { 0xE9, 0x00, 0x00, 0x00, 0x00 };
    *(DWORD*)&bpJump[1] = ( To - From ) - 5; //Calculate jump

    return WriteMemory( From, bpJump, 5 );

    return false; //If we end up here, something went horribly wrong

    BOOL WriteNop( DWORD dwAddress, DWORD dwAmount )
    BYTE bpNops[100]; //100 just incase I wanna do some gigantic NOPing
    memset( bpNops, 0x90, 100 ); //Fill byte-array with NOPs

    return WriteMemory( dwAddress, bpNops, dwAmount );

    Please, post your questions on forum, not by PM or mail

    I spend my time, so please pay a little bit of your time to keep world in equilibrium

Visitors found this page by searching for:

C memory patching

VirtualProtect c#


c# patch memory

C# virtualprotect

C# Memory patch

c memory patch

virtualprotect write

memory patch c

memory patch C#

c memory writing

c write bytes to memorysearch bytes for patch c#c write to memoryhow to write a byte in c in a memorywritememory c#c patch memoryc# memory patchingc memory writevc write memory address array of bytesBYTE bpNops[100]; 100 just incase I wanna do some gigantic NOPing writenops c c# memcpy void*memory patching C#0xE9 0x00 0x00 0x00 0x00


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts