/// <summary> /// Allocates memory region beyond 4GB limit /// </summary> /// <param name="imageMem">Image data</param> /// <param name="size">Block size</param> /// <returns>true on success</returns> bool MMap::AllocateInHighMem( MemBlock& imageMem, size_t size ) { HANDLE hFile = GetDriverHandle(); if (hFile != INVALID_HANDLE_VALUE) { ptr_t ptr = 0; // Align on page boundary size = Align( size, 0x1000 ); // // Get random address // bool found = true; static std::random_device rd; std::uniform_int_distribution<ptr_t> dist( 0x100000, 0x7FFFFFFF ); // Make sure address is unused for (ptr = dist( rd ) * 0x1000; found; ptr = dist( rd ) * 0x1000) { found = false; for (auto& entry : _usedBlocks) if (ptr >= entry.first && ptr < entry.first + entry.second) { found = true; break; } } PURGE_DATA data = { _process.core().pid(), 1, { ptr, size } }; DWORD junk = 0; BOOL ret = DeviceIoControl( hFile, static_cast<DWORD>(IOCTL_VADPURGE_ENABLECHANGE), &data, sizeof(data), NULL, 0, &junk, NULL ); CloseHandle( hFile ); // Change protection and save address if(ret == TRUE) { _usedBlocks.emplace_back( std::make_pair( ptr, size ) ); imageMem = MemBlock( &_process.memory(), ptr, size, PAGE_READWRITE, false ); _process.memory( ).Protect( ptr, size, PAGE_READWRITE ); return true; } } return false; }
Dword DTV_Initialize(Byte handleNum) { Dword dwError = ERR_NO_ERROR; DemodDriverInfo DriverInfo; g_hDriver = GetDriverHandle(handleNum); //DTV_ControlPowerSaving(1); // Check driver is loaded correctly dwError = DTV_GetVersion(&DriverInfo); if (g_hDriver == INVALID_HANDLE_VALUE) { printf("\nINVALID_HANDLE_VALUE fail\n"); dwError = ERR_INVALID_DEV_TYPE; } if (g_hDriver < INVALID_HANDLE_VALUE) { dwError = ERR_INVALID_DEV_TYPE; } return (dwError); }