变速齿轮,游戏加速的实现

这是本人几年前做游戏浏览器加速的时候的部分代码,当时查了很多资料,很多资料和代码都不全或者有问题经过差不多两周的时间才解决了这个问题,搞微软的东西就是这样,又不是很牛逼的东西又搞的累得半死

1,用dll注入到要控制的进程

2,利用MinHook(这个库可以在CSDN上找到)这个库hook住三个时间API,分别是GetTickCount,timeGetTime,QueryPerformanceCounter

部分主要代码如下(忽略掉了注入的代码)

其中m_s_iSpeedTimes是加速的倍速

MH_Initialize();
MH_CreateHook(&GetTickCount, &NewGetTickCount2,   
    reinterpret_cast(&s_oldGetTickCount));  
MH_EnableHook(&GetTickCount);  
  
MH_CreateHook(&timeGetTime, &NewTimeGetTime2,   
    reinterpret_cast(&s_oldTimeGetTime));  
MH_EnableHook(&timeGetTime);  
  
MH_CreateHook(&QueryPerformanceCounter, &NewQueryPerformanceCounter,   
    reinterpret_cast(&s_oldQueryPerformanceCounter));  
MH_EnableHook(&QueryPerformanceCounter);  
  
DWORD WINAPI CBrowser::NewTimeGetTime2(void)  
{  
    static DWORD fake = 0;  
    static DWORD last_real = 0;  
    DWORD now = (*s_oldTimeGetTime)();  
    DWORD result;  
  
    if(last_real == 0)  
    {  
        result = fake = last_real = now;  
    }  
    else  
    {  
        result = fake + m_s_iSpeedTimes * (now - last_real);   
        fake = result;  
        last_real = now;  
    }  
  
    return result;  
}  
  
DWORD WINAPI CBrowser::NewGetTickCount2(void)  
{  
    static DWORD fake = 0;  
    static DWORD last_real = 0;  
    DWORD now = (*s_oldGetTickCount)();  
    DWORD result;  
  
    if(last_real == 0)  
    {  
        result = fake = last_real = now;  
    }  
    else  
    {  
        result = fake + m_s_iSpeedTimes * (now - last_real);   
        fake = result;  
        last_real = now;  
    }  
  
    return result;  
}  
  
BOOL WINAPI CBrowser::NewQueryPerformanceCounter(__out LARGE_INTEGER *lpPerformanceCount)  
{  
    BOOL ret = (*s_oldQueryPerformanceCounter)(lpPerformanceCount);  
    if(!ret) return ret;  
  
    static LARGE_INTEGER fake = {0};  
    static LARGE_INTEGER last_real = {0};  
    LARGE_INTEGER now = *lpPerformanceCount;  
  
    if(last_real.QuadPart == 0)  
    {  
        fake = last_real = now;  
    }  
    else  
    {  
        lpPerformanceCount->QuadPart = fake.QuadPart + m_s_iSpeedTimes * (now.QuadPart - last_real.QuadPart);   
        fake = *lpPerformanceCount;  
        last_real = now;  
    }  
  
    return ret;  
} 


你可能感兴趣的:(C/C++)