CrackAndLoad



 
HomePortalCalendarFAQSearchMemberlistUsergroupsRegisterLog in
Welcome To CrackAndLoad! Please Be Active And Read Our Forum Rules To Avoid Get Banned! Enjoy !

Share | 
 

 Window mode toggler source code

View previous topic View next topic Go down 
AuthorMessage
CodeBlack

HYPER MODERATOR

avatar

Posts : 75
Post/Message Points : 175
Thanks : 26
Join date : 2012-12-01

PostSubject: Window mode toggler source code   Mon Dec 17, 2012 9:30 am

Now that Bf-Heroes is updating and i dont have anyting relevant to do, ive decided to put a small project together that enables window mode.
This method has worked on all the games ive tryed it out on so far.

I didnt comment alot as it explains itselfe else have a look here[You must be registered and logged in to see this link.]



Tested and working on following games:
BF2
BF2142
BFHeroes
Combat Arms EU (needs alt/tab after toggle)
COD2
GTR2
SIMS2
SETTLERS
Anno 1701
SPECIALFORCE

NOT working on:
Crossfire(fix posted)


Pleas post the game you try it on and let me know if its working or not.


Btw hotkey is VK_UP
EDIT: and be sure to give propper credit if you use this in your sold cheats.

detours.h
Code:
//////////////////////////////////////////////////////////////////////////////
//
//  File:      detours.h
//  Module:    detours.lib
//
//  Detours for binary functions.  Version 1.5 (Build 46)
//
//  Copyright 1995-2001, Microsoft Corporation
//

#pragma once
#ifndef _DETOURS_H_
#define _DETOURS_H_

//#pragma comment(lib, "detours")

//////////////////////////////////////////////////////////////////////////////
//
#ifndef GUID_DEFINED
#define GUID_DEFINED
typedef struct  _GUID
{
    DWORD Data1;
    WORD Data2;
    WORD Data3;
    BYTE Data4[ 8 ];
} GUID;
#endif // !GUID_DEFINED

#if defined(__cplusplus)
#ifndef _REFGUID_DEFINED
#define _REFGUID_DEFINED
#define REFGUID            const GUID &
#endif // !_REFGUID_DEFINED
#else // !__cplusplus
#ifndef _REFGUID_DEFINED
#define _REFGUID_DEFINED
#define REFGUID            const GUID * const
#endif // !_REFGUID_DEFINED
#endif // !__cplusplus
//
//////////////////////////////////////////////////////////////////////////////

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

/////////////////////////////////////////////////// Instruction Target Macros.
//
#define DETOUR_INSTRUCTION_TARGET_NONE          ((PBYTE)0)
#define DETOUR_INSTRUCTION_TARGET_DYNAMIC      ((PBYTE)~0ul)

/////////////////////////////////////////////////////////// Trampoline Macros.
//
// DETOUR_TRAMPOLINE(trampoline_prototype, target_name)
//
// The naked trampoline must be at least DETOUR_TRAMPOLINE_SIZE bytes.
//
#define DETOUR_TRAMPOLINE_SIZE          32
#define DETOUR_SECTION_HEADER_SIGNATURE 0x00727444  // "Dtr\0"

#define DETOUR_TRAMPOLINE(trampoline,target) \
static PVOID __fastcall _Detours_GetVA_##target(VOID) \
{ \
    return ⌖ \
} \
\
__declspec(naked) trampoline \
{ \
    __asm { nop };\
    __asm { nop };\
    __asm { call _Detours_GetVA_##target };\
    __asm { jmp eax };\
    __asm { ret };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
}

#define DETOUR_TRAMPOLINE_EMPTY(trampoline) \
__declspec(naked) trampoline \
{ \
    __asm { nop };\
    __asm { nop };\
    __asm { xor eax, eax };\
    __asm { mov eax, [eax] };\
    __asm { ret };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
    __asm { nop };\
}

/////////////////////////////////////////////////////////// Binary Structures.
//
#pragma pack(push, 8)
typedef struct _DETOUR_SECTION_HEADER
{
    DWORD      cbHeaderSize;
    DWORD      nSignature;
    DWORD      nDataOffset;
    DWORD      cbDataSize;
   
    DWORD      nOriginalImportVirtualAddress;
    DWORD      nOriginalImportSize;
    DWORD      nOriginalBoundImportVirtualAddress;
    DWORD      nOriginalBoundImportSize;
   
    DWORD      nOriginalIatVirtualAddress;
    DWORD      nOriginalIatSize;
    DWORD      nOriginalSizeOfImage;
    DWORD      nReserve;
} DETOUR_SECTION_HEADER, *PDETOUR_SECTION_HEADER;

typedef struct _DETOUR_SECTION_RECORD
{
    DWORD      cbBytes;
    DWORD      nReserved;
    GUID        guid;
} DETOUR_SECTION_RECORD, *PDETOUR_SECTION_RECORD;
#pragma pack(pop)

#define DETOUR_SECTION_HEADER_DECLARE(cbSectionSize) \
{ \
      sizeof(DETOUR_SECTION_HEADER),\
      DETOUR_SECTION_HEADER_SIGNATURE,\
      sizeof(DETOUR_SECTION_HEADER),\
      (cbSectionSize),\
      \
      0,\
      0,\
      0,\
      0,\
      \
      0,\
      0,\
      0,\
      0,\
}

///////////////////////////////////////////////////////////// Binary Typedefs.
//
typedef BOOL (CALLBACK *PF_DETOUR_BINARY_BYWAY_CALLBACK)(PVOID pContext,
                                                        PCHAR pszFile,
                                                        PCHAR *ppszOutFile);
typedef BOOL (CALLBACK *PF_DETOUR_BINARY_FILE_CALLBACK)(PVOID pContext,
                                                        PCHAR pszOrigFile,
                                                        PCHAR pszFile,
                                                        PCHAR *ppszOutFile);
typedef BOOL (CALLBACK *PF_DETOUR_BINARY_SYMBOL_CALLBACK)(PVOID pContext,
                                                          DWORD nOrdinal,
                                                          PCHAR pszOrigSymbol,
                                                          PCHAR pszSymbol,
                                                          PCHAR *ppszOutSymbol);
typedef BOOL (CALLBACK *PF_DETOUR_BINARY_FINAL_CALLBACK)(PVOID pContext);
typedef BOOL (CALLBACK *PF_DETOUR_BINARY_EXPORT_CALLBACK)(PVOID pContext,
                                                          DWORD nOrdinal,
                                                          PCHAR pszName,
                                                          PBYTE pbCode);

typedef VOID * PDETOUR_BINARY;
typedef VOID * PDETOUR_LOADED_BINARY;

//////////////////////////////////////////////////////// Trampoline Functions.
//
PBYTE WINAPI DetourFunction(PBYTE pbTargetFunction,
                            PBYTE pbDetourFunction);

BOOL WINAPI DetourFunctionWithEmptyTrampoline(PBYTE pbTrampoline,
                                              PBYTE pbTarget,
                                              PBYTE pbDetour);

BOOL WINAPI DetourFunctionWithEmptyTrampolineEx(PBYTE pbTrampoline,
                                                PBYTE pbTarget,
                                                PBYTE pbDetour,
                                                PBYTE *ppbRealTrampoline,
                                                PBYTE *ppbRealTarget,
                                                PBYTE *ppbRealDetour);

BOOL  WINAPI DetourFunctionWithTrampoline(PBYTE pbTrampoline,
                                          PBYTE pbDetour);

BOOL  WINAPI DetourFunctionWithTrampolineEx(PBYTE pbTrampoline,
                                            PBYTE pbDetour,
                                            PBYTE *ppbRealTrampoline,
                                            PBYTE *ppbRealTarget);

BOOL  WINAPI DetourRemove(PBYTE pbTrampoline, PBYTE pbDetour);

////////////////////////////////////////////////////////////// Code Functions.
//
PBYTE WINAPI DetourFindFunction(PCHAR pszModule, PCHAR pszFunction);
PBYTE WINAPI DetourGetFinalCode(PBYTE pbCode, BOOL fSkipJmp);

PBYTE WINAPI DetourCopyInstruction(PBYTE pbDst, PBYTE pbSrc, PBYTE *ppbTarget);
PBYTE WINAPI DetourCopyInstructionEx(PBYTE pbDst,
                                    PBYTE pbSrc,
                                    PBYTE *ppbTarget,
                                    LONG *plExtra);

///////////////////////////////////////////////////// Loaded Binary Functions.
//
HMODULE WINAPI DetourEnumerateModules(HMODULE hModuleLast);
PBYTE WINAPI DetourGetEntryPoint(HMODULE hModule);
BOOL WINAPI DetourEnumerateExports(HMODULE hModule,
                                  PVOID pContext,
                                  PF_DETOUR_BINARY_EXPORT_CALLBACK pfExport);

PBYTE WINAPI DetourFindPayload(HMODULE hModule, REFGUID rguid, DWORD *pcbData);
DWORD WINAPI DetourGetSizeOfPayloads(HMODULE hModule);

///////////////////////////////////////////////// Persistent Binary Functions.
//
BOOL WINAPI DetourBinaryBindA(PCHAR pszFile, PCHAR pszDll, PCHAR pszPath);
BOOL WINAPI DetourBinaryBindW(PWCHAR pwzFile, PWCHAR pwzDll, PWCHAR pwzPath);
#ifdef UNICODE
#define DetourBinaryBind  DetourBinaryBindW
#else
#define DetourBinaryBind  DetourBinaryBindA
#endif // !UNICODE

PDETOUR_BINARY WINAPI DetourBinaryOpen(HANDLE hFile);
PBYTE WINAPI DetourBinaryEnumeratePayloads(PDETOUR_BINARY pBinary,
                                          GUID *pGuid,
                                          DWORD *pcbData,
                                          DWORD *pnIterator);
PBYTE WINAPI DetourBinaryFindPayload(PDETOUR_BINARY pBinary,
                                    REFGUID rguid,
                                    DWORD *pcbData);
PBYTE WINAPI DetourBinarySetPayload(PDETOUR_BINARY pBinary,
                                    REFGUID rguid,
                                    PBYTE pbData,
                                    DWORD cbData);
BOOL WINAPI DetourBinaryDeletePayload(PDETOUR_BINARY pBinary, REFGUID rguid);
BOOL WINAPI DetourBinaryPurgePayloads(PDETOUR_BINARY pBinary);
BOOL WINAPI DetourBinaryResetImports(PDETOUR_BINARY pBinary);
BOOL WINAPI DetourBinaryEditImports(PDETOUR_BINARY pBinary,
                                    PVOID pContext,
                                    PF_DETOUR_BINARY_BYWAY_CALLBACK pfByway,
                                    PF_DETOUR_BINARY_FILE_CALLBACK pfFile,
                                    PF_DETOUR_BINARY_SYMBOL_CALLBACK pfSymbol,
                                    PF_DETOUR_BINARY_FINAL_CALLBACK pfFinal);
BOOL WINAPI DetourBinaryWrite(PDETOUR_BINARY pBinary, HANDLE hFile);
BOOL WINAPI DetourBinaryClose(PDETOUR_BINARY pBinary);

/////////////////////////////////////////////// First Chance Exception Filter.
//
LPTOP_LEVEL_EXCEPTION_FILTER WINAPI
DetourFirstChanceExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelFilter);

///////////////////////////////////////////////// Create Process & Inject Dll.
//
typedef BOOL (WINAPI *PDETOUR_CREATE_PROCESS_ROUTINEA)
    (LPCSTR lpApplicationName,
    LPSTR lpCommandLine,
    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    BOOL bInheritHandles,
    DWORD dwCreationFlags,
    LPVOID lpEnvironment,
    LPCSTR lpCurrentDirectory,
    LPSTARTUPINFOA lpStartupInfo,
    LPPROCESS_INFORMATION lpProcessInformation);

typedef BOOL (WINAPI *PDETOUR_CREATE_PROCESS_ROUTINEW)
    (LPCWSTR lpApplicationName,
    LPWSTR lpCommandLine,
    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    BOOL bInheritHandles,
    DWORD dwCreationFlags,
    LPVOID lpEnvironment,
    LPCWSTR lpCurrentDirectory,
    LPSTARTUPINFOW lpStartupInfo,
    LPPROCESS_INFORMATION lpProcessInformation);
                                 
BOOL WINAPI DetourCreateProcessWithDllA(LPCSTR lpApplicationName,
                                        LPSTR lpCommandLine,
                                        LPSECURITY_ATTRIBUTES lpProcessAttributes,
                                        LPSECURITY_ATTRIBUTES lpThreadAttributes,
                                        BOOL bInheritHandles,
                                        DWORD dwCreationFlags,
                                        LPVOID lpEnvironment,
                                        LPCSTR lpCurrentDirectory,
                                        LPSTARTUPINFOA lpStartupInfo,
                                        LPPROCESS_INFORMATION lpProcessInformation,
                                        LPCSTR lpDllName,
                                        PDETOUR_CREATE_PROCESS_ROUTINEA
                                        pfCreateProcessA);

