bool GetD3DAdr(){
bool bRet = false;
LPDIRECT3D9 Tmp_pD3D = NULL;
LPDIRECT3DDEVICE9 Tmp_pd3dDevice = NULL;
if (NULL == ::GetModuleHandleA(D3D9DLLName)){
goto Exit0;
}
if (NULL == (Tmp_pD3D = Direct3DCreate9(D3D_SDK_VERSION))){
goto Exit0;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
if (S_OK == Tmp_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,GetDesktopWindow(),
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,&Tmp_pd3dDevice)){
try{
g_Adr_DrawIndexedPrimitive = *(PDWORD)(*(PDWORD)Tmp_pd3dDevice + g_Oft_DrawIndexedPrimitive);
g_Adr_Present = *(PDWORD)(*(PDWORD)Tmp_pd3dDevice + g_Oft_Present);
LogDbg("g_Adr_DrawIndexedPrimitive:%.8X g_Adr_Present:%.8X",g_Adr_DrawIndexedPrimitive,g_Adr_Present);
bRet = true;
}
catch (...){
}
}
Exit0:
if( Tmp_pd3dDevice != NULL ){
Tmp_pd3dDevice->Release();
}
if( Tmp_pD3D != NULL ){
Tmp_pD3D->Release();
}
return bRet;
}
void __stdcall HookD3D9_DrawIndexedPrimitive_Mem(bool bHook)
{
if (bHook){
g_Stride = GetPrivateProfileInt(Ini_App,Ini_Key_Stride,CN_D3D_Stride,g_Cfg);
g_KeyBoardSwitch = GetPrivateProfileInt(Ini_App,Ini_Key_KeyBoardSwitch,0,g_Cfg) > 0 ? true:false;
OldDrawIndexedPrimitive = (TDrawIndexedPrimitive)g_Adr_DrawIndexedPrimitive;
//此处写内存即可实现Hook
*(PDWORD)(*(PDWORD)(g_Param_DrawIndexedPrimitive) + g_Oft_DrawIndexedPrimitive) = DWORD(NewDrawIndexedPrimitive);
}
else{
OldDrawIndexedPrimitive = NULL;
//此处写内存恢复Hook
*(PDWORD)(*(PDWORD)(g_Param_DrawIndexedPrimitive) + g_Oft_DrawIndexedPrimitive) = g_Adr_DrawIndexedPrimitive;
}
HookKeyBoard(bHook);
}
HRESULT __stdcall NewDrawIndexedPrimitive(LPDIRECT3DDEVICE9 pDevice,D3DPRIMITIVETYPE type,INT BaseVertexIndex,UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount)
{
HRESULT hRet = S_OK;
IDirect3DVertexBuffer9* pStreamData = NULL;
UINT iOffsetInBytes,iStride;
if(D3D_OK == pDevice->GetStreamSource(0,&pStreamData,&iOffsetInBytes,&iStride)){
pStreamData->Release();
}
if(iStride == g_Stride){
pDevice->SetRenderState(D3DRS_ZENABLE,FALSE);//禁用Z轴缓冲
hRet = OldDrawIndexedPrimitive(pDevice,type,BaseVertexIndex,MinVertexIndex,NumVertices,startIndex,primCount);
pDevice->SetRenderState(D3DRS_ZENABLE,TRUE );//恢复Z轴缓冲
}
else{
hRet = OldDrawIndexedPrimitive(pDevice,type,BaseVertexIndex,MinVertexIndex,NumVertices,startIndex,primCount);
}
return hRet;
}
void __stdcall HookD3D9_Present_Mem(bool bHook)
{
if (bHook){
g_CpuSleep = GetPrivateProfileIntA(Ini_App,Ini_Key_CpuSleep,0,g_Cfg);
LogDbg("%d",g_CpuSleep);
OldPresent = (TPresent)g_Adr_Present;
*(PDWORD)(*(PDWORD)(g_Param_DrawIndexedPrimitive) + g_Oft_Present) = DWORD(NewPresent);
}
else{
OldPresent = NULL;
*(PDWORD)(*(PDWORD)(g_Param_DrawIndexedPrimitive) + g_Oft_Present) = g_Adr_Present;
}
}
HRESULT __stdcall NewPresent(LPDIRECT3DDEVICE9 pDevice,const RECT *pSourceRect,const RECT *pDestRect,HWND hDestWindowOverride,const RGNDATA *pDirtyRegion)
{
HRESULT hRet = S_OK;
if (-1 == g_CpuSleep){ //黑屏,直接返回
Sleep(168);
return hRet;
};
//执行原函数
hRet = OldPresent(pDevice,pSourceRect,pDestRect,hDestWindowOverride,pDirtyRegion);
//Sleep降压
if (g_CpuSleep > 0){
Sleep(g_CpuSleep);
}
return hRet;
}
void __stdcall GetD3DParam_Hook()
{
while (0 == g_Param_DrawIndexedPrimitive){
//这边如此操作,是为了降低被针对性Crc检测到概率
HookDrawIndexedPrimitiveParam(true);
Sleep(2);
HookDrawIndexedPrimitiveParam(false);
Sleep(168);
}
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课