There's no way to lock GDI handles. You'll just have to conditionally delete
the handles, or create a new brush with the same color as the system brush.
The statement that system color brush handles must not be deleted, only
applies to early Windows 95. Here's the last paragraph of the Remarks
section of the GetSysColorBrush in different SDK dicumentation versions.
Windows 95 SDK:
"An application must not register a window class for a window using a system
MSDN Library 2008:
"System color brushes are owned by the system and must not be destroyed."
"System color brushes are owned by the system so you don't need to destroy
them. Although you don't need to delete the logical brush that
GetSysColorBrush returns, no harm occurs by calling DeleteObject."
Notice the significant change of the statement in MSDN Library 2008. It no
longer state about window class registration. Meaning that Microsoft has
added some protection to allow the system color brush to be used with window
class registration. And in current MSDN website, it states that it's OK to
delete system color brushes. Meaning that more protection has been added.
I have no Windows 95 to test with, but in Windows 98, Windows 2000, and
newer versions, the system won't delete system color brush handles.
DeleteObject() will give a success report, but it won't produce any effect.
System brush handles and probably other system GDI handles also, are part of
the system. They're not listed in the process' GDI handles list within the
process TEB (check with Nirsoft GDIView). Processes will see the system GDI
handles as if they're predefined handles. FYI, the handle values are
identical across different process - both 32-bit and 64-bit.
When I debug DeleteObject(), the kernel function for deleting GDI objects
seems to be responsible for protecting the system GDI objects. But
DeleteObject() is the one which protects stock objects.