BOOL WINAPI DetourCreateProcessWithDllW(LPCWSTR lpApplicationName,
                                        LPWSTR lpCommandLine,
                                        LPSECURITY_ATTRIBUTES lpProcessAttributes,
                                        LPSECURITY_ATTRIBUTES lpThreadAttributes,
                                        BOOL bInheritHandles,
                                        DWORD dwCreationFlags,
                                        LPVOID lpEnvironment,
                                        LPCWSTR lpCurrentDirectory,
                                        LPSTARTUPINFOW lpStartupInfo,
                                        LPPROCESS_INFORMATION lpProcessInformation,
                                        LPCWSTR lpDllName,
                                        PDETOUR_CREATE_PROCESS_ROUTINEW
                                        pfCreateProcessW);
                 
#ifdef UNICODE
#define DetourCreateProcessWithDll  DetourCreateProcessWithDllW
#define PDETOUR_CREATE_PROCESS_ROUTINE    PDETOUR_CREATE_PROCESS_ROUTINEW
#else
#define DetourCreateProcessWithDll  DetourCreateProcessWithDllA
#define PDETOUR_CREATE_PROCESS_ROUTINE    PDETOUR_CREATE_PROCESS_ROUTINEA
#endif // !UNICODE

BOOL WINAPI DetourContinueProcessWithDllA(HANDLE hProcess, LPCSTR lpDllName);
BOOL WINAPI DetourContinueProcessWithDllW(HANDLE hProcess, LPCWSTR lpDllName);

#ifdef UNICODE
#define DetourContinueProcessWithDll    DetourContinueProcessWithDllW
#else
#define DetourContinueProcessWithDll    DetourContinueProcessWithDllA
#endif // !UNICODE
//
//////////////////////////////////////////////////////////////////////////////
#ifdef __cplusplus
}
#endif // __cplusplus

/////////////////////////////////////////////////////////////////// Old Names.
//
#define ContinueProcessWithDll            DetourContinueProcessWithDll
#define ContinueProcessWithDllA          DetourContinueProcessWithDllA
#define ContinueProcessWithDllW          DetourContinueProcessWithDllW
#define CreateProcessWithDll              DetourCreateProcessWithDll
#define CreateProcessWithDllA            DetourCreateProcessWithDllA
#define CreateProcessWithDllW            DetourCreateProcessWithDllW
#define DETOUR_TRAMPOLINE_WO_TARGET      DETOUR_TRAMPOLINE_EMPTY
#define DetourBinaryPurgePayload          DetourBinaryPurgePayloads
#define DetourEnumerateExportsForInstance DetourEnumerateExports
#define DetourEnumerateInstances          DetourEnumerateModules
#define DetourFindEntryPointForInstance  DetourGetEntryPoint
#define DetourFindFinalCode              DetourGetFinalCode
#define DetourFindPayloadInBinary        DetourFindPayload
#define DetourGetSizeOfBinary            DetourGetSizeOfPayloads
#define DetourRemoveWithTrampoline        DetourRemove
#define PCREATE_PROCESS_ROUTINE          PDETOUR_CREATE_PROCESS_ROUTINE
#define PCREATE_PROCESS_ROUTINEA          PDETOUR_CREATE_PROCESS_ROUTINEA
#define PCREATE_PROCESS_ROUTINEW          PDETOUR_CREATE_PROCESS_ROUTINEW
//

//////////////////////////////////////////////// Detours Internal Definitions.
//
#ifdef __cplusplus
#ifdef DETOURS_INTERNAL

//////////////////////////////////////////////////////////////////////////////
//
#ifdef IMAGEAPI // defined by IMAGEHLP.H
typedef LPAPI_VERSION (NTAPI *PF_ImagehlpApiVersionEx)(LPAPI_VERSION AppVersion);

typedef BOOL (NTAPI *PF_SymInitialize)(IN HANDLE hProcess,
                                      IN LPSTR UserSearchPath,
                                      IN BOOL fInvadeProcess);
typedef DWORD (NTAPI *PF_SymSetOptions)(IN DWORD SymOptions);
typedef DWORD (NTAPI *PF_SymGetOptions)(VOID);
typedef BOOL (NTAPI *PF_SymLoadModule)(IN HANDLE hProcess,
                                      IN HANDLE hFile,
                                      IN PSTR ImageName,
                                      IN PSTR ModuleName,
                                      IN DWORD BaseOfDll,
                                      IN DWORD SizeOfDll);
typedef BOOL (NTAPI *PF_SymGetModuleInfo)(IN HANDLE hProcess,
                                          IN DWORD dwAddr,
                                          OUT PIMAGEHLP_MODULE ModuleInfo);
typedef BOOL (NTAPI *PF_SymGetSymFromName)(IN HANDLE hProcess,
                                          IN LPSTR Name,
                                          OUT PIMAGEHLP_SYMBOL Symbol);
typedef BOOL (NTAPI *PF_BindImage)(IN LPSTR pszImageName,
                                  IN LPSTR pszDllPath,
                                  IN LPSTR pszSymbolPath);

typedef struct _DETOUR_SYM_INFO
{
    HANDLE                  hProcess;
    HMODULE                  hImageHlp;
    PF_ImagehlpApiVersionEx  pfImagehlpApiVersionEx;
    PF_SymInitialize        pfSymInitialize;
    PF_SymSetOptions        pfSymSetOptions;
    PF_SymGetOptions        pfSymGetOptions;
    PF_SymLoadModule        pfSymLoadModule;
    PF_SymGetModuleInfo      pfSymGetModuleInfo;
    PF_SymGetSymFromName    pfSymGetSymFromName;
    PF_BindImage            pfBindImage;
} DETOUR_SYM_INFO, *PDETOUR_SYM_INFO;

PDETOUR_SYM_INFO DetourLoadImageHlp(VOID);

#endif // IMAGEAPI

//////////////////////////////////////////////////////////////////////////////
//
class CDetourEnableWriteOnCodePage
{
public:
    CDetourEnableWriteOnCodePage(PBYTE pbCode, LONG cbCode = DETOUR_TRAMPOLINE_SIZE)
    {
        m_pbCode = pbCode;
        m_cbCode = cbCode;
        m_dwOldPerm = 0;
        m_hProcess = GetCurrentProcess();

        if (m_pbCode && m_cbCode) {
            if (!FlushInstructionCache(m_hProcess, pbCode, cbCode)) {
                return;
            }
            if (!VirtualProtect(pbCode,
                                cbCode,
                                PAGE_EXECUTE_READWRITE,
                                &m_dwOldPerm)) {
                return;
            }
        }
    }

    ~CDetourEnableWriteOnCodePage()
    {
        if (m_dwOldPerm && m_pbCode && m_cbCode) {
            DWORD dwTemp = 0;
            if (!FlushInstructionCache(m_hProcess, m_pbCode, m_cbCode)) {
                return;
            }
            if (!VirtualProtect(m_pbCode, m_cbCode, m_dwOldPerm, &dwTemp)) {
                return;
            }
        }
    }

    BOOL SetPermission(DWORD dwPerms)
    {
        if (m_dwOldPerm && m_pbCode && m_cbCode) {
            m_dwOldPerm = dwPerms;
            return TRUE;
        }
        return FALSE;
    }

    BOOL IsValid(VOID)
    {
        return m_pbCode && m_cbCode && m_dwOldPerm;
    }

private:
    HANDLE  m_hProcess;
    PBYTE  m_pbCode;
    LONG    m_cbCode;
    DWORD  m_dwOldPerm;
};

//////////////////////////////////////////////////////////////////////////////
//
inline PBYTE DetourGenMovEax(PBYTE pbCode, UINT32 nValue)
{
    *pbCode++ = 0xB8;
    *((UINT32*&)pbCode)++ = nValue;
    return pbCode;
}

