Skip to content

Instantly share code, notes, and snippets.

@cfillion
Created May 6, 2022 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfillion/2b2a12368fb0825cbad19ba4a7f8179b to your computer and use it in GitHub Desktop.
Save cfillion/2b2a12368fb0825cbad19ba4a7f8179b to your computer and use it in GitHub Desktop.
Hack to allow input of any characters in Unicode windows
// cl /nologo /O2 reaper_widemsg.cpp User32.lib /link /DLL /OUT:reaper_widemsg.dll
#include <cstdint>
#define REAPERAPI_IMPLEMENT
#include "reaper_plugin_functions.h"
template<typename T>
bool patch(const uintptr_t addr, const T newCode, T *backup = nullptr)
{
uint8_t *firstByte { reinterpret_cast<uint8_t *>(addr) };
if(backup)
memcpy(backup, firstByte, sizeof(T));
DWORD oldProtect;
if(!VirtualProtect(firstByte, sizeof(T), PAGE_READWRITE, &oldProtect))
return false;
memcpy(firstByte, &newCode, sizeof(T));
VirtualProtect(firstByte, sizeof(T), oldProtect, &oldProtect);
return true;
}
#pragma pack(push, 1)
struct Jump {
#if defined(_WIN64)
// uint8_t int3 = 0xcc;
uint8_t mov = 0x48, reg = 0xb8; uint64_t dest; // mov rax, dest
uint8_t jmp = 0xff, modr_m = 0xe0; // jmp rax
#else
# error Unimplemented architecture
#endif
};
#pragma pack(pop)
class Redirect {
public:
Redirect(const uintptr_t funcAddr, const uintptr_t redirect)
: m_funcAddr { funcAddr }
{
Jump jump;
jump.dest = redirect;
patch(m_funcAddr, jump, &m_code);
}
~Redirect()
{
patch(m_funcAddr, m_code);
}
private:
uintptr_t m_funcAddr;
Jump m_code;
};
#define ATOW(api) new Redirect { reinterpret_cast<uintptr_t>(api##A), \
reinterpret_cast<uintptr_t>(api##W) }
extern "C" REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT(
REAPER_PLUGIN_HINSTANCE instance, reaper_plugin_info_t *rec)
{
if(!rec || rec->caller_version != REAPER_PLUGIN_VERSION)
return 0;
ATOW(GetMessage);
ATOW(PeekMessage);
ATOW(DispatchMessage);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment