Windows 2000 Service programs wake up on a scheduled Network

Source: Internet
Author: User
Tags cisco 2950

The Unit has two servers. One Bios has the timed start function, while the other does not. So the first thing to do after arriving at work every morning is to press the power switch of this server to start the system! Today, I wrote a Service program for timed network wake-up and installed it on the first server. When the first server is started, the system automatically sends a waeonline (WOL) broadcast package to wake up another server.

Steps:

Start Delphi, File-New-Other-Server Application, and then define the properties of the Service

 

The stored file name is WakeOnlineSrv. pas. The code for this unit is as follows:

Unit WakeOnlineSrv;

Interface

Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
WakeOnlineExecuteThread;

Type
TWakeOnLineServer = class (TService)
Procedure ServiceStart (Sender: TService; var Started: Boolean );
Procedure ServiceStop (Sender: TService; var Stopped: Boolean );
Procedure ServicePause (Sender: TService; var Paused: Boolean );
Procedure ServiceContinue (Sender: TService; var Continued: Boolean );
Private
{Private declarations}
ThdWakeOnline: TWakeOnline;
Public
Function GetServiceController: TServiceController; override;
{Public declarations}
End;

Var
WakeOnLineServer: TWakeOnLineServer;

Implementation

{$ R *. DFM}

Procedure ServiceController (CtrlCode: DWord); stdcall;
Begin
WakeOnLineServer. Controller (CtrlCode );
End;

Function TWakeOnLineServer. GetServiceController: TServiceController;
Begin
Result: = ServiceController;
End;

Procedure TWakeOnLineServer. ServiceStart (Sender: TService;
Var Started: Boolean );
Begin
// Start the service
ThdWakeOnline: = TWakeOnline. Create (False );
Started: = True;
End;

Procedure TWakeOnLineServer. ServiceStop (Sender: TService;
Var Stopped: Boolean );
Begin
// Stop the service
ThdWakeOnline. Terminate;
Stopped: = True;
End;

Procedure TWakeOnLineServer. ServicePause (Sender: TService;
Var Paused: Boolean );
Begin
// Suspend the service
ThdWakeOnline. Suspend;
Paused: = True;
End;

Procedure TWakeOnLineServer. ServiceContinue (Sender: TService;
Var Continued: Boolean );
Begin
// Restore the service
ThdWakeOnline. Resume;
Continued: = True;
End;

End.

Create a New unit File from File-New-Other-Thread Object and save the File name as WakeOnlineExecuteThread. pas. The Code is as follows:

Unit WakeOnlineExecuteThread;

Interface

Uses
Classes, IdUDPClient, SysUtils;

Const
WakeUpTime = '07: 50'; // Notice that Not 7:50!
StopSendTime = '07: 51 ';
TargetComputerMacAddress = '00025556e5c5 ';

Type
TWakeOnLine = class (TThread)
Private
{Private declarations}
Protected
Procedure Execute; override;
End;

Var Sent: Boolean = False;

Implementation

{Important: Methods and properties of objects in visual components can only be
Used in a method called using Synchronize, for example,

Synchronize (UpdateCaption );

And UpdateCaption cocould look like,

Procedure WakeOnLine. UpdateCaption;
Begin
Form1.Caption: = 'updated in a thread ';
End ;}

{WakeOnLine}

Procedure TWakeOnLine. Execute;
Var
MacAddr: String;
FUDP: TIdUDPClient;
SendCode: string;
I, J: Integer;
Bin: String [6];
Begin
{Place thread code here}
// Automatically released upon thread termination
Self. FreeOnTerminate: = True;
// Send flag
Sent: = false;
// Run the following loop if the thread is not terminated
While not terminated do
Begin
// If no message is sent and the time has elapsed
If (not sent) and (copy (formatdatetime ('hh: mm: ss', now), 1, 5) = wakeuptime) then
Begin
// Start sending
Macaddr: = targetcomputermacaddress;

Fudp: = tidudpclient. Create (NiL );
Try
Fudp. Port: = 7;
Fudp. HOST: = '10. 10.10.2 ';
Fudp. Active: = true;

Hextobin (pchar (macaddr), @ bin, 6 );
Sendcode: = stringofchar (#255, 6 );

// Fill the buffer
For I: = 0 to 15 do
For J: = 0 to 5 do
Sendcode: = sendcode + bin [J];

// Send messages six times in a row. It's safe...
For I: = 0 to 5 do
Fudp. Broadcast (sendcode, 7 );

Finally
Fudp. Free;
End;

Sent: = true;

End else

Begin
// If the message has been sent and has reached the next minute of the sending time, reset the sending flag so that the message can be sent in the next day.
If Sent and (Copy (FormatDateTime ('hh: mm: ss', Now), 1, 5) = StopSendTime) then
Sent: = False;

// Rest for 30 seconds to avoid wasting resources
Sleep (30000 );
End;

// Terminate if a termination signal exists.
If Terminated then Break;

End;

End;

End.

Finally, save the project file name as WakeOnline. dpr. The content is as follows:

Program WakeOnline;

Uses
SvcMgr,
WakeOnlineSrv in 'wakeonlinesrv. pa' {WakeOnLineServer: TService },
WakeOnlineExecuteThread in 'wakeonlineexecutethread. pa ';

{$ R *. RES}

Begin
Application. Initialize;
Application. CreateForm (TWakeOnLineServer, WakeOnLineServer );
Application. Run;
End.

Select an appropriate icon for the service program and compile an executable file named WakeOnLine. Exe.

Go to the Cmd window to install the service program: Run: WakeOnLine-install. A prompt is displayed, indicating that the service has been successfully installed.

Right-click my computer-Manage-services and applications-services, and find the installed Service: Wake On Line Server,

 

You can see that it has not been started. Double-click it and click Start:

In the program, the specified time is am to send a broadcast, start a network packet capture program, adjust the time to AM to test, wait for a while, and see what you have caught:

 

Start the target machine...

(Description: Delphi 7 compilation; CISCO 2950 switch; the IBM X205 server is awakened. Note that the Wake On Line option in the Bios is Enable)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.