@@ -102,19 +102,20 @@ CProxyDirect3D9::CProxyDirect3D9(IDirect3D9* pInterface)
102102 if (!IsValidComInterfacePointer (pInterface, ComPtrValidation::ValidationMode::ForceRefresh))
103103 {
104104 SString message;
105- message.Format (" CProxyDirect3D9 ctor: received invalid IDirect3D9 pointer %p" , pInterface);
105+ message.Format (" CProxyDirect3D9 ctor: received invalid IDirect3D9 pointer %p, proxy will be non-functional " , pInterface);
106106 AddReportLog (8753 , message, 5 );
107- m_pDevice = pInterface;
108- }
109- else
110- {
111- ReplaceInterface (m_pDevice, pInterface, " CProxyDirect3D9 ctor" );
112- pInterface->Release ();
107+ // Leave m_pDevice as nullptr - do not store invalid pointers
108+ return ;
113109 }
114110
111+ // Take ownership of the interface pointer
112+ // ReplaceInterface will AddRef(), giving us our own reference
113+ // Caller retains their reference
114+ ReplaceInterface (m_pDevice, pInterface, " CProxyDirect3D9 ctor" );
115+
115116 if (m_pDevice)
116117 {
117- if (IsValidComInterfacePointer (m_pDevice, ComPtrValidation::ValidationMode::ForceRefresh))
118+ if (IsValidComInterfacePointer (m_pDevice, ComPtrValidation::ValidationMode::ForceRefresh))
118119 {
119120 ms_CreatedDirect3D9List.push_back (m_pDevice);
120121 }
@@ -137,11 +138,8 @@ CProxyDirect3D9::~CProxyDirect3D9()
137138/* ** IUnknown methods ***/
138139HRESULT CProxyDirect3D9::QueryInterface (REFIID riid, void ** ppvObj)
139140{
140- if (!m_pDevice || ! IsValidComInterfacePointer (m_pDevice, ComPtrValidation::ValidationMode::ForceRefresh) )
141+ if (!m_pDevice)
141142 {
142- SString message;
143- message.Format (" CProxyDirect3D9::QueryInterface rejected invalid IDirect3D9 pointer %p" , m_pDevice);
144- AddReportLog (8752 , message, 5 );
145143 if (ppvObj)
146144 *ppvObj = nullptr ;
147145 return E_POINTER;
@@ -155,18 +153,7 @@ ULONG CProxyDirect3D9::AddRef()
155153 LONG lNewRefCount = InterlockedIncrement (&m_lRefCount);
156154
157155 if (m_pDevice)
158- {
159- if (IsValidComInterfacePointer (m_pDevice, ComPtrValidation::ValidationMode::ForceRefresh))
160- {
161- m_pDevice->AddRef ();
162- }
163- else
164- {
165- SString message;
166- message.Format (" CProxyDirect3D9::AddRef rejected invalid IDirect3D9 pointer %p" , m_pDevice);
167- AddReportLog (8752 , message, 5 );
168- }
169- }
156+ m_pDevice->AddRef ();
170157
171158 return static_cast <ULONG>(lNewRefCount);
172159}
@@ -184,18 +171,7 @@ ULONG CProxyDirect3D9::Release()
184171 }
185172
186173 if (m_pDevice && lNewRefCount > 0 )
187- {
188- if (IsValidComInterfacePointer (m_pDevice, ComPtrValidation::ValidationMode::ForceRefresh))
189- {
190- m_pDevice->Release ();
191- }
192- else
193- {
194- SString message;
195- message.Format (" CProxyDirect3D9::Release rejected invalid IDirect3D9 pointer %p" , m_pDevice);
196- AddReportLog (8752 , message, 5 );
197- }
198- }
174+ m_pDevice->Release ();
199175
200176 if (lNewRefCount == 0 )
201177 delete this ;
@@ -206,69 +182,95 @@ ULONG CProxyDirect3D9::Release()
206182/* ** IDirect3D9 methods ***/
207183HRESULT CProxyDirect3D9::RegisterSoftwareDevice (void * pInitializeFunction)
208184{
185+ if (!m_pDevice)
186+ return D3DERR_INVALIDDEVICE;
209187 return m_pDevice->RegisterSoftwareDevice (pInitializeFunction);
210188}
211189
212190UINT CProxyDirect3D9::GetAdapterCount ()
213191{
192+ if (!m_pDevice)
193+ return 0 ;
214194 return m_pDevice->GetAdapterCount ();
215195}
216196
217197HRESULT CProxyDirect3D9::GetAdapterIdentifier (UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER9* pIdentifier)
218198{
199+ if (!m_pDevice)
200+ return D3DERR_INVALIDDEVICE;
219201 return m_pDevice->GetAdapterIdentifier (Adapter, Flags, pIdentifier);
220202}
221203
222204UINT CProxyDirect3D9::GetAdapterModeCount (UINT Adapter, D3DFORMAT Format)
223205{
206+ if (!m_pDevice)
207+ return 0 ;
224208 return m_pDevice->GetAdapterModeCount (Adapter, Format);
225209}
226210
227211HRESULT CProxyDirect3D9::EnumAdapterModes (UINT Adapter, D3DFORMAT Format, UINT Mode, D3DDISPLAYMODE* pMode)
228212{
213+ if (!m_pDevice)
214+ return D3DERR_INVALIDDEVICE;
229215 return m_pDevice->EnumAdapterModes (Adapter, Format, Mode, pMode);
230216}
231217
232218HRESULT CProxyDirect3D9::GetAdapterDisplayMode (UINT Adapter, D3DDISPLAYMODE* pMode)
233219{
220+ if (!m_pDevice)
221+ return D3DERR_INVALIDDEVICE;
234222 return m_pDevice->GetAdapterDisplayMode (Adapter, pMode);
235223}
236224
237225HRESULT CProxyDirect3D9::CheckDeviceType (UINT Adapter, D3DDEVTYPE DevType, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat, BOOL bWindowed)
238226{
227+ if (!m_pDevice)
228+ return D3DERR_INVALIDDEVICE;
239229 return m_pDevice->CheckDeviceType (Adapter, DevType, AdapterFormat, BackBufferFormat, bWindowed);
240230}
241231
242232HRESULT CProxyDirect3D9::CheckDeviceFormat (UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat, DWORD Usage, D3DRESOURCETYPE RType,
243233 D3DFORMAT CheckFormat)
244234{
235+ if (!m_pDevice)
236+ return D3DERR_INVALIDDEVICE;
245237 return m_pDevice->CheckDeviceFormat (Adapter, DeviceType, AdapterFormat, Usage, RType, CheckFormat);
246238}
247239
248240HRESULT CProxyDirect3D9::CheckDeviceMultiSampleType (UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat, BOOL Windowed,
249241 D3DMULTISAMPLE_TYPE MultiSampleType, DWORD* pQualityLevels)
250242{
243+ if (!m_pDevice)
244+ return D3DERR_INVALIDDEVICE;
251245 return m_pDevice->CheckDeviceMultiSampleType (Adapter, DeviceType, SurfaceFormat, Windowed, MultiSampleType, pQualityLevels);
252246}
253247
254248HRESULT CProxyDirect3D9::CheckDepthStencilMatch (UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat, D3DFORMAT RenderTargetFormat,
255249 D3DFORMAT DepthStencilFormat)
256250{
251+ if (!m_pDevice)
252+ return D3DERR_INVALIDDEVICE;
257253 return m_pDevice->CheckDepthStencilMatch (Adapter, DeviceType, AdapterFormat, RenderTargetFormat, DepthStencilFormat);
258254}
259255
260256HRESULT CProxyDirect3D9::CheckDeviceFormatConversion (UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SourceFormat, D3DFORMAT TargetFormat)
261257{
258+ if (!m_pDevice)
259+ return D3DERR_INVALIDDEVICE;
262260 return m_pDevice->CheckDeviceFormatConversion (Adapter, DeviceType, SourceFormat, TargetFormat);
263261}
264262
265263HRESULT CProxyDirect3D9::GetDeviceCaps (UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS9* pCaps)
266264{
265+ if (!m_pDevice)
266+ return D3DERR_INVALIDDEVICE;
267267 return m_pDevice->GetDeviceCaps (Adapter, DeviceType, pCaps);
268268}
269269
270270HMONITOR CProxyDirect3D9::GetAdapterMonitor (UINT Adapter)
271271{
272+ if (!m_pDevice)
273+ return NULL ;
272274 return m_pDevice->GetAdapterMonitor (Adapter);
273275}
274276
@@ -336,7 +338,20 @@ HRESULT CProxyDirect3D9::CreateDevice(UINT Adapter, D3DDEVTYPE DeviceType, HWND
336338 #endif
337339
338340 // Set dark titlebar if needed
339- BOOL darkTitleBar = GetSystemRegistryValue ((uint)HKEY_CURRENT_USER, " Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Themes\\ Personalize" , " AppsUseLightTheme" ) == " \x0 " ;
341+ int themeStatus = 0 ;
342+ const SString appsUseLightTheme =
343+ GetSystemRegistryValue ((uint)HKEY_CURRENT_USER, " Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Themes\\ Personalize" , " AppsUseLightTheme" , &themeStatus);
344+ BOOL darkTitleBar = FALSE ;
345+ if (themeStatus > 0 )
346+ {
347+ // Parse the registry value into a numeric flag
348+ char * themeEnd = nullptr ;
349+ const long themeNumeric = strtol (appsUseLightTheme.c_str (), &themeEnd, 10 );
350+ if (themeEnd != appsUseLightTheme.c_str () && *themeEnd == ' \0 ' )
351+ {
352+ darkTitleBar = (themeNumeric == 0 );
353+ }
354+ }
340355 DwmSetWindowAttribute (hFocusWindow, DWMWA_USE_IMMERSIVE_DARK_MODE, &darkTitleBar, sizeof (darkTitleBar));
341356
342357 // Update icon
@@ -355,7 +370,14 @@ HRESULT CProxyDirect3D9::CreateDevice(UINT Adapter, D3DDEVTYPE DeviceType, HWND
355370 // Redraw, we avoid possible problems with the fact that it won't replace the icon somewhere
356371 InvalidateRect (hFocusWindow, nullptr , TRUE );
357372 UpdateWindow (hFocusWindow);
358-
373+
374+ // Check if proxy has valid device pointer
375+ if (!m_pDevice)
376+ {
377+ AddReportLog (8754 , SStringX (" CProxyDirect3D9::CreateDevice - proxy has no valid IDirect3D9 device" ), 5 );
378+ return D3DERR_INVALIDDEVICE;
379+ }
380+
359381 // Detect if second call to CreateDevice
360382 if (CreateDeviceSecondCallCheck (hResult, m_pDevice, Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface))
361383 {
@@ -857,13 +879,19 @@ void AddCapsReport(UINT Adapter, IDirect3D9* pDirect3D, IDirect3DDevice9* pD3DDe
857879 {
858880 // Log graphic card name
859881 D3DADAPTER_IDENTIFIER9 AdapterIdent1;
860- pDirect3D->GetAdapterIdentifier (Adapter, 0 , &AdapterIdent1);
882+ ZeroMemory (&AdapterIdent1, sizeof (AdapterIdent1));
883+ hr = pDirect3D->GetAdapterIdentifier (Adapter, 0 , &AdapterIdent1);
884+ if (FAILED (hr))
885+ WriteDebugEvent (SString (" Warning: pDirect3D->GetAdapterIdentifier failed: %08x" , hr));
861886 WriteDebugEvent (" pDirect3D:" );
862887 WriteDebugEvent (ToString (AdapterIdent1));
863888
864889 // Log graphic card name
865890 D3DADAPTER_IDENTIFIER9 AdapterIdent2;
866- pDirect3DOther->GetAdapterIdentifier (Adapter, 0 , &AdapterIdent2);
891+ ZeroMemory (&AdapterIdent2, sizeof (AdapterIdent2));
892+ hr = pDirect3DOther->GetAdapterIdentifier (Adapter, 0 , &AdapterIdent2);
893+ if (FAILED (hr))
894+ WriteDebugEvent (SString (" Warning: pDirect3DOther->GetAdapterIdentifier failed: %08x" , hr));
867895 WriteDebugEvent (" pDirect3DOther:" );
868896 WriteDebugEvent (ToString (AdapterIdent2));
869897
@@ -1020,7 +1048,10 @@ HRESULT HandleCreateDeviceResult(HRESULT hResult, IDirect3D9* pDirect3D, UINT Ad
10201048{
10211049 // Log graphic card name
10221050 D3DADAPTER_IDENTIFIER9 AdapterIdent;
1023- pDirect3D->GetAdapterIdentifier (Adapter, 0 , &AdapterIdent);
1051+ ZeroMemory (&AdapterIdent, sizeof (AdapterIdent));
1052+ HRESULT hr = pDirect3D->GetAdapterIdentifier (Adapter, 0 , &AdapterIdent);
1053+ if (FAILED (hr))
1054+ WriteDebugEvent (SString (" Warning: GetAdapterIdentifier failed: %08x" , hr));
10241055 WriteDebugEvent (ToString (AdapterIdent));
10251056
10261057 uint uiCurrentStatus = 0 ; // 0-unknown 1-fail 2-success after retry 3-success
@@ -1151,13 +1182,6 @@ HRESULT HandleCreateDeviceResult(HRESULT hResult, IDirect3D9* pDirect3D, UINT Ad
11511182 strMessage += SString (" Direct3D CreateDevice error: %08x" , hResult);
11521183 BrowseToSolution (" d3dcreatedevice-fail" , EXIT_GAME_FIRST | ASK_GO_ONLINE, strMessage);
11531184 }
1154- else
1155- {
1156- // Get current refresh rate
1157- D3DDISPLAYMODE DisplayMode;
1158- if (pDirect3D->GetAdapterDisplayMode (Adapter, &DisplayMode) == D3D_OK)
1159- CCore::GetSingleton ().SetCurrentRefreshRate (DisplayMode.RefreshRate );
1160- }
11611185
11621186 return hResult;
11631187}
@@ -1340,7 +1364,10 @@ HRESULT CCore::OnPostCreateDevice(HRESULT hResult, IDirect3D9* pDirect3D, UINT A
13401364
13411365 // Log graphic card name
13421366 D3DADAPTER_IDENTIFIER9 AdapterIdent;
1343- pDirect3D->GetAdapterIdentifier (Adapter, 0 , &AdapterIdent);
1367+ ZeroMemory (&AdapterIdent, sizeof (AdapterIdent));
1368+ HRESULT hr = pDirect3D->GetAdapterIdentifier (Adapter, 0 , &AdapterIdent);
1369+ if (FAILED (hr))
1370+ WriteDebugEvent (SString (" Warning: GetAdapterIdentifier failed: %08x" , hr));
13441371 WriteDebugEvent (ToString (AdapterIdent));
13451372
13461373 // Store the rendering window in the direct 3d data
@@ -1422,3 +1449,4 @@ HRESULT CCore::OnPostCreateDevice(HRESULT hResult, IDirect3D9* pDirect3D, UINT A
14221449
14231450 return hResult;
14241451}
1452+
0 commit comments