+#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
+ #undef _WIN32_WINNT
+ #define _WIN32_WINNT 0x0500
+#endif
+#include <windows.h>
+#include <stdio.h>
+#include <string.h>
+
+#if _MSC_VER
+#pragma comment(lib,"user32.lib")
+#endif
+
+/*
+
+ Windowsのロックと、ディスプレイの電源オフを行います。
+ ディスプレイの電源オフに関しては、http://www.knonline.net/d/?date=20080123 を参照しています。
+
+ usage:
+ lock_and_display_off
+ Windowsをロックし、ディスプレイの電源をオフにします
+ オプション:
+ -h
+ ヘルプを表示します
+ -lock
+ Windowsをロックします(デフォルト)
+ -nolock
+ Windowsをロックしません
+ -off
+ ディスプレイの電源をオフにします(デフォルト)
+ -nooff
+ ディスプレイの電源をオフにしません
+ -lockfirst
+ 最初にWindowsをロックし、次にディスプレイの電源をオフにします(デフォルト)
+ -offfirst
+ 最初にディスプレイの電源をオフにし、次にWindowsをロックします
+
+ 隠しオプション
+ -wait1st ミリ秒
+ 1個目の処理前に指定したミリ秒だけSleepします 有効範囲 0(デフォルト:Sleepしない)~WAIT_MSEC_MAX
+ これを指定しなくてもEXEC_WAITミリ秒(1秒)は強制Sleepしています。
+ -wait2nd ミリ秒
+ 2個目の処理前に指定したミリ秒だけSleepします 有効範囲 0(デフォルト:Sleepしない)~WAIT_MSEC_MAX
+ -waitDisplayOffDelay ミリ秒
+ ディスプレイの電源をオフにする際、省電力モード→オフの処理間に指定したミリ秒だけSleepします
+ 有効範囲 0(デフォルト:Sleepしない)~WAIT_MSEC_MAX
+
+*/
+
+#define WAIT_MSEC_MAX (60*1000)
+#define EXEC_WAIT 1000
+
+int cmdRet(const char* str)
+{
+ if(str) fprintf(stderr,"%s\n",str);
+ /* print usage */
+ fprintf(stderr,"usage: lock_and_display_off (-h) ([ -lock | -nolock ]) ([ -off | -nooff ])\n");
+ fprintf(stderr," ([ -lockfirst | -offfirst ])\n");
+ fprintf(stderr,"\toptions\n");
+ fprintf(stderr,"\t\t-h : show usage(this infomation printout)\n");
+ fprintf(stderr,"\t\t-lock : LockWindows(default)\n");
+ fprintf(stderr,"\t\t-nolock : no LockWindows\n");
+ fprintf(stderr,"\t\t-off : Display Power Off(default)\n");
+ fprintf(stderr,"\t\t-nooff : no Display Power Off\n");
+ fprintf(stderr,"\t\t-lockfirst : 1st LockWindows, next Display Power Off(default)\n");
+ fprintf(stderr,"\t\t-offfirst : 1st Display Power Off, next LockWindows\n");
+ fprintf(stderr,"\texample\n");
+ fprintf(stderr,"\t\thelp> lock_and_display_off -h\n");
+ fprintf(stderr,"\t\tLock and Display off> lock_and_display_off\n");
+ fprintf(stderr,"\t\tLock Only> lock_and_display_off -nooff\n");
+ fprintf(stderr,"\t\tDisplay off Only> lock_and_display_off -nolock\n");
+ fprintf(stderr,"\n");
+ fprintf(stderr,"\tversion 0.0.2011080412000\n");
+ fprintf(stderr,"\tLisence: ??? (c) copyright 2011 Ituki Kirihara/NI\n");
+ return 1;
+}
+
+int LockWindows(void)
+{
+ if (LockWorkStation() == 0){
+ return cmdRet("LockWindows:API:LockWorkStation Fail...");
+ }
+ return 0;
+}
+
+int DisplayOff(DWORD waitmsec)
+{
+ if (PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 1) == 0){
+ return cmdRet("DisplayOff:API:PostMessage 1st stage Fail...");
+ }
+ if (waitmsec > 0) Sleep(waitmsec);
+ if (PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2) == 0){
+ return cmdRet("DisplayOff:API:PostMessage 2nd stage Fail...");
+ }
+ return 0;
+}
+
+int main(int argc,char* argv[])
+{
+ int i;
+ int enableLock = 1;
+ int enableDisplayOff = 1;
+ int LockFirst = 1;
+ DWORD wait1st = 0;
+ DWORD wait2nd = 0;
+ DWORD waitDisplayOffDelay = 0;
+ char* nptr=NULL;
+ if (argc < 1 || argv == NULL) return cmdRet("unknown main function arg...");
+ for(i=1;i<argc;++i){
+ if(argv[i] == NULL) return cmdRet("unknown main function arg. argv is null...");
+ if(argv[i][0] != '-' || argv[i][1] == '\0') return cmdRet("unknown option");
+ if(strcmp(argv[i],"-h") == 0){
+ if (i != 1 || argc != 2){
+ return cmdRet("-h option : only this option can be specified.");
+ }else{
+ cmdRet(NULL);
+ return 0;
+ }
+ }else if(strcmp(argv[i],"-lock") == 0){
+ enableLock = 1;
+ }else if(strcmp(argv[i],"-nolock") == 0){
+ enableLock = 0;
+ }else if(strcmp(argv[i],"-off") == 0){
+ enableDisplayOff = 1;
+ }else if(strcmp(argv[i],"-nooff") == 0){
+ enableDisplayOff = 0;
+ }else if(strcmp(argv[i],"-lockfirst") == 0){
+ LockFirst = 1;
+ }else if(strcmp(argv[i],"-offfirst") == 0){
+ LockFirst = 0;
+ }else if(strcmp(argv[i],"-wait1st") == 0){
+ if (i+1<argc){
+ nptr = NULL;
+ wait1st = strtoul(argv[i+1],&nptr,0);
+ if (nptr == NULL || *nptr != '\0') return cmdRet("-wait1st option : msec is not number");
+ if (wait1st > WAIT_MSEC_MAX) return cmdRet("-wait1st option : msec time so long");
+ }else{
+ return cmdRet("-wait1st option : msec not set");
+ }
+ ++i;
+ }else if(strcmp(argv[i],"-wait2nd") == 0){
+ if (i+1<argc){
+ nptr = NULL;
+ wait2nd = strtoul(argv[i+1],&nptr,0);
+ if (nptr == NULL || *nptr != '\0') return cmdRet("-wait2nd option : msec is not number");
+ if (wait2nd > WAIT_MSEC_MAX) return cmdRet("-wait2nd option : msec time so long");
+ }else{
+ return cmdRet("-wait2nd option : msec not set");
+ }
+ ++i;
+ }else if(strcmp(argv[i],"-waitDisplayOffDelay") == 0){
+ if (i+1<argc){
+ nptr = NULL;
+ waitDisplayOffDelay = strtoul(argv[i+1],&nptr,0);
+ if (nptr == NULL || *nptr != '\0') return cmdRet("-waitDisplayOffDelay option : msec is not number");
+ if (waitDisplayOffDelay > WAIT_MSEC_MAX) return cmdRet("-waitDisplayOffDelay option : msec time so long");
+ }else{
+ return cmdRet("-waitDisplayOffDelay option : msec not set");
+ }
+ ++i;
+ }else{
+ return cmdRet("unknown option");
+ }
+ }
+ Sleep(EXEC_WAIT);
+ if (wait1st > 0) Sleep(wait1st);
+ if (LockFirst){
+ if (enableLock) LockWindows();
+ if (wait2nd > 0) Sleep(wait2nd);
+ if (enableDisplayOff) DisplayOff(waitDisplayOffDelay);
+ }else{
+ if (enableDisplayOff) DisplayOff(waitDisplayOffDelay);
+ if (wait2nd > 0) Sleep(wait2nd);
+ if (enableLock) LockWindows();
+ }
+ return 0;
+}
\ No newline at end of file