Microsoft KB Archive/249677: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - "<" to "<")
Line 134: Line 134:


     return lRetVal;
     return lRetVal;
}&lt;BR/&gt;
}<BR/&gt;
                 </pre>
                 </pre>



Revision as of 08:00, 21 July 2020

Knowledge Base


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
    • Microsoft Windows NT 4.0 Service Pack 4
    • Microsoft Windows NT 4.0 Service Pack 5
    • Microsoft Windows NT 4.0 Service Pack 6
    • Microsoft Windows NT 4.0 Service Pack 6a
    • Microsoft Windows 2000 Service Pack 1
    • Microsoft Windows 2000 Service Pack 2
    • Microsoft Windows 2000 Service Pack 3
    • Microsoft Windows XP Embedded
  • Microsoft Windows XP Professional for Itanium-based systems



This article was previously published under Q249677

SYMPTOMS

The ChangeDisplaySettingsEx() function may fail when called from a service.

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.

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/>
                

STATUS

This behavior is by design.

Keywords: kbdswgdi2003swept kbdisplay kbgdi kbprb kbservice KB249677