Open vladimir-cheverdyuk-altium opened 3 weeks ago
Steps
Another test case which reproduces that crash.
D3DImage
on the internal monitor.It looks like IDirect3D9.GetAdapterMonitor will return 0 because monitor is powered off (or unavailable)
I can confirm the source is correct.
Your workaround didn’t work for me because I don't want to render the 3D content at refresh rate of the monitor, but I was able to workaround using MinHook to replace IDirect3D9.GetAdapterMonitor
API method for the current process, changing nullptr
return value to the handle of the primary monitor.
Implementing the fix on the WPF side would be way simpler. Literally 2 lines of code in CInteropDeviceBitmap::GetDisplayFromUserDevice
method, after the pID3DUserObject->GetAdapterMonitor
line:
if( nullptr == hMon )
hMon = ::MonitorFromWindow( nullptr, MONITOR_DEFAULTTOPRIMARY );
I know that are many reasons for this error but I found that for my case it was related to using D3DImage.
Here is link to the project that demonstrates problem : https://github.com/vladimir-cheverdyuk-altium/dx9host Basically it is Microsoft code copied from here: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/walkthrough-creating-direct3d9-content-for-hosting-in-wpf?view=netframeworkdesktop-4.8 https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/walkthrough-hosting-direct3d9-content-in-wpf?view=netframeworkdesktop-4.8
It has project for .NET Framework and .NET6.
Steps:
Source of the problem It looks like IDirect3D9.GetAdapterMonitor will return 0 because monitor is powered off (or unavailable) and function
pDisplaySet->GetDisplayIndexFromMonitor(hMon, uDisplayIndex)
inCInteropDeviceBitmap::GetDisplayFromUserDevice
will return E_INVALIDARG and this eventually will lead to crash.So to me it looks like issue is in D3DImage that connected to adapter 0 while adapter 0 is disconnected from monitor.
**Fix*** To fix it I declared
private EventHandler renderingEventHandler;
inMainWindow.xaml.cs
then set rendering like this:and add these 2 lines to the end of constructor:
and lastly I added implementation:
and lastly I added 3rd parameter
true
to every call toSetBackBuffer
and removedd3dimg.IsFrontBufferAvailable
check inCompositionTarget_Rendering
.Does anybody know if this approach to detect monitor changes (using Microsoft.Win32.SystemEvents.DisplaySettingsChanging) is correct?
Just in case if somebody thing it is rare thing, we have 37933 crash reports from users for this issue. I'm not sure how many related to this particular reason but at least some are. Here is one of reports from our customers: "I also get this issue on my Lenovo laptop when connecting/disconnecting external monitors." Some customers state that it happened when computer is going to sleep or perhaps when monitors going to sleep but I couldn't able to reproduce it.