English 中文(简体)
C#: How to wake up system which has been shutdown?
原标题:

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.

最佳回答

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");
        }
    }
}
问题回答

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

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.

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签