Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2005

Tip: Looking for answers? Try searching our database.

ExitWindowsEx() of user32.dll Not Functioning

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
den 2005 - 14 Dec 2005 05:23 GMT
Hi everybody,
 Please look at this code, the rebooting/reset does not happen. Waht is
wrong with this code?
How to fixed this?

Code:
[StructLayout(LayoutKind.Sequential, Pack=1)]
private struct Luid //change to internal public accessor not working
{
  public int Count;
  public long pLuid;
  public int Attr;
}

const int SE_PRIVILEGE_ENABLED = 0x2;
const int TOKEN_QUERY = 0x8;
const int TOKEN_ADJUST_PRIVILEGES = 0x20;
const int EWX_REBOOT = 0x2;
const int EWX_FORCE = 0x4;
const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool
disall,ref Luid newst, int

len, IntPtr prev, IntPtr relen);

[DllImport("user32.dll")]
static extern int ExitWindowsEx(int uFlags, int dwReason);

private void ActivatePrivileges()
{
   Luid uID;
   IntPtr ptrProcess = Process.GetCurrentProcess().Handle;
   IntPtr ptrToken = IntPtr.Zero;
   //Need to adjust the priviledge in order to shut down...
   OpenProcessToken(ptrProcess, TOKEN_ADJUST_PRIVILEGES / TOKEN_QUERY,
ptrToken);
   uID.Count = 1;
   uID.pLuid = 0;
   uID.Attr = SE_PRIVILEGE_ENABLED;
   LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, uID.pLuid);
   AdjustTokenPrivileges(ptrToken, false, uID, 0, IntPtr.Zero,
IntPtr.Zero); //Error Occurs

here
}

//Executing inside
private void btnReset_Click(object sender, System.EventArgs e)
{
   ActivatePrivileges();
   ExitWindowsEx(2, 0);//This does nothing
}

Den2005
Signature

MCP Year 2005, Philippines

Mattias Sjögren - 14 Dec 2005 06:40 GMT
>  Please look at this code, the rebooting/reset does not happen. Waht is
>wrong with this code?

I recommend you start chcking the return values if the functions you
call. That should make it a lot easier to tell where it's failing.

>    OpenProcessToken(ptrProcess, TOKEN_ADJUST_PRIVILEGES / TOKEN_QUERY,
>ptrToken);

You want to use the | (OR) operator to combine the access flags, not
the division operator /.

>    LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, uID.pLuid);

The last parameter must be declared ref or out for uID.pLuid to be
assigned to.

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

den 2005 - 14 Dec 2005 08:32 GMT
Thanks for reply Mattias, I have found another sample and applied and it works.

den2005
Signature

MCP Year 2005, Philippines

> >  Please look at this code, the rebooting/reset does not happen. Waht is
> >wrong with this code?
[quoted text clipped - 14 lines]
>
> Mattias

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.