Source Code Robust D3D11 Dummy Creator with Interfaces C++

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

SICGames88

Newbie
Full Member
Nobleman
Sep 6, 2015
70
768
0
How long you been coding/hacking?
Since 8 years old but on and off. Not consistent.
I was looking through my Pixiebot code and was disguised. The code was so redundant. Over and over again, creating same lines of code. I took Rake's D3D11 x64 Present hook example. Also, the KoreWindow is fully customizable. So, this should be great for Dummy Window creation and dummy d3d11 objects. I tested this out on Fallout 4. It acted oddly. More about that later.

IKoreInterfaces.h:
#pragma once
#include "Core.h"

namespace Kore
{
    class KoreWindow;

    __interface IKoreWindowBase {
        bool Create(LPCWSTR, LPCWSTR, HINSTANCE, WNDPROC, int, int, int, bool);
        void SetTransparent(void);
        void SetPosition(int, int, LPCWSTR);
        void Show();
        void Update();
        void Destroy(void);
        int GetHeight();
        int GetWidth();

    };
    __interface IKore3D
    {
        bool Create(KoreWindow *);
        void BeginRender(ID3D11DeviceContext*, ID3D11RenderTargetView *, const FLOAT *);
        void EndRender(void);
        void Destroy(void);
        ID3D11Device *GetDevice(void);
        ID3D11DeviceContext *GetDeviceContext(void);
        IDXGISwapChain *GetSwapChain(void);

    };

    __interface IKore2D {
        bool Create(KoreWindow *KoreWindow);
        void BeginRender();
        void Draw();
        void DrawImage(ID2D1Bitmap *);
        void DrawRectangle(D2D_RECT_F &, float);
        bool LoadImageFile(LPCWSTR filename, ID2D1Bitmap *);
        ID2D1Bitmap *GetBitmap();

        void EndRender();
        void Destroy();
    };
}
Kore.h (Core.h):
#pragma once
#include <Windows.h>
#include <DXGI.h>
#include <d3d11.h>
#include <D3DX11.h>
#include <D3DX11tex.h>
#include <d3dcompiler.h>

#include <d3d10_1.h>
#include <d3d10.h>
#include <D3DX10.h>
#include <d2d1.h>
#include <dwrite.h>
#include <math.h>

//-- DirectXTK Headers
#include <DirectXTex.h>
#include <ScreenGrab.h>
#include <wrl/client.h>
#include <wincodec.h>

#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "d3dcompiler.lib")

#pragma comment(lib, "windowscodecs.lib")
#pragma comment(lib, "DirectXTK.lib")
#pragma comment(lib, "DirectXTex.lib")

#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "psapi.lib")
#pragma comment(lib, "detours.lib")

#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwrite.lib")


#include <Loggy.h>
#pragma comment(lib, "LoggyLib.lib")

#define SAFE_RELEASE(x) if(x) x->Release();

#include "IKoreInterfaces.h"

using namespace Loggy;
using namespace Microsoft::WRL;

namespace Kore
{

    class KoreWindow : public IKoreWindowBase
    {
    public:
        KoreWindow();
        ~KoreWindow();
        HWND GetHWND();
        WNDCLASSEX GetWindowClass();
        bool Create(LPCWSTR szClassname, LPCWSTR szTitlename, HINSTANCE hInstance, WNDPROC wndproc, int width, int height, int dwstyles, bool bIsDummy);
        void SetTransparent();
        void SetPosition(int, int, LPCWSTR);
        void Show();
        void Update();
        void Destroy();
        int GetHeight();
        int GetWidth();

        HWND hWnd;
        WNDCLASSEX wc = { 0 };
        int pheight, pwidth;
    private:
    protected:
    };
    
    class KoreD3D11: public IKore3D
    {
    public:
        KoreD3D11();
        ~KoreD3D11();
        bool Create(KoreWindow *);
        void BeginRender(ID3D11DeviceContext *, ID3D11RenderTargetView *, const FLOAT *);
        void EndRender(void);
        void Destroy(void);
        ID3D11Device *GetDevice(void);
        ID3D11DeviceContext *GetDeviceContext(void);
        IDXGISwapChain *GetSwapChain(void);

        ID3D11Device *dev;
        ID3D11DeviceContext *devCon;
        IDXGISwapChain *swapchain;
    private:
    protected:
    };

    class KoreD2D : public IKore2D
    {
    public:
        KoreD2D();
        ~KoreD2D();
        bool Create(KoreWindow *KoreWindow);
        void BeginRender();
        void Draw();
        void DrawImage(ID2D1Bitmap *);
        void DrawRectangle(D2D1_RECT_F &rect, float thickness);

