1 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
3 #define _WIN32_WINNT 0x0500
10 #pragma comment(lib,"user32.lib")
15 Windowsのロックと、ディスプレイの電源オフを行います。
16 ディスプレイの電源オフに関しては、http://www.knonline.net/d/?date=20080123 を参照しています。
20 Windowsをロックし、ディスプレイの電源をオフにします
29 ディスプレイの電源をオフにします(デフォルト)
33 最初にWindowsをロックし、次にディスプレイの電源をオフにします(デフォルト)
35 最初にディスプレイの電源をオフにし、次にWindowsをロックします
39 1個目の処理前に指定したミリ秒だけSleepします 有効範囲 0(デフォルト:Sleepしない)~WAIT_MSEC_MAX
40 これを指定しなくてもEXEC_WAITミリ秒(1秒)は強制Sleepしています。
42 2個目の処理前に指定したミリ秒だけSleepします 有効範囲 0(デフォルト:Sleepしない)~WAIT_MSEC_MAX
43 -waitDisplayOffDelay ミリ秒
44 ディスプレイの電源をオフにする際、省電力モード→オフの処理間に指定したミリ秒だけSleepします
45 有効範囲 0(デフォルト:Sleepしない)~WAIT_MSEC_MAX
49 #define WAIT_MSEC_MAX (60*1000)
50 #define EXEC_WAIT 1000
52 int cmdRet(const char* str)
54 if(str) fprintf(stderr,"%s\n",str);
56 fprintf(stderr,"usage: lock_and_display_off (-h) ([ -lock | -nolock ]) ([ -off | -nooff ])\n");
57 fprintf(stderr," ([ -lockfirst | -offfirst ])\n");
58 fprintf(stderr,"\toptions\n");
59 fprintf(stderr,"\t\t-h : show usage(this infomation printout)\n");
60 fprintf(stderr,"\t\t-lock : LockWindows(default)\n");
61 fprintf(stderr,"\t\t-nolock : no LockWindows\n");
62 fprintf(stderr,"\t\t-off : Display Power Off(default)\n");
63 fprintf(stderr,"\t\t-nooff : no Display Power Off\n");
64 fprintf(stderr,"\t\t-lockfirst : 1st LockWindows, next Display Power Off(default)\n");
65 fprintf(stderr,"\t\t-offfirst : 1st Display Power Off, next LockWindows\n");
66 fprintf(stderr,"\texample\n");
67 fprintf(stderr,"\t\thelp> lock_and_display_off -h\n");
68 fprintf(stderr,"\t\tLock and Display off> lock_and_display_off\n");
69 fprintf(stderr,"\t\tLock Only> lock_and_display_off -nooff\n");
70 fprintf(stderr,"\t\tDisplay off Only> lock_and_display_off -nolock\n");
72 fprintf(stderr,"\tversion 0.0.2011080412000\n");
73 fprintf(stderr,"\tLisence: ??? (c) copyright 2011 Ituki Kirihara/NI\n");
79 if (LockWorkStation() == 0){
80 return cmdRet("LockWindows:API:LockWorkStation Fail...");
85 int DisplayOff(DWORD waitmsec)
87 if (PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 1) == 0){
88 return cmdRet("DisplayOff:API:PostMessage 1st stage Fail...");
90 if (waitmsec > 0) Sleep(waitmsec);
91 if (PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2) == 0){
92 return cmdRet("DisplayOff:API:PostMessage 2nd stage Fail...");
97 int main(int argc,char* argv[])
101 int enableDisplayOff = 1;
105 DWORD waitDisplayOffDelay = 0;
107 if (argc < 1 || argv == NULL) return cmdRet("unknown main function arg...");
109 if(argv[i] == NULL) return cmdRet("unknown main function arg. argv is null...");
110 if(argv[i][0] != '-' || argv[i][1] == '\0') return cmdRet("unknown option");
111 if(strcmp(argv[i],"-h") == 0){
112 if (i != 1 || argc != 2){
113 return cmdRet("-h option : only this option can be specified.");
118 }else if(strcmp(argv[i],"-lock") == 0){
120 }else if(strcmp(argv[i],"-nolock") == 0){
122 }else if(strcmp(argv[i],"-off") == 0){
123 enableDisplayOff = 1;
124 }else if(strcmp(argv[i],"-nooff") == 0){
125 enableDisplayOff = 0;
126 }else if(strcmp(argv[i],"-lockfirst") == 0){
128 }else if(strcmp(argv[i],"-offfirst") == 0){
130 }else if(strcmp(argv[i],"-wait1st") == 0){
133 wait1st = strtoul(argv[i+1],&nptr,0);
134 if (nptr == NULL || *nptr != '\0') return cmdRet("-wait1st option : msec is not number");
135 if (wait1st > WAIT_MSEC_MAX) return cmdRet("-wait1st option : msec time so long");
137 return cmdRet("-wait1st option : msec not set");
140 }else if(strcmp(argv[i],"-wait2nd") == 0){
143 wait2nd = strtoul(argv[i+1],&nptr,0);
144 if (nptr == NULL || *nptr != '\0') return cmdRet("-wait2nd option : msec is not number");
145 if (wait2nd > WAIT_MSEC_MAX) return cmdRet("-wait2nd option : msec time so long");
147 return cmdRet("-wait2nd option : msec not set");
150 }else if(strcmp(argv[i],"-waitDisplayOffDelay") == 0){
153 waitDisplayOffDelay = strtoul(argv[i+1],&nptr,0);
154 if (nptr == NULL || *nptr != '\0') return cmdRet("-waitDisplayOffDelay option : msec is not number");
155 if (waitDisplayOffDelay > WAIT_MSEC_MAX) return cmdRet("-waitDisplayOffDelay option : msec time so long");
157 return cmdRet("-waitDisplayOffDelay option : msec not set");
161 return cmdRet("unknown option");
165 if (wait1st > 0) Sleep(wait1st);
167 if (enableLock) LockWindows();
168 if (wait2nd > 0) Sleep(wait2nd);
169 if (enableDisplayOff) DisplayOff(waitDisplayOffDelay);
171 if (enableDisplayOff) DisplayOff(waitDisplayOffDelay);
172 if (wait2nd > 0) Sleep(wait2nd);
173 if (enableLock) LockWindows();