来自:http://codego.net/4121758/
ExitWindows()究竟怎么用?太简单!
vc
怎样用它,和ExitWindowEX()函数上有什么区别?
本文地址 :CodeGo.net/4121758/
-------------------------------------------------------------------------------------------------------------------------
1. 好象ExitWindowsEx是32位的API,比Exitwindows要好点。
2. BOOL MyExitWindowsEx(UINT uFlag)
{
OSVERSIONINFO osv; //定义一个操作系统信息的结构体
osv.dwOSVersionInfoSize=sizeof OSVERSIONINFO;
GetVersionEx(&osv); //查询当前操作系统
if(osv.dwPlatformId==VER_PLATFORM_WIN32_NT) //判断是否是2000/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);
}
ExitWindowsEx(uFlag,0);
}
3. 在我所知道的里面,你所说的ExitWindow()已被新的函数取代(ExitWindowsEx ()),函数本身Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long是VB的声明方式,在VC++中不必要,但是使用是一致的,共需要两个参数 CodeGo.net,优先个参数可以是:
EWX_FORCE 强迫中止没有响应的进程
EWX_LOGOFF 中止进程,然后注销
EWX_SHUTDOWN 关掉系统电源(如果可能的话,ATX电源就可以)
EWX_REBOOT 重新引导系统
EWX_SHUTDOWN 关闭系统
第二个参数是“零”,我想,这应该足够。
4. exitwindow()是一个老的api在w98以上及wnt上不好用 exitwindowex()则可以 带2个参数,值只能是0或1。combinations使用,别是关机,重起,注销。你可以把参数植带进去试试就只知道。比如exitwindowex(0,0)可能是关机,而exitwindowex(1,0)是重起.
当然这个api 在使用前要声明以下才可以用的。如果你不知道就再提问。
5. 重起函数
void Reboot()
{
DWORD dwVersion = GetVersion();
HMODULE hDll = NULL;
if (dwVersion<0x80000000)
{//NT
hDll = LoadLibrary("advapi32.dll");
if(!hDll) goto l_clean;
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
BOOL (WINAPI* pfOpenProcessToken)(HANDLE,DWORD,PHANDLE);
*(DWORD*)&pfOpenProcessToken = (DWORD)GetProcAddress(hDll,"OpenProcessToken");
if(!pfOpenProcessToken) goto l_clean;
BOOL (WINAPI* pfLookupPrivilegeValueA)(LPCTSTR,LPCTSTR,PLUID);
*(DWORD*)&pfLookupPrivilegeValueA= (DWORD)GetProcAddress(hDll,"LookupPrivilegeValueA");
if(!pfLookupPrivilegeValueA) goto l_clean;
BOOL (WINAPI* pfAdjustTokenPrivileges)(HANDLE,BOOL,PTOKEN_PRIVILEGES,DWORD,PTOKEN_PRIVILEGES,PDWORD);
*(DWORD*)&pfAdjustTokenPrivileges= (DWORD)GetProcAddress(hDll,"AdjustTokenPrivileges");
if(!pfAdjustTokenPrivileges) goto l_clean;
// Get a token for this process.
if (!(*pfOpenProcessToken)(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return;
// Get the LUID for the shutdown privilege.
(*pfLookupPrivilegeValueA)(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.
(*pfAdjustTokenPrivileges)(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS) goto l_clean;
// Shut down the system and force all applications to close.
if (!ExitWindowsEx(EWX_REBOOT , 0)) goto l_clean;
tkp.Privileges[0].Attributes = 0;
(*pfAdjustTokenPrivileges)(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
}
else
{
if (!ExitWindowsEx(EWX_REBOOT , 0)) return;
}
l_clean:
if(hDll)
FreeLibrary(hDll);
}
本文标题 :ExitWindows()究竟怎么用?太简单!