Skip to content

Commit

Permalink
增加base::hook::patch
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Jan 8, 2014
1 parent 0df8a19 commit 7151c8a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

namespace base { namespace hook { namespace detail {

memory_protect::memory_protect(uintptr_t address)
memory_protect::memory_protect(uintptr_t address, size_t size)
: success_(false)
, access_(0)
, address_(address)
, size_(size)
{
if (0 == ::VirtualQuery((LPCVOID)address, &mbi_, sizeof(mbi_)))
return ;


if (!::VirtualProtect(mbi_.BaseAddress, mbi_.RegionSize, PAGE_WRITECOPY, &access_))
if (!::VirtualProtect((LPVOID)address_, size_, PAGE_READWRITE, &access_))
{
if (!::VirtualProtect(mbi_.BaseAddress, mbi_.RegionSize, PAGE_READWRITE, &access_))
{
return ;
}
return;
}

success_ = true;
Expand All @@ -24,8 +20,8 @@ namespace base { namespace hook { namespace detail {
{
if (success())
{
DWORD newAccess;
::VirtualProtect(mbi_.BaseAddress, mbi_.RegionSize, access_, &newAccess);
DWORD new_access;
::VirtualProtect((LPVOID)address_, size_, access_, &new_access);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
#include <base/config.h>
#include <Windows.h>

namespace base {
namespace hook { namespace detail {
namespace base { namespace hook { namespace detail {
class memory_protect
{
public:
memory_protect(uintptr_t address);
memory_protect(uintptr_t address, size_t size = sizeof(uintptr_t));
~memory_protect();
bool success() const;

private:
bool success_;
MEMORY_BASIC_INFORMATION mbi_;
DWORD access_;
bool success_;
DWORD access_;
uintptr_t address_;
size_t size_;
};
}}
}
}}}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace base { namespace hook { namespace detail {
{
return 0;
}

::FlushInstructionCache(::GetCurrentProcess(), (LPVOID)address, sizeof(uintptr_t));
}

return old_value;
Expand Down
21 changes: 21 additions & 0 deletions Development/Editor/Core/YDWEBase/base/hook/patch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <base/hook/patch.h>
#include <base/hook/detail/memory_protect.h>
#include <Windows.h>

namespace base { namespace hook {

bool patch(uintptr_t address, const char* value, size_t value_size)
{
detail::memory_protect protect_(address, value_size);
if (protect_.success())
{
DWORD written;
if (::WriteProcessMemory(::GetCurrentProcess(), (LPVOID)address, value, value_size, &written))
{
::FlushInstructionCache(::GetCurrentProcess(), (LPVOID)address, value_size);
return true;
}
}
return false;
}
}}
23 changes: 23 additions & 0 deletions Development/Editor/Core/YDWEBase/base/hook/patch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <base/config.h>
#include <cassert>
#include <cstdint>

namespace base { namespace hook {
_BASE_API bool patch(uintptr_t address, const char* value, size_t value_size);


template <size_t n>
bool patch(uintptr_t address, const char(&value)[n])
{
assert(n > 1);
return patch(address, value, n - 1);
}

template <size_t n>
bool patch(uintptr_t address, char(&value)[n])
{
return patch(address, value, std::string::traits_type::length(value));
}
}}

0 comments on commit 7151c8a

Please sign in to comment.