Inquiridor
C# - Menu D3D no Windows 8

Pergunta
-
Olá pessoal do MSDN, estou com uma duvida na linguagem C#, eu estou tentando criar um menu D3D e estou copilando o projeto em DLL para injetar em um jogo, mas eu injeto tudo normalmente so que ao aperta INSERT(tecla escolhida para fazer o menu aparecer/esconder) ele nao aparece, mas o injetor diz que foi injetada com sucesso.
Segue o codigo:
Main.h
#include <Windows.h> #include <d3d11.h> #pragma comment(lib, "d3d11.lib") #pragma comment(lib, "FW1FontWrapper.lib") #include "FW1FontWrapper.h" ID3D11Device *pDevice; IFW1Factory *pFW1Factory; IFW1FontWrapper *pFont; ID3D11DeviceContext *pContext; DWORD* pSwapChainVtable; DWORD* pDeviceContextVTable; void* detourBuffer; const void* __cdecl DetourFunc(BYTE* src, const BYTE* dest, const DWORD length) { BYTE* jump = new BYTE[length + 5]; detourBuffer = jump; DWORD dwVirtualProtectBackup; VirtualProtect(src, length, PAGE_READWRITE, &dwVirtualProtectBackup); memcpy(jump, src, length); jump += length; jump[0] = 0xE9; *(DWORD*)(jump + 1) = (DWORD)(src + length - jump) - 5; src[0] = 0xE9; *(DWORD*)(src + 1) = (DWORD)(dest - src) - 5; VirtualProtect(src, length, dwVirtualProtectBackup, &dwVirtualProtectBackup); return jump - length; } typedef HRESULT (__stdcall *D3D11PresentHook) (IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags); typedef void (__stdcall *D3D11DrawIndexedHook) (ID3D11DeviceContext* pContext, UINT IndexCount, UINT StartIndexLocation, INT BaseVertexLocation); typedef void (__stdcall *D3D11ClearRenderTargetViewHook) (ID3D11DeviceContext* pContext, ID3D11RenderTargetView *pRenderTargetView, const FLOAT ColorRGBA[4]); D3D11PresentHook phookD3D11Present = NULL; D3D11DrawIndexedHook phookD3D11DrawIndexed = NULL; D3D11ClearRenderTargetViewHook phookD3D11ClearRenderTargetView = NULL;
Main.cpp
#include "Main.h" #include "JBMenu.h" JBMenu Menu; #define ORANGE 0xFF00BFFF #define BLACK 0xFF000000 #define WHITE 0xFFFFFFFF #define GREEN 0xFF00FF00 #define RED 0xFFFF0000 #define GRAY 0xFF2F4F4F int Folder1 = 1; int Item1 = 0; int Item2 = 0; int Item3 = 1; int Item4 = 0; void Do_Menu() { Menu.AddFolder(L"Folder1",&Folder1); Menu.AddOption(L"Item1",&Item1,&Folder1); Menu.AddOption(L"Item2",&Item2,&Folder1); Menu.AddOption(L"Item3",&Item3,&Folder1); Menu.AddOption(L"Item4",&Item4,&Folder1); } HRESULT __stdcall hookD3D11Present(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags) { if (!pFW1Factory) { pSwapChain->GetDevice(__uuidof(pDevice), (void**)&pDevice); pDevice->GetImmediateContext(&pContext); FW1CreateFactory(FW1_VERSION, &pFW1Factory); pFW1Factory->CreateFontWrapper(pDevice, L"Tahoma", &pFont); pFW1Factory->Release(); } if(Menu.IsReady() == false) { Menu.Init_Menu(pFont,pContext,L"4LV3S D3D11",25,100); Do_Menu(); Menu.Color_Font = WHITE; Menu.Color_Off = RED; Menu.Color_On = GREEN; Menu.Color_Folder = ORANGE; Menu.Color_Current = GRAY; } Menu.Draw_Menu(); Menu.Navigation(); if(Item4) Menu.DrawTextW(L"Credits: 4LV3S",15,400,100,ORANGE); return phookD3D11Present(pSwapChain, SyncInterval, Flags); } void __stdcall hookD3D11DrawIndexed(ID3D11DeviceContext* pContext, UINT IndexCount, UINT StartIndexLocation, INT BaseVertexLocation) { return phookD3D11DrawIndexed(pContext, IndexCount, StartIndexLocation, BaseVertexLocation); } void __stdcall hookD3D11ClearRenderTargetView(ID3D11DeviceContext* pContext, ID3D11RenderTargetView *pRenderTargetView, const FLOAT ColorRGBA[4]) { return phookD3D11ClearRenderTargetView(pContext, pRenderTargetView, ColorRGBA); } DWORD __stdcall InitializeHook(LPVOID) { HWND hWnd = GetForegroundWindow(); IDXGISwapChain* pSwapChain; D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0; DXGI_SWAP_CHAIN_DESC swapChainDesc; ZeroMemory(&swapChainDesc, sizeof(swapChainDesc)); swapChainDesc.BufferCount = 1; swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; swapChainDesc.OutputWindow = hWnd; swapChainDesc.SampleDesc.Count = 1; swapChainDesc.Windowed = (GetWindowLong(hWnd, GWL_STYLE) & (WS_POPUP != 0)) ? false : true; swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; if (FAILED(D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, &featureLevel, 1 , D3D11_SDK_VERSION, &swapChainDesc, &pSwapChain, &pDevice, NULL, &pContext))) { MessageBoxA(hWnd, "Failed to create directX device and swapchain!", "uBoos?", MB_ICONERROR); return NULL; } pSwapChainVtable = (DWORD*)pSwapChain; pSwapChainVtable = (DWORD*)pSwapChainVtable[0]; pDeviceContextVTable = (DWORD*)pContext; pDeviceContextVTable = (DWORD*)pDeviceContextVTable[0]; phookD3D11Present = (D3D11PresentHook)DetourFunc((BYTE*)pSwapChainVtable[8], (BYTE*)hookD3D11Present, 5); phookD3D11DrawIndexed = (D3D11DrawIndexedHook)DetourFunc((BYTE*)pDeviceContextVTable[12], (BYTE*)hookD3D11DrawIndexed, 5); phookD3D11ClearRenderTargetView = (D3D11ClearRenderTargetViewHook)DetourFunc((BYTE*)pDeviceContextVTable[50], (BYTE*)hookD3D11ClearRenderTargetView, 5); DWORD dwOld; VirtualProtect(phookD3D11Present, 2, PAGE_EXECUTE_READWRITE, &dwOld); pDevice->Release(); pContext->Release(); pSwapChain->Release(); return NULL; } BOOL __stdcall DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved) { if (dwReason == DLL_PROCESS_ATTACH) { CreateThread(NULL, NULL, InitializeHook, NULL, NULL, NULL); } else if (dwReason == DLL_PROCESS_DETACH) { pFont->Release(); delete[] detourBuffer; } return TRUE; }
JBMenu.h
/* Name: JustBasics D3D11 Menu Version: 0.1 Date: 01-01-14 *On/Off (Colored) *Folder *Keyboard control (Insert = Hide/Show | Arrowkeys for select) *Using FW1FontWrapper for Font Crediting: *JustBasics (Menu Files) *evolution536 (For his D3D11 Hook) *UC-Forums *FW1FontWrapper */ #pragma once #include <d3d11.h> #pragma comment(lib, "FW1FontWrapper.lib") #include "FW1FontWrapper.h" #define MAX_ITEMS 25 #define T_FOLDER 1 #define T_OPTION 2 #define LineH 15 struct Options{ LPCWSTR Name; int *Function; BYTE Type; }; struct Menu{ LPCWSTR Title; int x; int y; int w; }; class JBMenu { public: JBMenu(void); void Init_Menu(IFW1FontWrapper *pFont,ID3D11DeviceContext *pContext,LPCWSTR Title,int x,int y); void DrawText(LPCWSTR text,int FontSize,int x, int y,DWORD Col); void Draw_Menu(); void Navigation(); void AddFolder(LPCWSTR Name,int *Pointer); void AddOption(LPCWSTR Name,int *Pointer,int *Folder); bool IsReady(); DWORD Color_Font; DWORD Color_On; DWORD Color_Off; DWORD Color_Folder; DWORD Color_Current; private: ID3D11DeviceContext *pContext; IFW1FontWrapper *pFont; bool Is_Ready,Visible; int Items,Cur_Pos; Options sOptions[MAX_ITEMS]; Menu sMenu; };
JBMenu.cpp
#include "JBMenu.h" JBMenu::JBMenu(void) { this->Visible = true; } void JBMenu::Init_Menu(IFW1FontWrapper *pFont,ID3D11DeviceContext *pContext,LPCWSTR Title,int x,int y) { this->pContext = pContext; this->pFont = pFont; this->Is_Ready = true; this->sMenu.Title = Title; this->sMenu.x = x; this->sMenu.y = y; } void JBMenu::AddFolder(LPCWSTR Name,int *Pointer) { sOptions[this->Items].Name = (LPCWSTR)Name; sOptions[this->Items].Function = Pointer; sOptions[this->Items].Type = T_FOLDER; this->Items++; } void JBMenu::AddOption(LPCWSTR Name,int *Pointer,int *Folder) { if(*Folder==0) return; sOptions[this->Items].Name = Name; sOptions[this->Items].Function = Pointer; sOptions[this->Items].Type = T_OPTION; this->Items++; } void JBMenu::Navigation() { if(GetAsyncKeyState(VK_INSERT)&1) this->Visible =! this->Visible; if(!this->Visible) return; int value=0; if(GetAsyncKeyState(VK_DOWN)&1) { this->Cur_Pos++; if(sOptions[this->Cur_Pos].Name == 0) this->Cur_Pos--; } if(GetAsyncKeyState(VK_UP)&1) { this->Cur_Pos--; if(this->Cur_Pos == -1) this->Cur_Pos++; } else if(GetAsyncKeyState(VK_RIGHT)&1) { if(*sOptions[this->Cur_Pos].Function == 0) value++; } else if((GetAsyncKeyState(VK_LEFT)&1) && *sOptions[this->Cur_Pos].Function == 1) { value--; } if(value){ *sOptions[this->Cur_Pos].Function += value; if(sOptions[this->Cur_Pos].Type == T_FOLDER) { memset(&sOptions,0,sizeof(sOptions)); this->Items = 0; } } } void JBMenu::Draw_Menu() { if(!this->Visible) return; this->DrawText(this->sMenu.Title,14,sMenu.x+10,sMenu.y,this->Color_Font); for(int i=0;i < this->Items;i++) { if(this->sOptions[i].Type == T_OPTION) { if(*this->sOptions[i].Function) { this->DrawText(L"[ON]",12,sMenu.x+150,sMenu.y+LineH*(i+2),this->Color_On); }else{ this->DrawText(L"[OFF]",12,sMenu.x+150,sMenu.y+LineH*(i+2),this->Color_Off); } } if(this->sOptions[i].Type == T_FOLDER) { if(*this->sOptions[i].Function) { this->DrawText(L"Opend",12,sMenu.x+150,sMenu.y+LineH*(i+2),this->Color_Folder); }else{ this->DrawText(L"Closed",12,sMenu.x+150,sMenu.y+LineH*(i+2),this->Color_Folder); } } DWORD Color = this->Color_Font; if(this->Cur_Pos == i) Color = this->Color_Current; this->DrawText(this->sOptions[i].Name,12,sMenu.x+5,sMenu.y+LineH*(i+2),Color); } } bool JBMenu::IsReady() { if(this->Items) return true; return false; } void JBMenu::DrawText(LPCWSTR text,int FontSize,int x, int y,DWORD Col) { if(this->Is_Ready==false) MessageBoxA(0,"Erro, Não foi possivel iniciar o menu!","Erro",MB_OK); pFont->DrawString(pContext,text, (float)FontSize, (float)x,(float) y, Col, FW1_RESTORESTATE); }
Agradeço desde ja a qualquer ajuda! ^_^
Todas as Respostas
-
Bom dia 4LV3S,
Então além de um comportamento diferente do esperado, nenhum erro com o código/aplicação está ocorrendo. É isso?
Marcos SJ Esse conteúdo e fornecido sem garantias de qualquer tipo, seja expressa ou implícita
MSDN Community Support
Por favor, lembre-se de Marcar como Resposta as postagens que resolveram o seu problema. Essa é uma maneira comum de reconhecer aqueles que o ajudaram e fazer com que seja mais fácil para os outros visitantes encontrarem a resolução mais tarde.
-