Advertisement
mgostih

FPS Limiter

Dec 12th, 2016
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #define Naked __declspec(naked)
  3. #define stdcall __stdcall
  4.  
  5. void SuperSleep(LONGLONG microseconds){
  6.     LARGE_INTEGER Time1, Time2, Frequency, DeltaTime;
  7.     DeltaTime.QuadPart = 0;
  8.     QueryPerformanceFrequency(&Frequency);
  9.     QueryPerformanceCounter(&Time1);
  10.     while (DeltaTime.QuadPart < microseconds){
  11.         QueryPerformanceCounter(&Time2);
  12.         DeltaTime.QuadPart = Time2.QuadPart - Time1.QuadPart;
  13.         DeltaTime.QuadPart *= 1000000;
  14.         DeltaTime.QuadPart /= Frequency.QuadPart;
  15.     }
  16.    
  17. }
  18. void PlaceJmp(HANDLE hwnd,PVOID JumpFrom, DWORD JumpTo){       
  19.     DWORD old;
  20.     DWORD bkup;
  21.     DWORD JmpOffset = (DWORD)JumpTo - (DWORD)JumpFrom - 5;
  22.     VirtualProtectEx(hwnd, JumpFrom, 5, PAGE_EXECUTE_READWRITE, &old);
  23.     WriteProcessMemory(hwnd, JumpFrom, "\xE9", 1, NULL);
  24.     WriteProcessMemory(hwnd, (PVOID)((DWORD)JumpFrom + 1),&JmpOffset,4,NULL);
  25.     VirtualProtectEx(hwnd, JumpFrom, 5, old, &bkup);
  26. }
  27.  
  28. PVOID GetHookedAddress(PVOID address){
  29.     if (*(unsigned char*)address == 0xE9){
  30.         return (PVOID)(*(DWORD*)((DWORD)address + 1) + (DWORD)address + 5);
  31.     }
  32.     else return (PVOID)0;
  33. }
  34.  
  35. Naked void OpenGLHook(){
  36.     SuperSleep(5555);
  37.    
  38.     __asm {
  39.         ret
  40.     }
  41. }
  42.  
  43. PVOID HookSetup(PVOID HookAddr, PVOID CallThis);    //Not Open Source
  44.  
  45. void LockDLL(HMODULE DllHandle);                    //Not Open Source
  46.  
  47. BOOL WINAPI DllMain(
  48.     _In_ HINSTANCE hinstDLL,
  49.     _In_ DWORD     fdwReason,
  50.     _In_ LPVOID    lpvReserved
  51.     ){
  52.     if (fdwReason == DLL_PROCESS_ATTACH){
  53.         PVOID ToHook = (PVOID)GetProcAddress(GetModuleHandleA("OPENGL32.dll"), "wglSwapBuffers");
  54.         PVOID AllocatedHook = HookSetup(ToHook, (PVOID)OpenGLHook);
  55.         PlaceJmp((HANDLE)-1, ToHook, (DWORD)AllocatedHook);
  56.         LockDLL(hinstDLL);
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement