2009. április 28., kedd

Disabling the system keys from your application


Problem/Question/Abstract:

When my application is running, I'd like to prevent users from using Ctrl-Alt-Del and Alt- Tab. What's the best way to do this?

Answer:

This is pretty quick one... The best way I've seen yet is to trick Windows into thinking that a screen saver is running. When Windows thinks a screensaver is active, Ctrl-Alt-Del and Alt-Tab (Win95 only for this) are disabled. You can perform this trickery by calling a WinAPI function, SystemParametersInfo. For a more in-depth discussion about what this function does, I encourage you to refer to the online help.

In any case, SystemParametersInfo takes four parameters. Here's its C declaration from the Windows help file:

BOOL SystemParametersInfo(
  UINT uiAction, // system parameter to query or set
  UINT uiParam, // depends on action to be taken
  PVOID pvParam, // depends on action to be taken
  UINT fWinIni // user profile update flag
  );

For our purposes we'll set uiAction to SPI_SCREENSAVERRUNNING, uiParam to 1 or 0 (1 to disable the keys, 0 to re-enable them), pvParam to a "dummy" pointer address, then fWinIni to 0. Pretty straight-forward. Here's what you do:

To disable the keystrokes, write this:

SystemParametersInfo(SPI_SCREENSAVERRUNNING, 1, @ptr, 0);

To enable the keystrokes, write this:

SystemParametersInfo(SPI_SCREENSAVERRUNNING, 0, @ptr, 0);

Not much to it, is there? Thanks to the folks on the Borland Forums for providing this information!

Nincsenek megjegyzések:

Megjegyzés küldése