Coder Social home page Coder Social logo

eugen15 / directx-present-hook Goto Github PK

View Code? Open in Web Editor NEW
28.0 2.0 9.0 59 KB

The project shows how to hook IDXGISwapChain::Present and capture window frames.

License: MIT License

CMake 1.21% Batchfile 0.27% C++ 28.34% C 69.93% HLSL 0.25%
directx directx12 directx-12 directx-11 directx11 hook dxgi

directx-present-hook's Introduction

DirectX Present Hook

The project is an answer to this Stack Overflow question https://stackoverflow.com/questions/40538590/getting-dxgi-swapchain-by-hwnd. It shows how to hook IDXGISwapChain::Present (when it is used with DirectX 11/DirectX 12) to capture frames of a window which you do not control. For example, it can be used for some ActiveX controls which you integrate to your application. Probably, the project can be extended for more complicated tasks.

Source code

See the following classes:

  • D3D11PresentHook: d3d11-present-hook.h, d3d11-present-hook.cpp.
  • D3D12PresentHook: d3d12-present-hook.h, d3d12-present-hook.cpp.

The classes above are well commented. So, I hope that even if they do not solve your task directly, they may give you some ideas at least. The other classes are auxiliary or used to test the hooks by creating a "black box" window with a moving square.

Building

Visual Studio

Visual Studio 2019 v16.10 & v16.11 or later is required. Personally, I use Visual Studio 2022. The download link is here https://visualstudio.microsoft.com/vs/community/.

CMake

CMake is available as a part of Visual Studio 2022 installation. In my case it is located in the following folder: C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/. Install CMake separately https://cmake.org/ if your Visual Studio does not include it.

PolyHook 2

You need to compile this https://github.com/stevemk14ebr/PolyHook_2_0

DirectX Present Hook

The root folder contains example-prepare-x32.cmd and example-prepare-x64.cmd scripts.

  • Change the include and library paths inside them to point to PolyHook 2.
  • Change the CMAKE path if you do not use Visual Studio 2022.
  • Depending on PolyHook 2 compilation parameters, you may have to change the target_link_libraries line in CMakeLists.txt.
  • Run one of the scripts to prepare the Visual Studio solution.
  • Go to the build folder, open the solution. compile it.
  • You might need asmjit.dll, capstone.dll, PolyHook_2.dll and Zydis.dll in the output folder. If you compiled PolyHook 2 via vcpkg, they will be there automatically.

Testing

  • directx-present-hook.exe will create a DirectX 11 window with a moving square, set the hook and save first ten frames into BMP files in the same output folder.
  • directx-present-hook.exe 12 will create a DirectX 12 window with a moving square, set the hook and save first ten frames into BMP files in the same output folder.
  • directx-present-hook.exe 11 C:\Temp will create a DirectX 11 window with a moving square, set the hook and save first ten frames into BMP files in C:\Temp.

directx-present-hook's People

Contributors

eugen15 avatar rjx-ray avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

directx-present-hook's Issues

Hooking IDXGISwapChain::Present in another process

Hi,
The example works well, it is very useful for me! Now I'm considering to capture window of other processes, and made some changes to test:

 // Set the hook object to capture some frames.
 hr = HookT::Get()->CaptureFrames(blackBoxDXWindow.GetHandle(), outputFolder, 10);

to

  // Set the hook object to capture some frames, but for another window.
  HWND hwnd = FindWindowW(NULL, L"Another Window");
  hr = HookT::Get()->CaptureFrames(hwnd, outputFolder, 10);

I'm using dx11 and I found the D3D11PresentHook::SwapChainPresent function still only catching the swapchain of the blackbox window created by its own.
I have noticed that the address of presentPointer_ in D3D11PresentHook::Hook is always the same, so I supposed the polyhook will work like some global hook of SetWindowsHook, but it's seems not.
And now I think the problem is cross-process, do you have any idea of this issue or I missed up something?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.