Solved

Wake Computer from standby

Posted on 2004-04-14
4
2,463 Views
Last Modified: 2013-12-03
I want to add programmatically a new task to the task scheduler, which has the option wake computer enabled. I already know how to add a task to the scheduler but I'm not able to eneable the option wake computer. Does anyone how I can do this? Please give me an examlpe in any of the folleowing languages: C++, C#, Delphi, VB, VBScript, Batch-file

Alternatively, I could also live with some other code which wakes the computer at a certain time from standby (using other timers).

0
Comment
Question by:maverick65
  • 3
4 Comments
 
LVL 3

Expert Comment

by:MikProg
ID: 10840931
The only reason to add task to task sheduler is that: User want manage tasks shedule only by task sheduler. When I try to solve same problem the choice was Waitable Timers. Task sheduler REQUIRES user MUST have logon password. But I want shedule tasks send computer to sleep or suspend mode without logging off. AND I NEVER USE PASSWORD on my computer.
The choice Waitable Timers (see Windows API help ). Now I'm on distance from my source codes. But if you interested notice me about and I send you source example.
0
 
LVL 4

Author Comment

by:maverick65
ID: 10841535
I think that you are on the right way. I already have the example from MSDN, but further source code would be helpful.
0
 
LVL 3

Expert Comment

by:MikProg
ID: 10841573
Wait awhile. I will be back monday. If I'll be late I loose my points :(
0
 
LVL 3

Accepted Solution

by:
MikProg earned 500 total points
ID: 10856677
Here it is. It is part of project that never ends so source is not clean. The main Idea is covered by 2 proc's TimerAPCProc and Execute. As you can see main program must create one thread per waitable event, althought another approach is to use WaitForSingle/MultipleObjects.

unit iuStartAlerter;

interface

uses
  Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};

type
  AlertThread = class(TThread)
  private
    FAwakeningAt: TSystemTime;
    Fth: Thandle;
    FSheduleObject: pointer;
    procedure SetName;
    procedure SetAwakeningAt(const Value: TSystemTime);
    procedure SetSheduleObject(const Value: pointer);
  protected
    procedure Execute; override;
  public
    property th: Thandle read Fth;
    property AwakeningAt: TSystemTime read FAwakeningAt write SetAwakeningAt;
    property SheduleObject: pointer read FSheduleObject write SetSheduleObject;
    procedure MakeAction;
  end;

implementation

uses
  iuStartForm, SysUtils;

{$IFDEF MSWINDOWS}
type
  TThreadNameInfo = record
    FType: LongWord;     // must be 0x1000
    FName: PChar;        // pointer to name (in user address space)
    FThreadID: LongWord; // thread ID (-1 indicates caller thread)
    FFlags: LongWord;    // reserved for future use, must be zero
  end;
{$ENDIF}

{ AlertThread }

procedure TimerAPCProc(Subj: pointer;
  dwTimerLowValue: DWORD; dwTimerHighValue: DWORD); stdcall
begin
  with AlertThread(Subj) do
    Synchronize(MakeAction);
end;

procedure AlertThread.SetName;
{$IFDEF MSWINDOWS}
var
  ThreadNameInfo: TThreadNameInfo;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
  ThreadNameInfo.FType := $1000;
  ThreadNameInfo.FName := 'iuStartAlerter';
  ThreadNameInfo.FThreadID := $FFFFFFFF;
  ThreadNameInfo.FFlags := 0;

  try
    RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof(LongWord), @ThreadNameInfo );
  except
  end;
{$ENDIF}
end;

procedure AlertThread.SetSheduleObject(const Value: pointer);
begin
  FSheduleObject := Value;
end;

procedure AlertThread.MakeAction;
begin
  Form1.Startiu();
end;

procedure AlertThread.SetAwakeningAt(const Value: TSystemTime);
begin
  FAwakeningAt := Value;
end;

procedure AlertThread.Execute;
var
  ft, ft1: TFileTime;
  st: TSystemTime;
  c: Int64;
begin
  SetName;
  th:=CreateWaitableTimer(nil,True,'Local\iuStartAlerter');
  SystemTimeToFileTime(FAwakeningAt,ft);
  DateTimeToSystemTime(now,st);
  SystemTimeToFileTime(st,ft1);
  c:=Int64(ft1)-Int64(ft);
  if SetWaitableTimer(th,c,0,@timerAPCProc,Self,True) then
    while not Terminated do
      SleepEx(1000,True);
  CloseHandle(th);
  while not Terminated do
    SleepEx(1000,True);
end;

end.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now