justinstenning / Direct3DHook

DirectX Capture and Overlays by using Direct3D API hooks
http://spazzarama.com/2011/03/14/c-screen-capture-and-overlays-for-direct3d-9-10-and-11-using-api-hooks
MIT License
580 stars 178 forks source link

Exception is thrown when trying to capture screenshot on dx11 game how can i fix it ? #16

Closed Csharper1972 closed 9 years ago

Csharper1972 commented 9 years ago

I'm running battlefield bad company 2 and it's working on dx11. When i'm clicking on the Request Capture button after some seconds it's throwing an exception in Form1.cs in the method DoRequest() the exception message is:

Could not set process window to the foreground

It didn't happen on DX9 game only on this battlefield bad company 2.

This is the full exception error message:

System.Exception was unhandled HResult=-2146233088 Message=Could not set process window to the foreground Source=System.Windows.Forms StackTrace: at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous) at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args) at TestScreenshot.Form1.DoRequest() in c:\Temp\spazz\Direct3DHook-master\TestScreenshot\Form1.cs:line 218 at TestScreenshot.Form1.btnCapture_Click(Object sender, EventArgs e) in c:\Temp\spazz\Direct3DHook-master\TestScreenshot\Form1.cs:line 196 at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at TestScreenshot.Program.Main() in c:\Temp\spazz\Direct3DHook-master\TestScreenshot\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

Line 196 in form1 is:

DoRequest();

And line 218 in form1 is: progressBar1.Invoke(new MethodInvoker(delegate() And the whole code in the DoRequest() method is marked with yellow when the exception is show up.

And so far it happen only on dx11 game battlefield bad company 2 and only when i click the button Request Capture

justinstenning commented 9 years ago

You can comment out the bringprocesstofront as that was only required for some old approaches.

On Friday, 12 September 2014, Csharper1972 notifications@github.com wrote:

I'm running battlefield bad company 2 and it's working on dx11. When i'm clicking on the Request Capture button after some seconds it's throwing an exception in Form1.cs in the method DoRequest() the exception message is:

Could not set process window to the foreground

It didn't happen on DX9 game only on this battlefield bad company 2.

This is the full exception error message:

System.Exception was unhandled HResult=-2146233088 Message=Could not set process window to the foreground Source=System.Windows.Forms StackTrace: at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous) at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args) at TestScreenshot.Form1.DoRequest() in c:\Temp\spazz\Direct3DHook-master\TestScreenshot\Form1.cs:line 218 at TestScreenshot.Form1.btnCapture_Click(Object sender, EventArgs e) in c:\Temp\spazz\Direct3DHook-master\TestScreenshot\Form1.cs:line 196 at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at TestScreenshot.Program.Main() in c:\Temp\spazz\Direct3DHook-master\TestScreenshot\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

Line 196 in form1 is:

DoRequest();

And line 218 in form1 is: progressBar1.Invoke(new MethodInvoker(delegate() And the whole code in the DoRequest() method is marked with yellow when the exception is show up.

And so far it happen only on dx11 game battlefield bad company 2 and only when i click the button Request Capture

— Reply to this email directly or view it on GitHub https://github.com/spazzarama/Direct3DHook/issues/16.

Csharper1972 commented 9 years ago

Thanks another question i'm not sure if it's issue or not. How can i make in form1 with a button click that it will show or not show the overlays ? One click will not show the fps another click will show. I know how can i call the Draw() method in DXOverlayEngine.cs from form1 or i can use the Inject button click Inject/Detach but i want just to display or not to display the overlays.

justinstenning commented 9 years ago

Look at the CaptureInterface and CaptureInterfaceProxy classes. You can extend to make any calls into the target application. Just have to create appropriate event handlers.

On Saturday, 13 September 2014, Csharper1972 notifications@github.com wrote:

Thanks another question i'm not sure if it's issue or not. How can i make in form1 with a button click that it will show or not show the overlays ? One click will not show the fps another click will show. I know how can i call the Draw() method in DXOverlayEngine.cs from form1 or i can use the Inject button click Inject/Detach but i want just to display or not to display the overlays.

— Reply to this email directly or view it on GitHub https://github.com/spazzarama/Direct3DHook/issues/16#issuecomment-55479042 .

Csharper1972 commented 9 years ago

Thank you. Working.