locked
How do I turn on/off AutoRotation by code? RRS feed

  • Question

  • I've found an API to retrieve status of AutoRotation(AR) which was GetAutoRotationState()

    And why there's no Set one? Even with Administrator Privilege still a way for developers. Now my hand's tied about that.

    Or may I reask my question.

    How to ENABLE AR after Locked in action center by code?


    Friday, August 16, 2019 10:25 AM

Answers

  • There is an api to Set AutoRotationState which is not documented. MSFT reserves the right to remove or change it at any time.

    C++:

    typedef BOOL(WINAPI* SETAUTOROTATION)(BOOL bEnable);
    SETAUTOROTATION SetAutoRotationState = (SETAUTOROTATION)GetProcAddress(LoadLibrary("user32.dll"), (LPCSTR)2507);
    if (SetAutoRotationState != NULL)
    {
    	SetAutoRotationState(TRUE);
    }

    C#:

    [DllImport("user32.dll", EntryPoint = "#2507")]
    extern static bool SetAutoRotationState(bool bEnable);
    
    SetAutoRotationState(true);

    Another alternative, use the registry API to modify the following registry key:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AutoRotation\Enable

    Best regards,

    Drake


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wow! This is very cool! Thanks a lot!
    • Marked as answer by Herlin.Drew Sunday, August 25, 2019 6:31 PM
    Sunday, August 25, 2019 6:30 PM

All replies

  • Friday, August 16, 2019 10:31 AM
  • There are some settings that are read only for a reason.

    Another way to interpret what you asked is "How do I override a user's preference? I, as a developer, know better."

    If I go to my display settings and enable or disable auto rotate, why do you think that I would be happy for your application to change those settings?


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Friday, August 16, 2019 3:49 PM
  • There is an api to Set AutoRotationState which is not documented. MSFT reserves the right to remove or change it at any time.

    C++:

    typedef BOOL(WINAPI* SETAUTOROTATION)(BOOL bEnable);
    SETAUTOROTATION SetAutoRotationState = (SETAUTOROTATION)GetProcAddress(LoadLibrary("user32.dll"), (LPCSTR)2507);
    if (SetAutoRotationState != NULL)
    {
    	SetAutoRotationState(TRUE);
    }

    C#:

    [DllImport("user32.dll", EntryPoint = "#2507")]
    extern static bool SetAutoRotationState(bool bEnable);
    
    SetAutoRotationState(true);

    Another alternative, use the registry API to modify the following registry key:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AutoRotation\Enable

    Best regards,

    Drake


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, August 19, 2019 7:25 AM
  • Hello,

    maybe this can help: https://stackoverflow.com/questions/12955335/set-screen-autorotation-system-setting-from-code

    Regards, Guido

    It's for Android, thanks for helping though.

    • Edited by Herlin.Drew Thursday, August 22, 2019 6:09 AM
    Wednesday, August 21, 2019 10:08 AM
  • Did the SetAutoRotationState method work for you?

    Best regards,

    Drake


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 22, 2019 5:28 AM
  • There is an api to Set AutoRotationState which is not documented. MSFT reserves the right to remove or change it at any time.

    C++:

    typedef BOOL(WINAPI* SETAUTOROTATION)(BOOL bEnable);
    SETAUTOROTATION SetAutoRotationState = (SETAUTOROTATION)GetProcAddress(LoadLibrary("user32.dll"), (LPCSTR)2507);
    if (SetAutoRotationState != NULL)
    {
    	SetAutoRotationState(TRUE);
    }

    C#:

    [DllImport("user32.dll", EntryPoint = "#2507")]
    extern static bool SetAutoRotationState(bool bEnable);
    
    SetAutoRotationState(true);

    Another alternative, use the registry API to modify the following registry key:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AutoRotation\Enable

    Best regards,

    Drake


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wow! This is very cool! Thanks a lot!
    • Marked as answer by Herlin.Drew Sunday, August 25, 2019 6:31 PM
    Sunday, August 25, 2019 6:30 PM
  • There is an api to Set AutoRotationState which is not documented. MSFT reserves the right to remove or change it at any time.

    C++:

    typedef BOOL(WINAPI* SETAUTOROTATION)(BOOL bEnable);
    SETAUTOROTATION SetAutoRotationState = (SETAUTOROTATION)GetProcAddress(LoadLibrary("user32.dll"), (LPCSTR)2507);
    if (SetAutoRotationState != NULL)
    {
    	SetAutoRotationState(TRUE);
    }

    C#:

    [DllImport("user32.dll", EntryPoint = "#2507")]
    extern static bool SetAutoRotationState(bool bEnable);
    
    SetAutoRotationState(true);

    Another alternative, use the registry API to modify the following registry key:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AutoRotation\Enable

    Best regards,

    Drake


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    And another question bothering. The registry you've montioned I already discovered before, but it turns out I could lock and unlock auto-rotation when the System switcher is OFF. If user turn it on in Action Centre, I cannot disable the lock by modifying that registry. Is that right? Or did I do something incorrect?
    Sunday, August 25, 2019 6:33 PM