在2000下关不了机????
ExitWindowsEx(EWX_SHUTDOWN, 0);

解决方案 »

  1.   

    /*
    Shut down the windows for any windows type (Win98, WinNt, Win2000, ...)
    use the following parameters to indicate the shut down type:
    [ LOGOFF, SHUTDOWN, REBOOT, FORCE, POWEROFF ] 2004.05.26 modified from original source
    */
    #include <windows.h>int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){
    OSVERSIONINFO osv;
    osv.dwOSVersionInfoSize=sizeof OSVERSIONINFO;
    GetVersionEx(&osv);
    if(osv.dwPlatformId==VER_PLATFORM_WIN32_NT){
    HANDLE hProcess,hToken;
    TOKEN_PRIVILEGES Privileges;
    LUID luid;
    hProcess=GetCurrentProcess();
    OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES,&hToken);
    Privileges.PrivilegeCount=1;
    LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);
    Privileges.Privileges[0].Luid=luid;
    Privileges.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
    AdjustTokenPrivileges(hToken,FALSE,&Privileges,NULL,NULL,NULL);
    }
    UINT shutdowntype;
    if (strcmp(lpCmdLine, "LOGOFF") == 0) {
    shutdowntype = EWX_LOGOFF;
    }
    else if (strcmp(lpCmdLine, "SHUTDOWN") == 0) {
    shutdowntype = EWX_SHUTDOWN;
    }
    else if (strcmp(lpCmdLine, "REBOOT") == 0) {
    shutdowntype = EWX_REBOOT;
    }
    else if (strcmp(lpCmdLine, "FORCE") == 0) {
    shutdowntype = EWX_FORCE;
    }
    else if (strcmp(lpCmdLine, "POWEROFF") == 0) {
    shutdowntype = EWX_POWEROFF;
    }
    else {
    return 0;
    }
    ExitWindowsEx(shutdowntype, 0);
    return 0;
    }
      

  2.   

    这段好用,我测试过了。可以到http://www.56kc.com去看看其他的。
    //关闭系统
    procedure Shutdown;
    var
      hToken:THANDLE;
      tkp:TOKEN_PRIVILEGES;
      ReturnLength,BufferLength: DWORD;
      ver:TOSVersionInfo;
    begin
      ver.dwOSVersionInfoSize:=sizeof(TOSVersionInfo);
      if not GetVersionEX(ver) then Exit;  if ver.dwPlatformId<VER_PLATFORM_WIN32_NT then
      begin
        ExitWindowsEx(EWX_POWEROFF or EWX_FORCE, 0);
        exit;
      end; // Get a token for this process.
     if not OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES OR TOKEN_QUERY, hToken) then
     begin
       ShowMessage('OpenProcessToken');
       Exit;
     end;
    // Get the LUID for the shutdown privilege.
    //    SE_SHUTDOWN_NAME :="SeShutdownPrivilege";
    //    Dest:=@Buf[1];    LookupPrivilegeValue(nil,'SeShutdownPrivilege' , tkp.Privileges[0].Luid);    tkp.PrivilegeCount := 1;  // one privilege to set
        tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;// Get the shutdown privilege for this process.
        BufferLength:=0;
        ReturnLength:=0;
        AdjustTokenPrivileges(hToken, LongBool(false), tkp, BufferLength,nil, ReturnLength);// Cannot test the return value of AdjustTokenPrivileges.   if GetLastError() <> ERROR_SUCCESS then
       begin
         ShowMessage('AdjustTokenPrivileges');
         Exit;
       end;
    // Shut down the system and force all applications to close.   if not ExitWindowsEx(EWX_POWEROFF or EWX_FORCE, 0) then
       begin
          ShowMessage('ExitWindowsEx');
          Exit;
       end;
    end;
      

  3.   

    我写过一个
    procedure ShutDown;
    var
      hdlProcessHandle: Cardinal;
      hdlTokenHandle: Cardinal;
      tmpLuid: Int64;
      tkp: TOKEN_PRIVILEGES;
      tkpNewButIgnored: TOKEN_PRIVILEGES;
      lBufferNeeded: Cardinal;
      Privilege: array[0..0] of _LUID_AND_ATTRIBUTES;
    begin
      hdlProcessHandle := GetCurrentProcess;
      OpenProcessToken(hdlProcessHandle,
        (TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY),
        hdlTokenHandle);  //取得关机特权的LUID
      LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);
      Privilege[0].Luid := tmpLuid;
      Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
      //设置一个特权
      tkp.PrivilegeCount := 1;
      tkp.Privileges[0] := Privilege[0];
      //在当前进程存取标识中加入关机特权
      AdjustTokenPrivileges(hdlTokenHandle,
        False,
        tkp,
        Sizeof(tkpNewButIgnored),
        tkpNewButIgnored,
        lBufferNeeded);  if ForceShutdown then //强制关机
        ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF or EWX_FORCE, $FFFF)
      else
        ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF, $FFFF);
    end;