/*
Name: APM
Author: cLx
Description: To switch monitors in green mode
OSs: Windows: 9x/ME/2k/XP
Date: 06/08/04 20:02
Thanks to: Hiro
Uses: Green mode: > APM
Lock PC before: > APM --lock
Help: > APM --help
Comments: C'est un bout de code que j'utilise pour faire passer en veille les moniteurs
de mes PC rapidement avec un combinaison de touches. On peut le locker en même
temps si le systeme le permet, sans poser devoir le recompiler grace a un
chargement dynamique de la DLL. Depuis le temps que je l'utilise, j'avais oublié
que j'avais fait un petit message d'aide ;)
*/
#define TITLE "APM -- http://clx.freeshell.org/"
#include <windows.h>
#include <stdio.h>
#include <string.h>
int LockWorkStation(void) {
typedef BOOL (*LPFNLWS)(void);
LPFNLWS lpfnLockWS = NULL;
HINSTANCE hUser32;
unsigned long ret = 0;
hUser32 = LoadLibrary("User32.dll");
if (hUser32) {
lpfnLockWS = (LPFNLWS)GetProcAddress(hUser32, "LockWorkStation");
if (lpfnLockWS) {
(*lpfnLockWS)();
ret = GetLastError();
}
FreeLibrary(hUser32);
return ret;
}
else { return 0; }
}
int main(unsigned int argc, char **argv) {
int ret;
if (argc != 1) {
if (!stricmp(argv[1], "--lock")) {
if (!LockWorkStation()) {
ret = MessageBox (NULL, "LockWorkStation() failled.
Common reasons the workstation might not be locked even if the function succeeds include the following:
no user is logged on, the workstation is already locked, the process is not running on the interactive
desktop, or the request is denied by the Graphical Identification and Authentication (GINA) DLL.
Continue anyway ?" , TITLE, 0 + MB_OKCANCEL + MB_ICONHAND);
if (ret != MB_OK) { return -1; }
}
Sleep(1500);
}
else {
MessageBox (NULL, "Switch the monitor in the green mode using an win32's API.
You can use the option --lock on NT based windows." , TITLE, 0);
return 0;
}
}
Sleep(500);
PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
return 0;
}