        bool LoadImageFile(LPCWSTR file, ID2D1Bitmap *);
        ID2D1Bitmap *GetBitmap();
        void EndRender();
        void Destroy();

        ID2D1SolidColorBrush * SolidBrush;
        ID2D1RenderTarget *D2DRenderTarget;
        IDWriteFactory *DwriteFactory;
        ID2D1Factory *D2D1Factory;
        IDWriteTextFormat *TextFormat;
        D2D1::Matrix3x2F World, translation, rotation, scale, skew;
        D2D1_COLOR_F selectionBrushColor;
        ID2D1Bitmap *_bmp;
        IDXGISwapChain *_swapchain;
        ID3D11Device *_device;
        ID3D11DeviceContext *_devicecontext;
        ID3D11RenderTargetView *_rendertargetview;

    private:
    };
}
Kore.cpp:
#include "Core.h"

using namespace Kore;

KoreWindow::KoreWindow() {
    hWnd = NULL;
}
KoreWindow::~KoreWindow() {

}
bool KoreWindow::Create(LPCWSTR className, LPCWSTR titleName, HINSTANCE hInst, WNDPROC wndProc, int width, int height, int dwStyle, bool bIsDummy)
{
    //-- this Dummy Window Creation was obtained from Rake's x64 DX11 Hook Present. And the Dummy Device Creation.
    
    wc.cbSize = sizeof(wc);
    wc.lpfnWndProc = wndProc;
    wc.lpszClassName = className;
    
    if (!bIsDummy) {
        wc.hbrBackground = (HBRUSH)RGB(0, 0, 0);
        wc.hInstance = hInst;
    }
    
    if (!RegisterClassEx(&wc)) {
        Logger::Write("Unable to create Kore Window Class.", LOGTYPE::WARNING);
        return NULL;
    }
    hWnd = CreateWindowW(wc.lpszClassName, titleName, dwStyle, 0, 0, width,height, NULL, NULL, hInst, nullptr);
    if (!hWnd)
        return false;

    pwidth = width;
    pheight = height;

    return true;
}
void KoreWindow::SetTransparent() {
    SetWindowLong(hWnd, GWL_EXSTYLE, (int)GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT);
    SetLayeredWindowAttributes(hWnd, RGB(0, 0, 0), 0, ULW_COLORKEY);
    SetLayeredWindowAttributes(hWnd, 0, 255, LWA_ALPHA);
}
void KoreWindow::SetPosition(int x, int y, LPCWSTR windowName)
{
    SetWindowPos(FindWindow(NULL, windowName), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
HWND KoreWindow::GetHWND() {
    return hWnd;
}
WNDCLASSEX KoreWindow::GetWindowClass() {
    return wc;
}
void KoreWindow::Show() {
    ShowWindow(GetHWND(), 0);
}
void KoreWindow::Update() {
    UpdateWindow(GetHWND());

}
void KoreWindow::Destroy()
{
    DestroyWindow(hWnd);
    UnregisterClass(wc.lpszClassName, GetModuleHandle(nullptr));
}
int KoreWindow::GetHeight() {
    return pheight;
}
int KoreWindow::GetWidth() {
    return pwidth;

}
KoreD3D11::KoreD3D11() {

}
KoreD3D11::~KoreD3D11() {

}

bool KoreD3D11::Create(KoreWindow *kWindow) {

    D3D_FEATURE_LEVEL featureLevel;
    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 = kWindow->GetHWND();
    swapChainDesc.SampleDesc.Count = 1;
    swapChainDesc.BufferDesc.RefreshRate = { 30, 1 };
    swapChainDesc.Windowed = true;
    swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
    swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_STRETCHED;
    swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;

    if (FAILED(D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, D3D11_CREATE_DEVICE_BGRA_SUPPORT, nullptr, 0
        , D3D11_SDK_VERSION, &swapChainDesc, &swapchain, &dev, &featureLevel, &devCon)))
    {
        Logger::Write("Failed to create D3D11 Swapchain and Device.", LOGTYPE::CRITICAL);
        kWindow->Destroy();
        return false;
    }
    else {
        Logger::Write("Successfully create dummy D3D11 Swapchain and Device.", LOGTYPE::MESSAGE);
    }

    return true;

}
void KoreD3D11::BeginRender(ID3D11DeviceContext * devCon, ID3D11RenderTargetView *rtv, const FLOAT* backcolor) {
    devCon->ClearRenderTargetView(rtv, backcolor);
}
void KoreD3D11::EndRender()
{
    GetSwapChain()->Present(0, 0);
}
ID3D11Device *KoreD3D11::GetDevice() {
    return dev;
}
ID3D11DeviceContext *KoreD3D11::GetDeviceContext() {
    return devCon;
}
IDXGISwapChain *KoreD3D11::GetSwapChain() {
    return swapchain;
}
void KoreD3D11::Destroy()
{
    try {
        SAFE_RELEASE(swapchain);
        SAFE_RELEASE(devCon);
        SAFE_RELEASE(dev);
    }
    catch (exception &ex) {
        char reason[MAX_PATH] = { 0 };
        sprintf_s(reason, MAX_PATH, "Exception Caught: %s", ex.what());
        Logger::Write(reason, LOGTYPE::CRITICAL);
    }
}


KoreD2D::KoreD2D() {

}
KoreD2D::~KoreD2D() {

}
bool KoreD2D::Create(KoreWindow *korewindow) {
    
    HRESULT result = S_OK;
    // create a struct to hold information about the swap chain
    DXGI_SWAP_CHAIN_DESC scd;
    HRESULT hr = S_OK;

    // clear out the struct for use
    ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));

    // fill the swap chain description struct

    scd.BufferCount = 1;                                    // one back buffer
    scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;     // use 32-bit color
    scd.BufferDesc.Width = korewindow->GetWidth();
    scd.BufferDesc.Height = korewindow->GetHeight();
    scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;      // how swap chain is to be used
    scd.OutputWindow = korewindow->GetHWND();                                // the window to be used
    scd.SampleDesc.Count = 1;                               // how many multisamples
    scd.SampleDesc.Quality = 0;                             // multisample quality level
    scd.Windowed = TRUE;                                    // windowed/full-screen mode

    IDXGIFactory1 *pFactory;
    hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1*), (void**)&pFactory);
    if (FAILED(hr))
        return false;

    IDXGIAdapter *pAdapter;
    hr = pFactory->EnumAdapters(0, &pAdapter);
    if (FAILED(hr))
        return false;

    DXGI_ADAPTER_DESC pAdapterDesc;
    hr = pAdapter->GetDesc(&pAdapterDesc);
    if (FAILED(hr))
        return false;

    SAFE_RELEASE(pFactory);

    // create a device, device context and swap chain using the information in the scd struct
    hr = D3D11CreateDeviceAndSwapChain(pAdapter,
        D3D_DRIVER_TYPE_UNKNOWN,
        NULL,
        D3D11_CREATE_DEVICE_DEBUG | D3D11_CREATE_DEVICE_BGRA_SUPPORT,
        NULL,
        NULL,
        D3D11_SDK_VERSION,
        &scd,
        &_swapchain,
        &_device,
        NULL,
        &_devicecontext);

    if (FAILED(hr)) {
        return false;
    }

    SAFE_RELEASE(pAdapter);

    // get the address of the back buffer
    ID3D11Texture2D *pBackBuffer;
    _swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);

    // use the back buffer address to create the render target
    hr = _device->CreateRenderTargetView(pBackBuffer, NULL, &_rendertargetview);
    if (FAILED(hr)) {
        return false;
    }
    SAFE_RELEASE(pBackBuffer);


    // set the render target as the back buffer
    _devicecontext->OMSetRenderTargets(1, &_rendertargetview, NULL);

    // Set the viewport
    D3D11_VIEWPORT viewport;
    ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

    viewport.TopLeftX = 0;
    viewport.TopLeftY = 0;
    viewport.Width = korewindow->GetWidth();
    viewport.Height = korewindow->GetHeight();

    _devicecontext->RSSetViewports(1, &viewport);

    // And lets create our D2D factory and DWrite factory at this point as well, that way if any of them fail we'll fail out completely.
    auto options = D2D1_FACTORY_OPTIONS();
    options.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;
    result = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, options, &D2D1Factory);
    if (FAILED(result))
    {
        Logger::Write("Unable to create D2D1Factory.", LOGTYPE::WARNING);
        return false;
    }

    result = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown * *>(&DwriteFactory));
    if (FAILED(result)) {
        Logger::Write("Unable to create Dwrite Factory.", LOGTYPE::WARNING);
        return false;
    }

    if (_swapchain == nullptr)
    {
        Logger::Write("D3D11 Swapchain is null.", LOGTYPE::WARNING);
        return false;
    }

    ComPtr<IDXGISurface1> d2dRT;
    result = _swapchain->GetBuffer(0, IID_PPV_ARGS(&d2dRT));
    if (FAILED(result)) {
        Logger::Write("Unable to obtain D2D Render Target.", LOGTYPE::WARNING);
        return false;
    }

    //-- failing
    DXGI_SWAP_CHAIN_DESC swapchainDesc;
    _swapchain->GetDesc(&swapchainDesc);

    auto d2dRTProps = D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT, D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED), 0, 0);
    result = D2D1Factory->CreateDxgiSurfaceRenderTarget(d2dRT.Get(), &d2dRTProps, &D2DRenderTarget);
    if (FAILED(result))
    {
        Logger::Write("Failed to create D2D Surface.", LOGTYPE::WARNING);
        return false;
    }

    result = D2DRenderTarget->CreateSolidColorBrush(D2D1::ColorF(1.0f, 0, 0, 1.0f), &SolidBrush);
    if (FAILED(result)) {
        Logger::Write("Unable to create solid brush for D2D.", LOGTYPE::WARNING);
        return false;
    }

    World = D2D1::Matrix3x2F::Identity();

    return true;
}

