From 6d8a5fa0bbec06559af469cfab8c70815c9e442d Mon Sep 17 00:00:00 2001 From: Mohammad Amin Mollazadeh Date: Fri, 26 Jun 2026 21:14:49 +0330 Subject: [PATCH] [WIN32SS:NTGDI] Allow calling NtGdiCreateHalftonePalette with a NULL hDC (#9217) CORE-20231 Tests show this is allowed on Windows and even Wine. Also, official MS Windows SDK headers indicate that the `CreateHalftonePalette()` `hDC` parameter is optional, and so can be NULL. Not allowing a NULL `hDC` caused compatibility issues, breaking applications like Internet Explorer 8.0 and Microsoft Encarta. Make `NtGdiCreateHalftonePalette()` accept a NULL `hDC`, fixing its crash in this case, and only locking it when it's not NULL. Simplify also the function execution path. Co-authored-by: Simone Mario Lombardo --- win32ss/gdi/ntgdi/palette.c | 45 ++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/win32ss/gdi/ntgdi/palette.c b/win32ss/gdi/ntgdi/palette.c index 41a3d9639c9..1841c16e790 100644 --- a/win32ss/gdi/ntgdi/palette.c +++ b/win32ss/gdi/ntgdi/palette.c @@ -531,13 +531,7 @@ NtGdiCreateHalftonePalette(HDC hDC) PPALETTE ppal; PDC pdc; HPALETTE hpal = NULL; - - pdc = DC_LockDc(hDC); - if (!pdc) - { - EngSetLastError(ERROR_INVALID_HANDLE); - return NULL; - } + BOOL UseDefaultPalette = TRUE; RtlZeroMemory(PalEntries, sizeof(PalEntries)); @@ -553,13 +547,33 @@ NtGdiCreateHalftonePalette(HDC hDC) PalEntries[246 + i].peBlue = g_sysPalTemplate[10 + i].peBlue; } - ppal = PALETTE_ShareLockPalette(pdc->dclevel.hpal); - if (ppal && (ppal->flFlags & PAL_INDEXED)) + if (hDC) { - /* FIXME: optimize the palette for the current palette */ - UNIMPLEMENTED; + pdc = DC_LockDc(hDC); + if (!pdc) + { + EngSetLastError(ERROR_INVALID_HANDLE); + return NULL; + } + + ppal = PALETTE_ShareLockPalette(pdc->dclevel.hpal); + if (ppal) + { + if (ppal->flFlags & PAL_INDEXED) + { + UseDefaultPalette = FALSE; + + /* FIXME: optimize the palette for the current palette */ + UNIMPLEMENTED; + } + + PALETTE_ShareUnlockPalette(ppal); + } + + DC_UnlockDc(pdc); } - else + + if (UseDefaultPalette) { for (r = 0; r < 6; r++) { @@ -584,11 +598,6 @@ NtGdiCreateHalftonePalette(HDC hDC) } } - if (ppal) - PALETTE_ShareUnlockPalette(ppal); - - DC_UnlockDc(pdc); - ppal = PALETTE_AllocPalWithHandle(PAL_INDEXED, 256, PalEntries, 0, 0, 0); if (ppal) { @@ -778,7 +787,7 @@ IntGdiRealizePalette(HDC hDC) ASSERT(ppalDC->flFlags & PAL_INDEXED); - DPRINT1("RealizePalette unimplemented for %s\n", + DPRINT1("RealizePalette unimplemented for %s\n", (pdc->dctype == DCTYPE_MEMORY ? "memory managed DCs" : "device DCs")); cleanup: