This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://web.archive.org/web/20120401132446/http://bloglitb.blogspot.com/2011/12/access-to-private-members-safer.html | |
// This technique was invented by litb in 2010, long before | |
// templates were supercharged in C++17. Inspired by this method, I have | |
// reimplemented it using more modern C++ features. | |
// | |
// https://www.youtube.com/watch?v=SmlLdd1Q2V8 | |
/* | |
14.7.2p8 The usual access checking rules do not apply to names used to specify | |
explicit instantiations. [Note: In particular, the template arguments and names | |
used in the function declarator (including parameter types, return types and |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <Unknwn.h> | |
#include <exception> | |
template <typename T> struct COMPtrOut | |
{ | |
inline COMPtrOut() = delete; | |
inline COMPtrOut(const COMPtrOut&) = delete; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void* allocate_in_32bits(uint32_t start, uint32_t sz) { | |
if (!sz) return NULL; | |
MEMORY_BASIC_INFORMATION mbi = { 0 }; | |
char* min = (char*)start; | |
char* max = (char*)UINT32_MAX - sz; | |
while (min < max) { | |
if (!VirtualQuery(min, &mbi, sizeof mbi)) return NULL; | |
if (mbi.State == MEM_FREE && mbi.RegionSize > sz && mbi.BaseAddress) { | |
void* alloc = VirtualAlloc(mbi.BaseAddress, sz, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); | |
if ((uint64_t)alloc > UINT32_MAX) { VirtualFree(alloc, 0, MEM_RELEASE); return NULL; } |