Use VC ++ to sleep under Win2000XP

xiaoxiao2021-03-06  56

Use VC to sleep under Win2000 / XP

[Excerpt from: Pacific Computer Network]

It is very simple to achieve sleeping function, as long as you do the sleep function setpower () as follows, in fact, sleep is simple, only API: setsystempowerstate (), just like shutdown / logout / restart, Just need EXITWINDOWEX (), but these APIs can be used directly in 98, but they cannot be used in 2000 / XP, because the user privilege is involved, there is no permission that cannot be permissible, so, we must get permissions, below Code: #define RTN_ERROR 13 VOID Perr (LPTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTER SZAPI, DWORD DWLASTERROR) // A function called when you sleep, used to record the error {lptstr messagebuffer during sleep {LPTSTR MessageBuffer; fprintf (stderr, "% s error ! (rc =% lu) / n ", szAPI, dwLastError); if (dwBufferLength = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwLastError, LANG_NEUTRAL, (LPTSTR) & MessageBuffer, 0, NULL)) {DWORD dwBytesWritten; WriteFile (GetStdHandle ( STD_ERROR_HANDLE), MessageBuffer, dwBufferLength, & dwBytesWritten, NULL); LocalFree (MessageBuffer);}} INT SetPower () there // main function of the function {TOKEN_PRIVILEGES tp; HANDLE hToken; LUID luid; LPTSTR MachineName = NULL;! if (OpenProcessToken (GetCurrentProcess (), From here token_adjust_privileges, & htokeen) {Perr ("openprocessToken", getLastError ()); return RTN_ERROR;} if (! LookupPrivilegeValue (MachineName, SE_SHUTDOWN_NAME, & luid)) {PERR ( "LookupPrivilegeValue", GetLastError ()); return RTN_ERROR;} tp.PrivilegeCount = 1; tp.Privileges [0] .Luid = luid; tp .Privileges [0] .Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges (hToken, FALSE, & tp, sizeof (TOKEN_PRIVILEGES), NULL, NULL); here, it is to obtain permission // SetSystemPowerState (FALSE, TRUE); return 0;} Thus, in When you need to sleep, you call setPower (); you can sleep, but you should pay attention to: Sleeping support for advanced power management must be opened.

转载请注明原文地址:https://www.9cbs.com/read-112954.html

New Post(0)