Domanda

I non programmato un WinAPI così ho un piccolo problema qui.

ho bisogno di spegnere il pc dalla mia applicazione.

Ho trovato questo esempio testo del link poi ho trovato questo esempio di come modificare i privilegi testo del link

Ma ho problema come ottenere quel parametro MANICO hToken // accesso handle token

Credo che ho bisogno di fare nella prossima fine di ottenere il parametro   OpenProcessToken LookupPrivilegeValue AdjustTokenPrivileges ma ci sono parametri molto che non ho idea di che cosa fare con loro.

forse hai Jere alcuni esempi di come ottengo che gestiscono parametro hToken per fare quel lavoro.

Tra l'altro ho già visto il seguente post testo

Grazie mille tutti voi.

È stato utile?

Soluzione

Questo è un po 'troppo per i commenti sul risposta di Daniel, quindi mi ha messo qui.

Sembra che il problema principale a questo punto è che il processo non è in esecuzione con i privilegi necessari per eseguire un arresto del sistema.

La documentazione per ExitWindowsEx contengono questo riga:

  

Per arrestare o riavviare il sistema,   il processo chiamante deve utilizzare il   funzione di AdjustTokenPrivileges   abilitare il privilegio SE_SHUTDOWN_NAME.   Per ulteriori informazioni, vedere Running with   Privilegi speciali .

Ci sono anche alcuni esempio di codice . In un pizzico, si può solo copiare questo.

Altri suggerimenti

// ==========================================================================
// system shutdown
// nSDType: 0 - Shutdown the system
//          1 - Shutdown the system and turn off the power (if supported)
//          2 - Shutdown the system and then restart the system
void SystemShutdown(UINT nSDType)
{
    HANDLE           hToken;
    TOKEN_PRIVILEGES tkp   ;

    ::OpenProcessToken(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken);
    ::LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);

    tkp.PrivilegeCount          = 1                   ; // set 1 privilege
    tkp.Privileges[0].Attributes= SE_PRIVILEGE_ENABLED;

    // get the shutdown privilege for this process
    ::AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);

    switch (nSDType)
    {
        case 0: ::ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE, 0); break;
        case 1: ::ExitWindowsEx(EWX_POWEROFF|EWX_FORCE, 0); break;
        case 2: ::ExitWindowsEx(EWX_REBOOT  |EWX_FORCE, 0); break;
    }
}

Si potrebbe utilizzare ShellExecute () per chiamare shutdown.exe

#include<iostream>
using namespace std;
int main(){
system("shutdown -s -f -t 0");
}

Alcuni codice di lavoro per InitiateSystemShutdownEx:

// Get the process token
HANDLE hToken;
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
    &hToken);

// Build a token privilege request object for shutdown
TOKEN_PRIVILEGES tk;
tk.PrivilegeCount = 1;
tk.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
LookupPrivilegeValue(NULL, TEXT("SeShutdownPrivilege"), &tk.Privileges[0].Luid);

// Adjust privileges
AdjustTokenPrivileges(hToken, FALSE, &tk, 0, NULL, 0);

// Go ahead and shut down
InitiateSystemShutdownEx(NULL, NULL, 0, FALSE, FALSE, 0);

Per quanto posso dire, il vantaggio di questo sopra la soluzione ExitWindowsEx è che il processo chiamante non ha bisogno di appartenere alla utente attivo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top