1. Pinoy Hideout is the Ultimate Pinoy Chat, Mobile and PC Community
    We have been leading the scene for over 7 years and currently offer:
    • latest and updated game cheats and softwares
    • internet tricks for your android phone, mobile network, and computer
    • a staff team who helps protect its members from malware while enforcing a great community

    • You must register an account before you can post, download files, and see any advanced features or content.

  2. How to avail PHO VIP hack/s?
    Click here for more Info!
    [PHO] VIP Hacks Price

      SF1 VIP|ROS VIP

    • 7 Days = php 150
    • 15 Days = php 200
    • 1 Month = php 300

      SF2 VIP

    • 7 Days = php 200
    • 15 Days = php 250
    • 1 Month = php 500
    Dismiss Notice
Dismiss Notice
Announcement!
Gusto mo bang makakuha ng VIP subcription ng libre? CLICK HERE<

Source D3D9 Window Mode Create Hook

Discussion in 'Specialforce Source Code & Tutorials' started by killovehack123, Oct 7, 2019.

  1. killovehack123

    Reputations:
    20
    Joined:
    Feb 14, 2019
    Posts:
    15
    Likes Received:
    4
    Trophy Points:
    3

    Code:
    #define _WIN32_WINNT 0x0501
    #define WINVER 0x0501
    #include <Windows.h>
    #include <fstream>
    #include "detours.h"
    #include <d3d9.h>
    #pragma comment( lib, "detours.lib" )
    #pragma comment( lib, "d3d9.lib" )
    using namespace std;
    #define SAFE_RELEASE(p){if(p)(p)->Release();(p)=NULL;}
    #define HookFunc(addy,func) p##func = (func##_t)DetourFunction((PBYTE)addy,(PBYTE)func##_Detour);
    #define UnHookFunc(func) DetourRemove((PBYTE)p##func,(PBYTE)func##_Detour);
    #define DEF_DETOUR_FUNC( func , returntype , proto ) \
    returntype WINAPI  func##_Detour proto; \
    typedef returntype ( WINAPI * func##_t ) proto ; \
    func##_t p##func = NULL;
    BOOL IsWindowed = false;    // Flag windowed/fullscreen mode asume that we are in fullscreen from start
    HMENU hMenu;       // Handle to store menu between toggles
    static bool bInvalid = false;   // Flag To Settting D3DERR_DEVICELOST/D3DERR_DEVICNOTRESET
    DEF_DETOUR_FUNC(SwapPresent,HRESULT,(IDirect3DSwapChain9* pDevice,CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion,DWORD dwFlags));
    DEF_DETOUR_FUNC(Present,HRESULT,(IDirect3DDevice9* pDevice,CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion));
    DEF_DETOUR_FUNC(Reset,HRESULT,(IDirect3DDevice9* pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters));
    DEF_DETOUR_FUNC(TestCooperativeLevel,HRESULT,(IDirect3DDevice9* pDevice));
    PVOID GetInterfaceFuncAddress(PVOID pDeviceInterface, const char *fmt, ...)
    { 
    va_list va_alist;
    char buf[32];
    memset(buf, 0, sizeof(buf));
    va_start(va_alist, fmt);
    _vsnprintf_s(buf, sizeof(buf), fmt, va_alist);
    va_end(va_alist);
    char *op = (char *)strtoul(buf, NULL, 16);
    while(1)
    {
      if(*op == '\xFF')
      {
       op++;
       if(*op == '\xA0')
       {
        int iIndex = 0;
        op++;
        memcpy(&iIndex, op, (4));
        return *(PVOID*)(*(DWORD*)pDeviceInterface + iIndex  );
       }
       op++;
       break;
      }
      op++;
    }
    if (((int)*op)  < 0)
    {
      DWORD *dwRet = (DWORD*)pDeviceInterface;
      return *(PVOID*)dwRet[0];
    }
    return *(PVOID*)(*(DWORD*)pDeviceInterface + ((int)*op ) ); //Credits: as2 (G-D)
    }
    HRESULT APIENTRY SwapPresent_Detour(IDirect3DSwapChain9* pDevice,CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion,DWORD dwFlags)
    {
    __asm NOP
    if (GetAsyncKeyState(VK_UP)&1)
    if (PRESS(VK_UP)&1)
      bInvalid=!bInvalid;
    if (bInvalid)
      return D3DERR_DEVICELOST;
    return pSwapPresent(pDevice,pSourceRect,pDestRect,hDestWindowOverride,pDirtyRegion,dwFlags);
    }
    HRESULT APIENTRY Present_Detour(IDirect3DDevice9* pDevice,CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion)
    {
    __asm NOP
    if (GetAsyncKeyState(VK_UP)&1)
    if (PRESS(VK_UP)&1)
      bInvalid=!bInvalid;
    
    if (bInvalid)
      return D3DERR_DEVICELOST;
    return pPresent(pDevice,pSourceRect,pDestRect,hDestWindowOverride,pDirtyRegion);
    }
    HRESULT APIENTRY TestCooperativeLevel_Detour(IDirect3DDevice9* pDevice)
    {
    __asm NOP
    HRESULT hr = pTestCooperativeLevel(pDevice);
    if (bInvalid)
      hr = D3DERR_DEVICENOTRESET;
    return hr;
    }
    HRESULT APIENTRY Reset_Detour(IDirect3DDevice9* pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters)
    {
    __asm pushad
    __asm NOP
      bool bSetWindowMode = false;
    D3DPRESENT_PARAMETERS PresentParam = *pPresentationParameters;
    if (bInvalid)
    {
      if (IsWindowed)
      {
       SetWindowLong(PresentParam.hDeviceWindow, GWL_STYLE, WS_POPUP|WS_SYSMENU|WS_VISIBLE);
       if(hMenu == NULL)     
       {           
        hMenu = GetMenu(PresentParam.hDeviceWindow);           
        SetMenu(PresentParam.hDeviceWindow, NULL);       
       }
       PresentParam.Windowed = FALSE;
       IsWindowed = FALSE;
      }else
      {
       PresentParam.Windowed = 1;
       PresentParam.Flags = 0;
       PresentParam.FullScreen_RefreshRateInHz = 0;
       SetWindowPos(PresentParam.hDeviceWindow, HWND_NOTOPMOST, 0, 0, pPresentationParameters->BackBufferWidth, pPresentationParameters->BackBufferHeight, SWP_SHOWWINDOW);
       bSetWindowMode = true;
       IsWindowed = TRUE;
      }
      bInvalid = false;
    }
    HRESULT hResult = pReset(pDevice,&PresentParam);
    if (hResult == D3D_OK)
    {
      if (bSetWindowMode)
      {
       if (hMenu)
        SetMenu(PresentParam.hDeviceWindow, hMenu);
       RECT r;
       SetWindowLong(PresentParam.hDeviceWindow, GWL_STYLE,WS_POPUP | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE);
       GetClientRect(PresentParam.hDeviceWindow, &r);
       SetWindowPos(PresentParam.hDeviceWindow, HWND_NOTOPMOST,0, 0,
        PresentParam.BackBufferWidth + (PresentParam.BackBufferWidth - r.right),
        PresentParam.BackBufferHeight + (PresentParam.BackBufferHeight - r.bottom),
        SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOMOVE);
      }
    }
    return hResult;
    }
    __declspec(dllexport) IDirect3D9* D3DCreate9(UINT SDKVersion)
    {
    return Direct3DCreate9(D3D_SDK_VERSION/*SDKVersion*/);
    }
    DWORD InitializeD3DHooks(LPVOID /*pv*/)
    {
    
    LoadLibrary("d3d9.dll");
    D3DPRESENT_PARAMETERS d3dpp;
    LPDIRECT3DDEVICE9 pd3dDevice;
    IDirect3D9 *pd3dInterface = D3DCreate9/*Direct3DCreate9*/(D3D_SDK_VERSION);
    ZeroMemory(&d3dpp,sizeof(d3dpp));
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    HRESULT hr = pd3dInterface->CreateDevice(  D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,GetForegroundWindow(),D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pd3dDevice );
    if (!SUCCEEDED(hr))
    {
      char dbgstr[MAX_PATH]="";
      sprintf_s(dbgstr,_TRUNCATE,"0x%08X",hr);
      OutputDebugString(dbgstr);
      return false;
    }
    else
    {
      LPDIRECT3DSWAPCHAIN9 pSwarpchain;
      if (pd3dDevice->GetNumberOfSwapChains())
      {
       pd3dDevice->GetSwapChain(0,&pSwarpchain);
       HookFunc(GetInterfaceFuncAddress((DWORD*)pSwarpchain,"%p",&IDirect3DSwapChain9::Present),SwapPresent)
       SAFE_RELEASE(pSwarpchain)
      }
      HookFunc(GetInterfaceFuncAddress((DWORD*)pd3dDevice,"%p",&IDirect3DDevice9::Present),Present);
      HookFunc(GetInterfaceFuncAddress((DWORD*)pd3dDevice,"%p",&IDirect3DDevice9::Reset),Reset)
      HookFunc(GetInterfaceFuncAddress((DWORD*)pd3dDevice,"%p",&IDirect3DDevice9::TestCooperativeLevel),TestCooperativeLevel)
      SAFE_RELEASE(pd3dInterface)
      SAFE_RELEASE(pd3dDevice)
    }
    return (1);
    return true;
    }
    void RemoveD3DHooks()
    {
    if(pSwapPresent)
      UnHookFunc(SwapPresent)
    UnHookFunc(Present)
    UnHookFunc(Reset)
    UnHookFunc(TestCooperativeLevel)
    }
    BOOL WINAPI DllMain( HMODULE hModule, DWORD dwReason, LPVOID lpvReserved )
    {
    switch ( dwReason )
    {
    case DLL_PROCESS_ATTACH:
      DisableThreadLibraryCalls( hModule );
      CreateThread( NULL, 0, ( LPTHREAD_START_ROUTINE ) InitializeD3DHooks, NULL, 0, NULL );
      break;
    case DLL_PROCESS_DETACH:
      DisableThreadLibraryCalls( hModule );
      RemoveD3DHooks();
      break;
    }
    return TRUE;
    }
     
    imnotlucky likes this.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Share This Page

Pinoy Hideout