void KoreD2D::BeginRender() {
    float bg[4] = { 0,0,0,0 };
    _devicecontext->ClearRenderTargetView(_rendertargetview, bg);
    D2DRenderTarget->BeginDraw();
}
void KoreD2D::Draw() {

    //-- purposely left blank. Render whatever you want.
}
void KoreD2D::DrawImage(ID2D1Bitmap *bmp) {
    D2DRenderTarget->DrawBitmap(
        bmp,
        D2D1::RectF(0.0f, 0.0f, bmp->GetSize().width, bmp->GetSize().height),
        1.0f,
        D2D1_BITMAP_INTERPOLATION_MODE::D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR);
}

void KoreD2D::DrawRectangle(D2D1_RECT_F &rect, float thickness) {
    D2DRenderTarget->DrawRectangle(&rect, SolidBrush, thickness, NULL);
}

bool KoreD2D::LoadImageFile(LPCWSTR filename, ID2D1Bitmap *bmp) {
    
    HRESULT hr;

    // Create a WIC Factory
    IWICImagingFactory *wicFactory = NULL;
    hr = CoCreateInstance(
        CLSID_WICImagingFactory,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_IWICImagingFactory,
        (LPVOID*)&wicFactory);

    if (FAILED(hr))
        return false;

    // Create a decoder
    IWICBitmapDecoder *wicDecoder = NULL;
    hr = wicFactory->CreateDecoderFromFilename(
        filename,
        NULL,
        GENERIC_READ,
        WICDecodeMetadataCacheOnLoad,
        &wicDecoder);

    if (FAILED(hr))
        return false;

    // Read a frame from the image
    IWICBitmapFrameDecode* wicFrame = NULL;
    hr = wicDecoder->GetFrame(0, &wicFrame);

    if (FAILED(hr))
        return false;

    // Create a converter
    IWICFormatConverter *wicConverter = NULL;
    hr = wicFactory->CreateFormatConverter(&wicConverter);

    if (FAILED(hr))
        return false;


    // Setup the converter
    hr = wicConverter->Initialize(
        wicFrame,
        GUID_WICPixelFormat32bppPBGRA,
        WICBitmapDitherTypeNone,
        NULL,
        0.0,
        WICBitmapPaletteTypeMedianCut);

    if (FAILED(hr))
        return false;

    // Use the converter to create an D2D1Bitmap

    hr = D2DRenderTarget->CreateBitmapFromWicBitmap(
        wicConverter,
        NULL,
        &bmp);

    if (FAILED(hr))
        return false;

    if (wicFactory)
        wicFactory->Release();

    if (wicDecoder)
        wicDecoder->Release();

    if (wicConverter)
        wicConverter->Release();

    if (wicFrame)
        wicFrame->Release();

    return true;

}
void KoreD2D::EndRender() {
    D2DRenderTarget->EndDraw();
    _swapchain->Present(0, 0);

}
ID2D1Bitmap *KoreD2D::GetBitmap() {
    return _bmp;
}
void KoreD2D::Destroy() {
    SAFE_RELEASE(SolidBrush);
    SAFE_RELEASE(D2DRenderTarget);
    SAFE_RELEASE(DwriteFactory);
    SAFE_RELEASE(D2D1Factory);
    //-- needs to release D3D Created objects too.
    //-- Overlay Window Created.
    //-- This uses D3D and D2D interlope.
    //-- didn't put time aside to make sure these are released.
}

That's a lot of damn code right there. Don't worry about Logger. It's just a simple Logger program that'll report any issues or successful messages.

Honestly, I think I can do a lot of improvement on the code persay. It looks robust. Also, it can be adapted to use OpenGL. Just haven't gotten there yet. I do believe I can shrink down the code some. As you can see the CreateWindow function is customizable. D3D11 Creation is kinda rigid. But still good. Most likely, I'll change shit around.

The KoreD3D->Destroy() closes Fallout4.exe. Not entirely sure why. I did pass them to dummy variables. Also the KoreD3D may change. Oh, also, the variables like ID3D11Device *_dev. They should be in private. So another thing to do.

The good news is that the main PixieBot hookthread is looking tidier.
 
  • Love
  • Like
Reactions: XdarionX and Rake
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods