On Sat, 3 Apr 2021 10:25:29 +0200, R.Wieser wrote:
Hello all,
I'm currently playing around with giving a dialog different colors than the standard one, and have ran into a bit of a problem:
I want to mix using the results of CreateSolidBrush and GetSysColorBrush.
The thing is that while the latter handles do need to be deleted, the former ones must be (otherwise GDI resource leakage will occur).
While calling DeleteObject on a GetSysColorBrush handle doesn't do anything, accidentily prematurily deleting a CreateSolidBrush handle causes "bad things" to happen.
So:
tl;dr:
I would like to cache (lock?) my CreateSolidBrush handle just like the GetSysColorBrush handle is said to be. How do I do that ?
Regards,
Rudy Wieser
P.s.
I tried to take a peek into the CreateSolidBrush result handle (in an
attempt to compare the cached and non-cached brush in search for a "lock" bit), but alas, it seems to be stored somewhere where I am not allowed to even look at it ....
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 brush."
MSDN Library 2008:
"System color brushes are owned by the system and must not be destroyed."
MSDN website:
"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.
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)