为什么偶在winXP中用VC6.0编程,调用ExitwindowEx(EWX_SHUTDOWN)没有作用?
怎样用程序改变访问权限?

解决方案 »

  1.   

    在Window98以上版本中关机时要有权限的,具体的做法我这里没有,你搜索以前的帖子,肯定有的
      

  2.   

    程序必须拥有SE_SHUTDOWN_NAME权限才能调用关闭函数
    HANDLE hToken; 
    TOKEN_PRIVILEGES tkp; // Get a token for this process. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
    Error("OpenProcessToken"); // Get the LUID for the shutdown privilege. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, 
    &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. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 
    (PTOKEN_PRIVILEGES)NULL, 0); // Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) error("AdjustTokenPrivileges"); // Shut down the system and force all applications to close. if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0)) error("ExitWindowsEx");
      

  3.   

    //取得当前进程的标记
    if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
    ::AfxMessageBox("OpenProcessToken"); 
    // Get the LUID for the shutdown privilege.
    //寻找关闭系统的特权并赋值给变量TOKEN_PRIVILEGES
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&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. 
    //为当前的进程取得关闭系统的特权
    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES)NULL, 0); 
    // Cannot test the return value of AdjustTokenPrivileges. 
    if (GetLastError() != ERROR_SUCCESS) 
    // ::AfxMessageBox("AdjustTokenPrivileges"); 
    // 关闭系统
    if (!ExitWindowsEx(EWX_POWEROFF|EWX_FORCE,0L)) 
    {
    if(!ExitWindowsEx(EWX_REBOOT|EWX_FORCE,0L))
    {
    if(!ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE,0L))
    {
    if(!ExitWindows(0,0))
    ::AfxMessageBox("重启动错误");
    }
    }
    }
      

  4.   

    //取得当前进程的标记
    if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
    ::AfxMessageBox("OpenProcessToken"); 
    // Get the LUID for the shutdown privilege.
    //寻找关闭系统的特权并赋值给变量TOKEN_PRIVILEGES
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&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. 
    //为当前的进程取得关闭系统的特权
    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES)NULL, 0); 
    // Cannot test the return value of AdjustTokenPrivileges. 
    if (GetLastError() != ERROR_SUCCESS) 
    // ::AfxMessageBox("AdjustTokenPrivileges"); 
    // 关闭系统
    if (!ExitWindowsEx(EWX_POWEROFF|EWX_FORCE,0L)) 
    {
    if(!ExitWindowsEx(EWX_REBOOT|EWX_FORCE,0L))
    {
    if(!ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE,0L))
    {
    if(!ExitWindows(0,0))
    ::AfxMessageBox("重启动错误");
    }
    }
    }