How to Shutdown, Restart and LogOff windows using C#?


Here I expalined about How to Shutdown, Restart,LogOff windows using C#.. Using Dllimport attribute done those operation. Dllimport attribute is used to when reusing unmanaged code in a managed application.The benefits of using DllImport to make calls to native code from a managed application. We can Shutdown, Restart and LogOff windows using C# coding,and how to use System.Runtime.InteropServices.

What is the use of "DllImport attribute"?


The DllImport attribute is very useful when reusing existing unmanaged code in a managed application.
For instance, Our managed application might need to make calls to the unmanaged WIN32 API.


code behind of form and add reference of:


using System.Runtime.InteropServices;


Use of System.Runtime.InteropServices

The System.Runtime.InteropServices namespace provides a wide variety of members that support COM interop and platform invoke services

static extern method in your coding...

public static extern int ExitEx(int operationFlag, int operationReason);


For Example:

[DllImport("user32.dll")]



On click event button Operation:



  • call ExitEX method with proper flag


  • For log off operation flag value is 0.


  • ExitEx(0, 0);


  • For shut down operation flag value is 1.


  • ExitEx(1, 0);


  • For Restart operation flag value is 2.


  •  ExitEx(2, 0);

    Sample Code:



    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Runtime.InteropServices;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    [DllImport("user32.dll")]
    public static extern int ExitEx(int operationFlag, int operationReason);
    protected void btnRestart_Click(object sender, EventArgs e)
    {
    ExitEx(2, 0);
    }
    protected void btnLogOff_Click(object sender, EventArgs e)
    {
    ExitWindowsEx(0, 0);
    }
    protected void btnShutDown_Click(object sender, EventArgs e)
    {
    ExitEx(1, 0);
    }
    }


    Conclusion:

    Here how to use DllImport attribute in our coding and what is the usage of DllImport.and also use of System.Runtime.InteropServices in our application.
    Hope this code snippets will help to you....

    Refer the Below Link:

    http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.aspx
    http://msdn.microsoft.com/en-us/library/aa984739(v=vs.71).aspx


    Reference: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.aspx


    Comments

    Author: Sibtain23 Jul 2011 Member Level: Gold   Points : 1

    I have tried this code but getting following error.

    Unable to find an entry point named 'ExitEx' in DLL 'user32.dll'.

    Where I am going wrong? Will you please help.

    Author: PalaniKumar.A25 Jul 2011 Member Level: Gold   Points : 1

    hi,

    try ExitWindowsEx function name....


    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Runtime.InteropServices;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    [DllImport("user32.dll")]
    public static extern int ExitWindowsEx(int operationFlag, int operationReason);
    protected void btnRestart_Click(object sender, EventArgs e)
    {
    ExitWindowsEx(2, 0);
    }
    protected void btnLogOff_Click(object sender, EventArgs e)
    {
    ExitWindowsEx(0, 0);
    }
    protected void btnForcedLogOff_Click(object sender, EventArgs e)
    {
    ExitWindowsEx(4, 0);
    }
    protected void btnShutDown_Click(object sender, EventArgs e)
    {
    ExitWindowsEx(1, 0);
    }
    }


    my posted code working properly to me...
    try above code...



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: