45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:关机代码分析

关机代码分析

2016-09-02 20:25:15 来源:www.45fan.com 【

关机代码分析

给你一段代码吧,下面是我的关机程序的部分代码

 

void CShutDownView::OnLogoff()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
LUID sedebugnameValue;
if(!IsWin98())
{
if(!OpenProcessToken( GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
return ;
if(!LookupPrivilegeValue( NULL, SE_SHUTDOWN_NAME , &sedebugnameValue ) )
{
CloseHandle( hToken );
return ;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = sedebugnameValue;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL );
CloseHandle( hToken );
}
if(!ExitWindowsEx(EWX_LOGOFF,0L))
AfxMessageBox("关闭计算机失败!"); ////EWX_REBOOT可以用其它参数代替来实现关闭电源等功能。


}

void CShutDownView::OnExit()
{


CWnd* pMainWindow = AfxGetMainWnd();

::PostMessage(pMainWindow->m_hWnd,WM_CLOSE,0,0);
}

void CShutDownView::OnRestart()
{
/*
步骤:
1、opens the access token associated with a process.
*/

HANDLE hToken;
TOKEN_PRIVILEGES tkp;
LUID sedebugnameValue;
if(!IsWin98())
{
if(!OpenProcessToken( GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
return ;
/* The LookupPrivilegeValue function retrieves the locally unique identifier (LUID)
used on a specified system to locally represent the specified privilege name.
BOOL LookupPrivilegeValue(
LPCTSTR lpSystemName,// address of string specifying the system

LPCTSTR lpName, // address of string specifying the privilege
PLUID lpLuid // address of locally unique identifier
);
*/
if(!LookupPrivilegeValue( NULL, SE_SHUTDOWN_NAME , &sedebugnameValue ) )
{
CloseHandle( hToken );
return ;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = sedebugnameValue;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
/*
The AdjustTokenPrivileges function enables or disables privileges in the specified access token. Enabling or disabling privileges in an access token requires TOKEN_ADJUST_PRIVILEGES access.
TokenPrivileges(
HANDLE TokenHandle, // handle to token that contains privileges
BOOL DisableAllPrivileges,// flag for disabling all privileges
PTOKEN_PRIVILEGES NewState,// pointer to new privilege information
DWORD BufferLength, // size, in bytes, of the PreviousState buffer
PTOKEN_PRIVILEGES PreviousState, // receives original state of changed privileges
PDWORD ReturnLength // receives required size of the PreviousState buffer
);*/

AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL );
CloseHandle( hToken );
}
if(!ExitWindowsEx(EWX_REBOOT,0L))
AfxMessageBox("重新启动失败!"); ////EWX_REBOOT可以用其它参数代替来实现关闭电源等功能。


}

void CShutDownView::OnShutdown()
{

HANDLE hToken;
TOKEN_PRIVILEGES tkp;
LUID sedebugnameValue;
if(!IsWin98())
{
if(!OpenProcessToken( GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
return ;
if(!LookupPrivilegeValue( NULL, SE_SHUTDOWN_NAME , &sedebugnameValue ) )
{
CloseHandle( hToken );
return ;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = sedebugnameValue;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL );
CloseHandle( hToken );
}
if(!ExitWindowsEx(EWX_SHUTDOWN,0L))
AfxMessageBox("关闭计算机失败!"); ////EWX_REBOOT可以用其它参数代替来实现关闭电源等功能。

}


void CShutDownView::OnPoweroff()
{

HANDLE hToken;
TOKEN_PRIVILEGES tkp;
LUID sedebugnameValue;
if(!IsWin98())
{

if(!OpenProcessToken( GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
return ;
if(!LookupPrivilegeValue( NULL, SE_SHUTDOWN_NAME , &sedebugnameValue ) )
{
CloseHandle( hToken );
return ;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = sedebugnameValue;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL );
CloseHandle( hToken );
}
if(!ExitWindowsEx(EWX_POWEROFF,0L))
AfxMessageBox("关闭计算机失败!"); ////EWX_REBOOT可以用其它参数代替来实现关闭电源等功能。

}

BOOL CShutDownView::IsWin98()
{
DWORD dwVersionNum = GetVersion();
if(dwVersionNum<0x80000000)//NT
return FALSE;
else //WIN98
return TRUE;
}

这段代码应该也是可以的,先获取特权然后再.....

BOOL CMainFrame::shutdown()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
BOOL fResult; // Get the current process token handle so we can get shutdown privilege.
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE; // Get the LUID for shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get 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)
return FALSE;
// Actually shutdown
fResult = InitiateSystemShutdown(NULL, NULL, 0, TRUE, FALSE);
// to shutdown
//fResult = InitiateSystemShutdown(NULL, NULL, 0, TRUE, TRUE);
// to reboot
if(!fResult)
return FALSE; // Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
if(GetLastError() != ERROR_SUCCESS)
return FALSE;
return TRUE;
}

 

本文地址:http://www.45fan.com/a/question/71459.html
Tags: 关机 代码 给你
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部