1+ #include <windows.h>
2+ #include <stdio.h>
3+ #include <string.h>
4+ #include <mmsystem.h>
5+ #include <powrprof.h>
6+
7+ #pragma comment(lib, "winmm.lib")
8+ #pragma comment(lib, "PowrProf.lib") // For power management
9+
10+ // Screen lock
11+ void lock_screen () {
12+ LockWorkStation ();
13+ }
14+
15+ // Turn off monitor
16+ void monitor_off () {
17+ SendMessage (HWND_BROADCAST , WM_SYSCOMMAND , SC_MONITORPOWER , (LPARAM ) 2 );
18+ }
19+
20+ // Enter standby mode
21+ void standby () {
22+ SetSystemPowerState (TRUE, FALSE);
23+ }
24+
25+ // Log off
26+ void log_off () {
27+ ExitWindowsEx (EWX_LOGOFF , 0 );
28+ }
29+
30+ // Reboot
31+ void reboot () {
32+ ExitWindowsEx (EWX_REBOOT | EWX_FORCE , SHTDN_REASON_MAJOR_OPERATINGSYSTEM );
33+ }
34+
35+ // Shutdown
36+ void shutdown_pc () {
37+ ExitWindowsEx (EWX_POWEROFF | EWX_FORCE , SHTDN_REASON_MAJOR_OPERATINGSYSTEM );
38+ }
39+
40+ //=======[ Sound Management ]=======//
41+ // Decrease volume
42+ void volume_down () {
43+ keybd_event (VK_VOLUME_DOWN , 0 , 0 , 0 );
44+ keybd_event (VK_VOLUME_DOWN , 0 , KEYEVENTF_KEYUP , 0 );
45+ }
46+
47+ // Increase volume
48+ void volume_up () {
49+ keybd_event (VK_VOLUME_UP , 0 , 0 , 0 );
50+ keybd_event (VK_VOLUME_UP , 0 , KEYEVENTF_KEYUP , 0 );
51+ }
52+
53+ // Mute volume
54+ void mute_volume () {
55+ keybd_event (VK_VOLUME_MUTE , 0 , 0 , 0 );
56+ keybd_event (VK_VOLUME_MUTE , 0 , KEYEVENTF_KEYUP , 0 );
57+ }
58+
59+ // Next track
60+ void next_track () {
61+ keybd_event (VK_MEDIA_NEXT_TRACK , 0 , 0 , 0 );
62+ keybd_event (VK_MEDIA_NEXT_TRACK , 0 , KEYEVENTF_KEYUP , 0 );
63+ }
64+
65+ // Previous track
66+ void prev_track () {
67+ keybd_event (VK_MEDIA_PREV_TRACK , 0 , 0 , 0 );
68+ keybd_event (VK_MEDIA_PREV_TRACK , 0 , KEYEVENTF_KEYUP , 0 );
69+ }
70+
71+ // Play/Pause
72+ void play_pause () {
73+ keybd_event (VK_MEDIA_PLAY_PAUSE , 0 , 0 , 0 );
74+ keybd_event (VK_MEDIA_PLAY_PAUSE , 0 , KEYEVENTF_KEYUP , 0 );
75+ }
76+
77+ //====[ CD ROM ]====//
78+ // Open CD-ROM
79+ void cdrom_open (char drive ) {
80+ char command [32 ];
81+ sprintf (command , "set %cdaudio door open" , drive );
82+ mciSendString (command , NULL , 0 , NULL );
83+ }
84+
85+ // Close CD-ROM
86+ void cdrom_close (char drive ) {
87+ char command [32 ];
88+ sprintf (command , "set %cdaudio door closed" , drive );
89+ mciSendString (command , NULL , 0 , NULL );
90+ }
91+
92+ // Kill process by name
93+ void kill_process (const char * process ) {
94+ char command [256 ];
95+ sprintf (command , "taskkill /IM %s /F" , process );
96+ system (command );
97+ }
98+
99+ // Kill process by PID
100+ void kill_pid (int pid ) {
101+ char command [64 ];
102+ sprintf (command , "taskkill /PID %d /F" , pid );
103+ system (command );
104+ }
105+
106+ // Kill all not responding processes
107+ void kill_not_responding () {
108+ system ("taskkill /FI \"STATUS eq NOT RESPONDING\" /F" );
109+ }
110+
111+ // screen brightness (0-100%)
112+ void set_brightness (int brightness ) {
113+ if (brightness < 0 ) brightness = 0 ;
114+ if (brightness > 100 ) brightness = 100 ;
115+
116+ // Get monitor handle
117+ HANDLE hMonitor = CreateFileA ("\\\\.\\LCD" , GENERIC_WRITE , 0 , NULL , OPEN_EXISTING , 0 , NULL );
118+ if (hMonitor == INVALID_HANDLE_VALUE ) {
119+ printf ("Failed to change brightness.\n" );
120+ return ;
121+ }
122+
123+ DWORD dwBrightness = (brightness * 255 ) / 100 ;
124+ DeviceIoControl (hMonitor , 0x8010204C , & dwBrightness , sizeof (dwBrightness ), NULL , 0 , NULL , NULL );
125+ CloseHandle (hMonitor );
126+ }
127+
128+ // Enable/disable Wi-Fi
129+ void toggle_wifi (int enable ) {
130+ if (enable ) {
131+ system ("netsh interface set interface \"Wi-Fi\" enable" );} else {
132+ system ("netsh interface set interface \"Wi-Fi\" disable" );}}
133+
134+ // Display system information
135+ void system_info () {
136+ system ("cmd /k systeminfo" );
137+ }
138+
139+ // Open program
140+ void open_program (const char * path ) {
141+ ShellExecuteA (NULL , "open" , path , NULL , NULL , SW_SHOWNORMAL );
142+ }
143+
144+ int main (int argc , char * argv []) {
145+ if (argc < 2 ) {
146+ printf ("Usage: %s [lockscreen | volumedown | volumeup | volumemute | nexttrack | prevtrack | playpause | monitoroff | standby | logoff | reboot | shutdown | kill <process> | killpid <PID> | killnotresp | cdrom <drive> open | cdrom <drive> close | brightness <0-100> | wifi <on|off> | sysinfo | open <path>]\n" , argv [0 ]);
147+ return 1 ;
148+ }
149+ if (strcmp (argv [1 ], "lockscreen" ) == 0 ) {
150+ lock_screen ();
151+ } else if (strcmp (argv [1 ], "volumedown" ) == 0 ) {
152+ volume_down ();
153+ } else if (strcmp (argv [1 ], "volumeup" ) == 0 ) {
154+ volume_up ();
155+ } else if (strcmp (argv [1 ], "volumemute" ) == 0 ) {
156+ mute_volume ();
157+ } else if (strcmp (argv [1 ], "nexttrack" ) == 0 ) {
158+ next_track ();
159+ } else if (strcmp (argv [1 ], "prevtrack" ) == 0 ) {
160+ prev_track ();
161+ } else if (strcmp (argv [1 ], "playpause" ) == 0 ) {
162+ play_pause ();
163+ } else if (strcmp (argv [1 ], "monitoroff" ) == 0 ) {
164+ monitor_off ();
165+ } else if (strcmp (argv [1 ], "standby" ) == 0 ) {
166+ standby ();
167+ } else if (strcmp (argv [1 ], "logoff" ) == 0 ) {
168+ log_off ();
169+ } else if (strcmp (argv [1 ], "reboot" ) == 0 ) {
170+ reboot ();
171+ } else if (strcmp (argv [1 ], "shutdown" ) == 0 ) {
172+ shutdown_pc ();
173+ } else if (argc == 3 && strcmp (argv [1 ], "kill" ) == 0 ) {
174+ kill_process (argv [2 ]);
175+ } else if (argc == 3 && strcmp (argv [1 ], "killpid" ) == 0 ) {
176+ kill_pid (atoi (argv [2 ]));
177+ } else if (strcmp (argv [1 ], "killnotresp" ) == 0 ) {
178+ kill_not_responding ();
179+ } else if (argc == 4 && strcmp (argv [1 ], "cdrom" ) == 0 ) {
180+ if (strcmp (argv [3 ], "open" ) == 0 ) {
181+ cdrom_open (argv [2 ][0 ]);
182+ } else if (strcmp (argv [3 ], "close" ) == 0 ) {
183+ cdrom_close (argv [2 ][0 ]);
184+ } else {
185+ printf ("Unknown command: %s %s %s\n" , argv [1 ], argv [2 ], argv [3 ]);
186+ }
187+ } else if (argc == 3 && strcmp (argv [1 ], "brightness" ) == 0 ) {
188+ set_brightness (atoi (argv [2 ]));
189+ } else if (argc == 3 && strcmp (argv [1 ], "wifi" ) == 0 ) {
190+ toggle_wifi (strcmp (argv [2 ], "on" ) == 0 );
191+ } else if (strcmp (argv [1 ], "sysinfo" ) == 0 ) {
192+ system_info ();
193+ } else if (argc == 3 && strcmp (argv [1 ], "open" ) == 0 ) {
194+ open_program (argv [2 ]);
195+ } else {
196+ printf ("Unknown command: %s\n" , argv [1 ]);
197+ }
198+
199+ return 0 ;
200+ }
0 commit comments