C language system automatic shutdown function code

  • 2020-05-09 19:02:30
  • OfStack

ime_t t; time( & t);
Function name: time
Function prototype: time_t time(time_t *timer)
Function function: get the machine's calendar time or set the calendar time
Function returns: machine calendar time
Parameter description: when timer=NULL, the machine calendar time is obtained; when timer= time value, it is used to set the calendar time; time_t is a type of long
Document: < time.h >


 #include <time.h>
 #include <stdio.h>
 #include <dos.h>
 int main()
 {
 time_t t;
 t=time();
 printf('The number of seconds since January 1,1970 is %ld',t);
 return 0;
 }

Sleep function
 
  introduction:
The Sleep function, which suspends the execution of a program, is used with the top file #include < windows.h > !
Note:
The first English character in Sleep is uppercase 'S'!! Lower case causes a compiler error!!
The 1 form of Sleep function:
Sleep (nusidned long);
The units in Sleep() are in milliseconds, so if you want to hold the function for a second, it's Sleep(1000);
VOID_cdeal Sleep (nusidned long);
'nusidned long' can be a variable or an integer! But 1 must be unsigned!! There was no "-"!
  cases:


 #include<windows.h> // The header file 
   #include <stdio.h>
  int main()
  { 
  int a;
  a=1000;
  Sleep(a);
  printf('%d',a);
  }

SYSTEMTIME structure is defined as follows:

SYSTEMTIME STRUCT
  {WORD wYear; years
  WORD wMonth; month
  WORD wDayOfWeek; Week, 0= Sunday, 1= Monday 1...
WORD wDay; day
  WORD wHour; when
  WORD wMinute; points
  WORD wSecond; seconds
  WORD wMilliseconds; ms
 }; (SYSTEMTIME ENDS)

The fields in the structure are all of word type, while Win32 programs usually use dword type variables. Therefore, before using these data, they should be converted to dword type, which can be realized with movzx instruction.
  corresponds to the function that obtains the system time. The system time can be set with the following two functions:
  invoke GetLocalTime (SystemTime st); Get local time
For Greenwich mean time,   invoke GetSystemTime(SystemTime lt)


#include <stdlib.h>
 int main( )
 {
 system('shutdown -f -s -t 3600'); //3600 Seconds after power off 
}

Timing shutdown program is a simple small program, the core part of the design idea is nothing more than: 1. Set the shutdown time →2. Set the timer, capture the current time of the system and shutdown time comparison →3. Most of the timing shutdown procedures step 1 and step 2 are no different, similar, therefore, here I do not waste time "tang monk". Let's talk about step 1, step 3. Do not know when you write the shutdown program is to use which function to shut down the system? ExitWindows, ExitWindowsEx or InitiateSystemShutdown? These functions require administrator privileges before they can be shut down. So, are there any other simple and straightforward functions that you can use? Yes, that's the powerful system function. This function is declared in the header file stdlib.h and is primarily used to enter system commands on the command line. When writing the command line program, we know that system('pause') can suspend the program, system('ping www.163.com ') can ping netease server, system('systeminfo') can view the system configuration details, system('ipconfig') can view the local host IP address information... Calling system is like manually entering system command 1 directly from the command line. So, the command line shutdown command everyone to clear it? -- shutdown, that's it. Is it clear what to do next? Add a sentence like 'system' (' shutdown-s-t 10') to your program to OK. Is it easier and faster than using ExitWindows or something like that?


Related articles: