Question

I am trying to hook Directx11 IDXGISwapChain.Present for getting screenshots using the Direct3DHook library and EasyHook. I guess the Direct3DHook is written for Single screenshots and I am trying to extend it to get a stream of screenshots. After the first few screenshots, the host application gets an exception at a random rendering stack.

I can isolate the error to the following code in the file DXHookD3D11.cs:

using (MemoryStream ms = new MemoryStream())
{
    // This line crashes the main program, removing this makes the program not crash               
    res = Texture2D.ToStream(textureDest.Device.ImmediateContext, textureDest, ImageFileFormat.Bmp, ms).Code;
    if (res != 0)
    {
        // this debugmessage never gets printed
        this.DebugMessage("PresentHook: ERror in Tostream " + res.ToString());
    }
    .......                                    
}
Was it helpful?

Solution

I think I found the error. Direct3DHook uses a threadpool to run the code above.

ThreadPool.QueueUserWorkItem(delegate


                            {

                                
                               using (MemoryStream ms = new MemoryStream())


                                {


                                    Texture2D.ToStream(textureDest.Device.ImmediateContext, textureDest, ImageFileFormat.Bmp, ms);

                                    SendResponse(ms, requestId);


                                    this.DebugMessage("PresentHook: Send response time: " + (DateTime.Now - startSendResponse).ToString());


                                 }


                                 // Free the textureDest as we no longer need it.


                                textureDest.Dispose();


                                 textureDest = null;


                             });

But the Texture.ToStream call needs to be single threaded because internally it probably issues the GetRenderTarget call which is single threaded and will cause an exception if two calls are started asynchronously. I removed the ThreadPool call and it worked okay.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top