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

标签 c# .net windows-xp acpi winapi

是否有任何 Win32 API 可以在特定时间唤醒已关闭的系统?我见过一个名为 Autopower On 的程序,它可以在指定的时间启动系统。

最佳答案

得到 the below post从一个网站。有人试过这个吗?

框架中没有任何内容,因此您必须“PInvoke”一下。 您需要调用的 API 是 CreateWaitableTimer 和 SetWaitableTimer。 以下是一个完整的 (Q&D) 示例,说明了如何设置 使用以上 Win32 API 将系统从 sleep /休眠状态唤醒。 请注意,这里我将相对唤醒时间设置为 300000000 nSecs。 这意味着计算机将被唤醒(假设他睡着了或 休眠)在设置定时器后 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");
        }
    }
}

关于C#:如何唤醒已关闭的系统?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4061844/

相关文章:

c# - 运算符 '==' 不能应用于类型 'Type?' 和 'Type?' 的操作数

c# - 比较两个 List<string> 是否相等

c# - C# 模型的自定义 setter

security - XP 上的 WMI 访问被拒绝

c# - 寻找组合来自不同程序集的部分类的模式

c# - AspNet.Core Identity - 无法登录用户

c# - OpenXML、SAX 和简单地读取 Xlsx 文件

c# - WPF ViewModel在另一个线程中运行方法,报告进度,更改Button的IsEnabled并显示结果

python - 为什么这个 python WMI 调用给我一个错误?

windows - 没有 CredSSP 的双跳访问复制文件