使用SetThreadExecutionState时编译错误

时间:2018-04-13 01:02:56

标签: c++ windows-10 codeblocks

我需要让电脑开机一段时间。我在MSDN上找到了如何使用SetThreadExecutionState()来防止程序在计算机入睡时暂停。我不完全确定该怎么做,因为我已经完成了文章向我展示的所有内容。

我的C ++代码如下:

#include <iostream>
#include <windows.h> // Required for winbase.h
#include <winbase.h> // Required for SetThreadExecutionState
#include <conio.h>
#include <sstream>

using namespace std;

int sec = 14400;   // Default number of seconds until shutdown (4 hr.)
string input = ""; // Input for the the time query 

int main()
{
    system("title Timed Shutdown");
    while(true)
    {
        // Method of getting user input will break once a number or default code is given.
    }
    cout << "Shutting down in " << sec << " seconds." << endl;
    for(int i=0; i<sec; i++)
    {
        cout << sec-i << " seconds remaining till timeout." << endl;
        Sleep(1000);
        if(i%45==0)
            SetThreadExecutionState(ES_SYSTEM_REQUIRED); // First error on compiler. 
    } // Countdown
    // Method that checks for shutdown clearance.
    system("shutdown /s /t 60 /c \"TimedShutdown.exe\"");
    SetThreadExecutionState(ES_CONTINUOUS); // Second error on compiler.
    cout << "System is timing out shutting down in 60 sec." << endl;
    cout << "Press any key to cancel shutdown." << endl;
    getch(); // Prevents the last line from 
    system("shutdown /a"); // Aborts timeout period. 
    return 0;
} 

但是,编译器返回:

|        |=== Build: Debug in shutdown (compiler: GNU GCC Compiler) ===
|main.cpp|In function 'int main()':
|main.cpp|error: 'SetThreadExecutionState' was not declared in this scope
|main.cpp|error: 'SetThreadExecutionState' was not declared in this scope
|        |=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===

有没有办法解决这个问题?或者我在文章中遗漏了什么?

0 个答案:

没有答案