Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
Available now!
Buy at Amazon US or
Buy at Amazon UK



Articles

1.Windows API reference
2.HTML to WML Converter
3.Webcam streaming in VB.NET
4.Remoting with firewalls
5.RSA from first principles
6.Key & MouseLogger in .NET
7.Networking Resource Kit for .NET
8.Automatic Reboot with .NET
9.XAML Schema
10.Migrating VB6 Winsock to VB.NET
11.Migrating C++ sockets to C#
12.RFC Reference guide
13.Lingua - Localization webservice
14.COM Reference guide
15.WMI Reference guide
16.SQL stored procedures
17.TCP & UDP port reference
18..NET Framework reference
19.Ethernet Type codes
21.MAC address assignments
22.DLL entry point reference
23.WHOIS server list
24. Turing Numbers
25. Boost SQL performance
26. Progress Bar in ASP.NET
27. OleDb WebService
27. Internet Explorer

Contact us

Click on the timer and add the following code.

C#

private void tmrReboot_Tick(object sender, System.EventArgs e)

{

 lblCurrentTime.Text = "The time is " + DateTime.Now.ToShortTimeString();

 if (DateTime.Parse(dateTimePicker.Text).ToShortTimeString() == DateTime.Now.ToShortTimeString() && chkReboot.Checked)

  {

    reboot();

  }

}

VB.NET

Private Sub tmrReboot_Tick(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles tmrReboot.Tick

 lblCurrentTime.Text = "The time is " + DateTime.Now().ToShortTimeString()

 If DateTime.Parse(DateTimePicker.Text).ToShortTimeString() = _ DateTime.Now().ToShortTimeString() And chkReboot.Checked Then

  reboot()

 End If

End Sub

What this code does is that it displays the current time on screen, in an easy to read format. If the current time matches the time as set with the DateTimePicker and the reboot check box has been checked then the reboot function is called.

The reboot() function needs to be implemented. This firstly acquires privileges to reboot the computer, and then calls a windows API function to perform the reboot. Parameters are passed to the function which forces a unattended shutdown, where whenever possible any requests for user intervention are ignored.

C#

public void reboot()

{

    AdjustToken();

    ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE | EWX_REBOOT, 0xFFFF);

}

VB.NET

Public Sub reboot()

 AdjustToken()

 ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT, &HFFFF)

End Sub

A reboot requires special privileges in order to be called programmatically. This goes some way to make it more difficult for rogue processes, or viruses to potentially render a computer useless by constantly rebooting it. The function AdjustToken is used to grant these privileges to the application. 

There is a particular style to Windows API programming. It doesn’t have the same neat object oriented architecture as the .NET framework, instead when a resource is requested an integer or handle is returned rather than an object representing the resource. This handle can then be passed to related functions, which then can make alterations to the instance of the resource retrieved in the previous call.

To request extra privileges for a process, the fist task is to get the current process handle using GetCurrentProcess(). With this handle, the process token can be retrieved. The process token contains information on what actions the process is permitted to perform. To request extended permissions to the process token, and therefore the process itself, a reference to the specific permission is requested, then the token is adjusted with a call to AdjustTokenPriviliges().

C#

private void AdjustToken()

{

 TOKEN_PRIVILEGES tkp;

 IntPtr hproc = GetCurrentProcess();

 IntPtr htok = IntPtr.Zero;

 OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );

 tkp.Count = 1;

 tkp.Luid = 0;

 tkp.Attr = SE_PRIVILEGE_ENABLED;

 LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tkp.Luid );

 AdjustTokenPrivileges( htok, false, ref tkp, 0,IntPtr.Zero,IntPtr.Zero);

}

Page 1  Page 3 



Google

Copyright 2012 Open Merchant Account Ltd.
Free SMS UK Free SMS Ireland SMS Gratis Norway SMS Gratis Sverige Ilmainen SMS Suomi SMS Gratis Danmark SMS Tasuta Eestisse SMS Nemokamai Lietuva SMS Bezmaksas Latviju Darmowe smsy Polska SMS Zdarma Ceské SMS Zdarma Slovensko SMS Gratis Deutschland SMS Gratis Schweiz SMS Gratis Österreich SMS Gratuit Belgique SMS Gratis Nederland SMS Gratuit France SMS Gratis Espańa SMS Gratis Portugal Free SMS South Africa Free SMS USA SMS Percuma Malaysia Free SMS Hong Kong