I have copied the existing code into a VS program framework for test.
The call of DwmSetWindowAttribute() with DWMWA_CAPTION_COLOR (and also the undocumented value 21) fails, which can be seen also with return value. According to the documentation, the DWMWA_CAPTION_COLOR flag is supported with DwmSetWindowAttribute() only from Windows 11 Build 22000.
The error code for Windows10 is E_INVALIDARG (One or more arguments are invalid.).
According to the documentation it is required that "client area rendering" is disabled, which it obviously is not by default.
See:
Controlling non - client region rendering
https://learn.microsoft.com/en-us/windows/win32/dwm/composition-ovw#controlling-non-client-region-rendering
DWMWINDOWATTRIBUTE enumeration
https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
hr = DisableNCRendering(hWnd);
BOOL isNCRenderingEnabled{ FALSE };
hr = DwmGetWindowAttribute(hWnd, DWMWA_NCRENDERING_ENABLED, &isNCRenderingEnabled, sizeof(isNCRenderingEnabled));
COLORREF mycolor = 0;
hr = DwmSetWindowAttribute(hWnd, DWMWA_CAPTION_COLOR, &mycolor, sizeof(mycolor));
There's an undocumented window attribute allowing dark mode title bars (DWMWA_USE_IMMERSIVE_DARK_MODE). Apparently this flag does not work under Win10 either.
We have also discussed this question many times and there are also CodeProject articles that deal with it. Unfortunately, Microsoft has changed a lot in the meantime, so that these articles either don't work anymore or can't be compiled.