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:
ExitEx(0, 0);
ExitEx(1, 0);
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
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.