1

我正在尝试使用Direct3DHook库和EasyHook来挂钩 Directx11 IDXGISwapChain.Present以获取屏幕截图。我猜 Direct3DHook 是为单个屏幕截图编写的,我正在尝试扩展它以获取屏幕截图流。在最初的几个屏幕截图之后,主机应用程序在随机渲染堆栈处获得异常。

我可以将错误隔离到文件 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());
    }
    .......                                    
}
4

1 回答 1

0

我想我发现了错误。Direct3DHook 使用线程池来运行上面的代码。

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;


                             });

但是 Texture.ToStream 调用需要是单线程的,因为它在内部可能会发出单线程的 GetRenderTarget 调用,如果两个调用异步启动,则会导致异常。我删除了 ThreadPool 调用,它工作正常。

于 2013-01-14T23:34:25.960 回答