Change the Screen Resolution
and Shut Down Windows

From Visual Basic you can change the screen resolution using the ChangeDisplaySettings API function and can also shut down and restart Windows via the ExitWindowsEx API function. On NT and 2000 shutting down Windows is a little more difficult because you have to first give your process shutdown privileges using the AdjustTokenPrivileges API.

Change Screen Resolution and Shut Down Windows.
Download Source Code

Changing the Screen Resolution

To change the screen resolution on the fly from Visual Basic is really pretty easy. Start by calling EnumDisplaySettings to retrieve information about the current graphics mode on the current display device into a DevMode structure. Then set the .dmFields value of the structure to DM_PELSWIDTH Or DM_PELSHEIGHT which indicates that the screen width or height values will be changing. Next, set the DevMode structure's dmPelsWidth and dmPelsHeight values to the desired values such as 1024 and 768. Use the ChangeDisplaySettings API function, passing it the DevMode structure with the new values, to apply the new resolution.

Then to insure all open windows resize accordingly, use the SendMessage function with HWND_BROADCAST as the window handle. HWND_BROADCAST will send our message to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows but not to child windows. For the message, use WM_DISAPLAYCHANGE with a wparam of SPI_SETNONCLIENTMETRICS. This informs the above windows to set the metrics associated with their non-client areas.

Shut Down and Restart Windows

On 9x machines shutting down Windows is as simple as calling the ExitWindowsEx function and passing it a flag telling Windows how to shut down. Windows can be instructed to shut down in a number of ways including shutting down all processes running and logging off the user (EWX_LOGOFF), shut down and restart the system (EWX_REBOOT), forcibly shut down the system without closing files resulting in possible data loss (EWX_FORCE) and shut down the system to a point where it is safe to turn off the system (EWX_SHUTDOWN).

On Windows NT and 2000 machines shutting down windows is more involved. You must get the access token of the current process with the privileges of querying the access token and adjusting its privileges. The GetCurrentProcess function returns the current process while the OpenProcessToken function and TOKEN_ADJUST_PRIVILEGES and TOKEN_QUERY flags do the rest.

Next you need to get the locally unique identifier (LUID) which represents the shutdown privilege. This is accomplished with the LookupPrivilegeValue function passing it the &quo;SeShutdownPrivilege" string. Once you have the locally unique identifier you can populate a Token_Privileges structure. You must allow your process to shut down the computer by setting the structure's Privileges.Attributes field to SE_PRIVILEGE_ENABLED. A call to AdjustTokenPrivileges passing it the newly populated Token_Privileges structure gives you application the right to shut down Windows.

Finally, all that remains is a call to ExitWindowsEx as described above.

Instructions

Download the source code. Press F5 to run the program.

To change the screen resolution, select a resolution and click the Change Resolution button.

To shut down Windows, select a shut down mode (see code) then click the Re-Start PC button.




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page