inline PBYTE DetourGenMovEbx(PBYTE pbCode, UINT32 nValue)
{
    *pbCode++ = 0xBB;
    *((UINT32*&)pbCode)++ = nValue;
    return pbCode;
}

inline PBYTE DetourGenMovEcx(PBYTE pbCode, UINT32 nValue)
{
    *pbCode++ = 0xB9;
    *((UINT32*&)pbCode)++ = nValue;
    return pbCode;
}

inline PBYTE DetourGenMovEdx(PBYTE pbCode, UINT32 nValue)
{
    *pbCode++ = 0xBA;
    *((UINT32*&)pbCode)++ = nValue;
    return pbCode;
}

inline PBYTE DetourGenMovEsi(PBYTE pbCode, UINT32 nValue)
{
    *pbCode++ = 0xBE;
    *((UINT32*&)pbCode)++ = nValue;
    return pbCode;
}

inline PBYTE DetourGenMovEdi(PBYTE pbCode, UINT32 nValue)
{
    *pbCode++ = 0xBF;
    *((UINT32*&)pbCode)++ = nValue;
    return pbCode;
}

inline PBYTE DetourGenMovEbp(PBYTE pbCode, UINT32 nValue)
{
    *pbCode++ = 0xBD;
    *((UINT32*&)pbCode)++ = nValue;
    return pbCode;
}

inline PBYTE DetourGenMovEsp(PBYTE pbCode, UINT32 nValue)
{
    *pbCode++ = 0xBC;
    *((UINT32*&)pbCode)++ = nValue;
    return pbCode;
}

inline PBYTE DetourGenPush(PBYTE pbCode, UINT32 nValue)
{
    *pbCode++ = 0x68;
    *((UINT32*&)pbCode)++ = nValue;
    return pbCode;
}

inline PBYTE DetourGenPushad(PBYTE pbCode)
{
    *pbCode++ = 0x60;
    return pbCode;
}

inline PBYTE DetourGenPopad(PBYTE pbCode)
{
    *pbCode++ = 0x61;
    return pbCode;
}

inline PBYTE DetourGenJmp(PBYTE pbCode, PBYTE pbJmpDst, PBYTE pbJmpSrc = 0)
{
    if (pbJmpSrc == 0) {
        pbJmpSrc = pbCode;
    }
    *pbCode++ = 0xE9;
    *((INT32*&)pbCode)++ = pbJmpDst - (pbJmpSrc + 5);
    return pbCode;
}

inline PBYTE DetourGenCall(PBYTE pbCode, PBYTE pbJmpDst, PBYTE pbJmpSrc = 0)
{
    if (pbJmpSrc == 0) {
        pbJmpSrc = pbCode;
    }
    *pbCode++ = 0xE8;
    *((INT32*&)pbCode)++ = pbJmpDst - (pbJmpSrc + 5);
    return pbCode;
}

inline PBYTE DetourGenBreak(PBYTE pbCode)
{
    *pbCode++ = 0xcc;
    return pbCode;
}

inline PBYTE DetourGenRet(PBYTE pbCode)
{
    *pbCode++ = 0xc3;
    return pbCode;
}

inline PBYTE DetourGenNop(PBYTE pbCode)
{
    *pbCode++ = 0x90;
    return pbCode;
}
#endif DETOURS_INTERAL
#endif // __cplusplus

#endif // _DETOURS_H_
//
////////////////////////////////////////////////////////////////  End of File.


Code:
//Window Mode Toggler By UnknownPK
//Just a small project that will force modt d3d9 game to toggle window mode
//its no big deal as it just emulates what normaly happens when a game lose its device

//and all you ppl who's been there all the years

#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)
      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)
      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;

}

DWORD InitializeD3DHooks(LPVOID /*pv*/)
{
   
   //LoadLibrary("d3d9.dll");
   D3DPRESENT_PARAMETERS d3dpp;
   LPDIRECT3DDEVICE9 pd3dDevice;
   IDirect3D9 *pd3dInterface = 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 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;
}


Other Require

[You must be registered and logged in to see this link.]

[url=facebook.com/itzmegrayhamz]FB Account ! EHEHHEHE[/url]
Back to top Go down
View user profile
 

Window mode toggler source code

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
CrackAndLoad :: Computer Programming :: C++ Programming-