C#:如何唤醒已经关闭的系统?

发布于 2024-09-29 22:58:57 字数 80 浏览 3 评论 0原文

是否有任何 Win32 API 用于在特定时间唤醒已关闭的系统?我见过一个名为 Autopower On 的程序,它能够在指定的时间打开系统电源。

Is there any Win32 API for waking up a system that has been shut down, at a specific time? I have seen a program named Autopower On which is able to power the system on at a specified time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

究竟谁懂我的在乎 2024-10-06 22:58:57

得到以下帖子来自某个网​​站。有人尝试过这个吗?

框架中没有任何内容,因此您必须“PInvoke”一下。
您需要调用的 API 是 CreateWaitableTimer 和 SetWaitableTimer。
以下是一个完整的(Q&D)示例,说明了如何设置
使用上述 Win32 API 将系统从睡眠/休眠状态唤醒。
请注意,这里我将相对唤醒时间设置为 300000000 纳秒。
这意味着计算机将被唤醒(假设他睡着了或者
设置定时器后 30 秒内休眠)。
有关 SetWaitableTimer 及其参数的详细信息,请参阅 MSDN 文档。

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Willys
{
    class Program
    {
        [DllImport("kernel32.dll")]
        public static extern IntPtr CreateWaitableTimer(IntPtr lpTimerAttributes,
        bool bManualReset, string lpTimerName);

        [DllImport("kernel32.dll")]
        public static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long
        pDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr
        lpArgToCompletionRoutine, bool fResume);

        [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
        public static extern Int32 WaitForSingleObject(IntPtr handle, uint
        milliseconds);

        static void Main()
        {
            SetWaitForWakeUpTime();
        }

        static IntPtr handle;
        static void SetWaitForWakeUpTime()
        {
            long duetime = -300000000; // negative value, so a RELATIVE due time
            Console.WriteLine("{0:x}", duetime);
            handle = CreateWaitableTimer(IntPtr.Zero, true, "MyWaitabletimer");
            SetWaitableTimer(handle, ref duetime, 0, IntPtr.Zero, IntPtr.Zero, true);
            uint INFINITE = 0xFFFFFFFF;
            int ret = WaitForSingleObject(handle, INFINITE);
            MessageBox.Show("Wake up call");
        }
    }
}

Got the below post from a site. Any body tried this?

Nothing in the framework, so you'll have to "PInvoke" a bit.
The API's you need to call are CreateWaitableTimer and SetWaitableTimer.
Following is a complete (Q&D) sample that illustrates how you can set a
system to be awoken from sleep/hibernate using above Win32 API's.
Note that here I'm setting a relative wakeup time of 300000000 nSecs.
That means that the computer will wake-up (supposing he's asleep or
hibernating) within 30 seconds after setting the timer.
Consult the MSDN docs for details on SetWaitableTimer and it's arguments.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Willys
{
    class Program
    {
        [DllImport("kernel32.dll")]
        public static extern IntPtr CreateWaitableTimer(IntPtr lpTimerAttributes,
        bool bManualReset, string lpTimerName);

        [DllImport("kernel32.dll")]
        public static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long
        pDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr
        lpArgToCompletionRoutine, bool fResume);

        [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
        public static extern Int32 WaitForSingleObject(IntPtr handle, uint
        milliseconds);

        static void Main()
        {
            SetWaitForWakeUpTime();
        }

        static IntPtr handle;
        static void SetWaitForWakeUpTime()
        {
            long duetime = -300000000; // negative value, so a RELATIVE due time
            Console.WriteLine("{0:x}", duetime);
            handle = CreateWaitableTimer(IntPtr.Zero, true, "MyWaitabletimer");
            SetWaitableTimer(handle, ref duetime, 0, IntPtr.Zero, IntPtr.Zero, true);
            uint INFINITE = 0xFFFFFFFF;
            int ret = WaitForSingleObject(handle, INFINITE);
            MessageBox.Show("Wake up call");
        }
    }
}
单调的奢华 2024-10-06 22:58:57

这个 Wake on LAN (WOL) 代码项目帖子可能有用(主板并且网卡必须支持WOL)

This Wake on LAN (WOL) code project post might be of use (Both motherboard and NIC must support WOL)

七月上 2024-10-06 22:58:57

一旦您实际关闭计算机(不是睡眠或休眠),您就无法直接从 C#、C++ 代码中唤醒它。毕竟操作系统本身已关闭。

唯一的机会是您的主板支持某种计时器机制。并使用一些 C++ 函数能够将一些标志写入 BIOS 并设置计时器。

Once you actually shut down your computer(not sleeping or hibernating) you can't wake it up from C#, C++ code directly. After all the OS itself is closed.

The only chance would be for your motherboard to support some kind of timer mechanism. And with some C++ function to be able to write some flags into the BIOS and set that timer.

攀登最高峰 2024-10-06 22:58:57

使用 Windows 的“计划任务”菜单。请参阅:

http://www. howtogeek.com/119028/how-to-make-your-pc-wake-from-sleep-automatically/

您创建一个要在唤醒时运行的程序,然后手动将其输入到 Windows 计划任务中列表在特定时间后第一次唤醒。

当你的程序唤醒时,我不确定你的程序是否需要调用Windows API来
停止再次唤醒它,如果您希望程序在启动另一个计划任务以再次唤醒它后关闭电脑电源,则同上。需要研究一下有哪些 Windows API 可以用于此目的。

Use the windows "Scheduled Task" menu. See:

http://www.howtogeek.com/119028/how-to-make-your-pc-wake-from-sleep-automatically/

You create a program that you want to run upon wake up and then manually enter it into the windows scheduled task list for the first time to wake after a specific time.

When you program awakens, I am not sure if you need your program to call a Windows API to
stop it being awoken again, ditto if you want your program to power off the PC after starting another sceduled task to awaken it again. Need to reserach what windows APIs there are for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文