🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #9323

Re: ExitWindowsEx

#9323
From: Edward
Date: Thu, 15 Jul 2004 20:25
132 lines
4727 bytes
Right code

 Description de la source
Une fonction pour arr�ter Windows (ou red�marrer) en C++Builder 5
Le param�tre indique s'il faut red�marrer (TRUE) ou arr�ter (FALSE)


 Source
void __fastcall TFrmMain::mvCloseWindows(bool pReboot)
{
TOKEN_PRIVILEGES wTokenIn, wTokenOut;
DWORD wLength;
HANDLE wCurrentProcess, wToken;
_LUID wLuid;
OSVERSIONINFO wOsVer;
try
    {
    wCurrentProcess = GetCurrentProcess();
    OpenProcessToken(wCurrentProcess, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
&wToken);
    LookupPrivilegeValue(NULL, "SeShutdownPrivilege", &wLuid);
    wTokenIn.PrivilegeCount = 1;
    wTokenIn.Privileges[0].Luid = wLuid;
    wTokenIn.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    AdjustTokenPrivileges(wToken, FALSE, &wTokenIn,
sizeof(TOKEN_PRIVILEGES), &wTokenOut,&wLength);
    CloseHandle(wToken);
    }
__finally
    {
    Application->ProcessMessages();
    if (pReboot)
    ExitWindowsEx(EWX_REBOOT | EWX_FORCEIFHUNG, 0);
    else
        {
        wOsVer.dwOSVersionInfoSize = sizeof(wOsVer);
        GetVersionEx(&wOsVer);
        if (wOsVer.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
        ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCEIFHUNG, 0);
        else
            ExitWindowsEx(EWX_POWEROFF | EWX_FORCEIFHUNG, 0);
        }
    }
}

"Brent" <nospam@nospam.com> escreveu na mensagem
news:40ec8643@newsgroups.borland.com...
> Hi Edward,
>
> I gave it a go, but unfortunately it didn't work.  But the code has put me
> into the right area for research.  Had a look at the AdjustTokenPrivileges
> API function, and it said that it can not add privileges, only enable of
> disable existing privileges for an application.
>
> Looks like I'm playing catchup from Win95 to WinXP.
>
> cheers, and thanks for the help.
>
> Brent
>
> "Edward" <Registrados2004@aol.com> wrote in message
> news:40eb6806$1@newsgroups.borland.com...
> > Try this:
> >
> > and do not forget #include <shellapi.h>
> >
> > TOKEN_PRIVILEGES wTokenIn, wTokenOut;
> > DWORD wLength;
> > HANDLE wCurrentProcess, wToken;
> > _LUID wLuid;
> > OSVERSIONINFO wOsVer;
> > try
> >     {
> >     wCurrentProcess = GetCurrentProcess();
> >     OpenProcessToken(wCurrentProcess, TOKEN_ADJUST_PRIVILEGES |
> TOKEN_QUERY,
> > &wToken);
> >     LookupPrivilegeValue(NULL, "SeShutdownPrivilege", &wLuid);
> >     wTokenIn.PrivilegeCount = 1;
> >     wTokenIn.Privileges[0].Luid = wLuid;
> >     wTokenIn.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
> >     AdjustTokenPrivileges(wToken, FALSE, &wTokenIn,
> > sizeof(TOKEN_PRIVILEGES), &wTokenOut,&wLength);
> >     CloseHandle(wToken);
> >     }
> > __finally
> >     {
> >     Application->ProcessMessages();
> >     if (1)
> >       ExitWindowsEx(EWX_REBOOT | EWX_FORCEIFHUNG, 0);
> >     else
> >         {
> >         wOsVer.dwOSVersionInfoSize = sizeof(wOsVer);
> >         GetVersionEx(&wOsVer);
> >         if (wOsVer.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
> >         ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCEIFHUNG, 0);
> >         else
> >             ExitWindowsEx(EWX_POWEROFF | EWX_FORCEIFHUNG, 0);
> >         }
> >     }
> >
> >
> >
> >
> >
> > "Brent" <nospam@nospam.com> escreveu na mensagem
> > news:40eb5ae2$1@newsgroups.borland.com...
> > > Hi All,
> > >
> > > ExitWindowsEx(EWX_SHUTDOWN, 0);
> > >
> > > I want to use the above code to turn the computer off.  On XP, the
> program
> > > will only shut down to the login screen.  How do I ensure that the
> program
> > > has the SE_SHUTDOWN_NAME privilege set?  Is it something I can do
within
> > the
> > > application.
> > >
> > > Any help greatly appreciated.
> > >
> > > thanks,
> > >
> > > Brent
> > >
> > >
> > >
> >
> >
>
>
Technical Details
Message-ID: <40f6e854@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <40eb5ae2$1@newsgroups.borland.com> <40eb6806$1@newsgroups.borland.com> <40ec8643@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,877cf1e5dc319316
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII
Reply-To: "Edward" <Registrados2004@aol.com>
From: "Edward" <Registrados2004@aol.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <40eb5ae2$1@newsgroups.borland.com> <40eb6806$1@newsgroups.borland.com> <40ec8643@newsgroups.borland.com>
Subject: Re: ExitWindowsEx
Date: Thu, 15 Jul 2004 17:25:53 -0300
Lines: 134
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
NNTP-Posting-Host: 200.213.49.155
Message-ID: <40f6e854@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1089923156 200.213.49.155 (15 Jul 2004 13:25:56 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail