Microsoft KB Archive/249677

= PRB: ChangeDisplaySettingsEx Fails When Called From a Service =

Article ID: 249677

Article Last Modified on 2/12/2007

-

APPLIES TO

 Microsoft Platform Software Development Kit-January 2000 Edition, when used with:  Microsoft Windows NT 4.0

 Microsoft Windows NT 4.0 Service Pack 1

 Microsoft Windows NT 4.0 Service Pack 2

 Microsoft Windows NT 4.0 Service Pack 3</li></ul>

 Microsoft Windows NT 4.0 Service Pack 4</li></ul>

 Microsoft Windows NT 4.0 Service Pack 5</li></ul>

 Microsoft Windows NT 4.0 Service Pack 6</li></ul>

 Microsoft Windows NT 4.0 Service Pack 6a</li></ul>

 Microsoft Windows 2000 Service Pack 1</li></ul>

 Microsoft Windows 2000 Service Pack 2</li></ul>

 Microsoft Windows 2000 Service Pack 3</li></ul>

 Microsoft Windows XP Embedded</li></ul> </li> <li>Microsoft Windows XP Professional for Itanium-based systems</li></ul>

-

<div class="notice_section">

This article was previously published under Q249677

<div class="symptoms_section">

SYMPTOMS
The ChangeDisplaySettingsEx function may fail when called from a service.

<div class="cause_section">

CAUSE
If ChangeDisplaySettingsEx is being called from a thread that is not currently attached to the active Desktop, the thread cannot make changes to the display driver used by that Desktop.

<div class="resolution_section">

RESOLUTION
To resolve this problem, attach the currently active Desktop to the thread that is running in the service.

The code demonstrates how to do the following:
 * 1) Switch to the active Desktop.
 * 2) Change the display mode.
 * 3) Switch back to the original desktop.

The following code demonstrates how to resolve this problem. LONG ChangeDesktopDisplaySettings(LPDEVMODE lpdm) {   HDESK   hdeskInput; HDESK  hdeskCurrent; long   lRetVal;

// Save the current desktop hdeskCurrent = GetThreadDesktop(GetCurrentThreadId); if (hdeskCurrent == NULL) return (-1); // Determine the current input desktop hdeskInput = OpenInputDesktop(0, FALSE, MAXIMUM_ALLOWED); if (hdeskInput == NULL) return (-1);

// Set thread to the input desktop. if (!SetThreadDesktop(hdeskInput)) return (-1);

// Change the display settings. lRetVal = ChangeDisplaySettingsEx (NULL, lpdm, NULL, CDS_GLOBAL|CDS_UPDATEREGISTRY|CDS_NORESET,NULL);

// Reset thread to the original desktop. SetThreadDesktop(hdeskCurrent);

// Close handle to the input desktop. CloseDesktop(hdeskInput);

return lRetVal; }<BR/>

<div class="status_section">

STATUS
This behavior is by design.

Keywords: kbdswgdi2003swept kbdisplay kbgdi kbprb kbservice KB249677

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.