在vc中如何实现重新启动计算机??????

huanghuijin 2004-10-29 01:22:06
我采用了如下两种方法,都不行,在win98环境中
ExitWindows( EWX_REBOOT | EWX_FORCE, 0 )
ShellExecute(NULL,"open","rundll32.exe","user,restartwindows",NULL,SW_SHOW);

请教还有没有其他的可行方法?
多谢
...全文
293 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fujoey 2004-10-29
  • 打赏
  • 举报
回复
up
楼上几位已经说得很清楚了。
oyljerry 2004-10-29
  • 打赏
  • 举报
回复
获得权限,ExitWindowsEx
菲斯可儿 2004-10-29
  • 打赏
  • 举报
回复
The ExitWindows function logs off the current user. It sends the WM_QUERYENDSESSION message to determine if they can be terminated.
To shut down the system, or shut down and restart the system, use the ExitWindowsEx function.

kugou123 2004-10-29
  • 打赏
  • 举报
回复
UP
楼上几位已经说得很清楚了。
huaboy408 2004-10-29
  • 打赏
  • 举报
回复
调用下面的函数,实现:
//完全关机
CloseComputerFunc(TRUE, EWX_POWEROFF);
//安全关机
CloseComputerFunc(TRUE, EWX_SHUTDOWN);
//重启
CloseComputerFunc(TRUE, EWX_REBOOT);

BOOL CloseComputer(BOOL FForce, UINT FFlag)
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;

if (FForce) // Forces processes to terminate
FFlag |= EWX_FORCE;

// Get version info to determine operation
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
//The GetVersionEx function obtains extended information about the version of the operating system that is currently running.
if (GetVersionEx(&osvi) == 0)
return false;

// Determine the platform
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
// Windows NT 3.51, Windows NT 4.0, Windows 2000,
// Windows XP, or Windows .NET Server
//The OpenProcessToken function opens the access token associated with a process.
//If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError.
//打开跟当前进程相关联的access token,输出值是第三个参数hToken
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return false;
//参数1:lpSystemName [in] Pointer to a null-terminated string specifying the name of the system on which the privilege name is looked up. If a null string is specified, the function attempts to find the privilege name on the local system.
//参数2:#define SE_SHUTDOWN_NAME TEXT("SeShutdownPrivilege")
// #define SE_SECURITY_NAME TEXT("SeSecurityPrivilege")
//参数3:lpLuid [out] Pointer to a variable that receives the locally unique identifier by which the privilege is known on the system specified by the lpSystemName parameter.
//The LookupPrivilegeValue function retrieves the locally unique identifier (LUID) used on a specified system to locally represent the specified privilege name.
//获得the shutdown privilege's locally unique identifier on the local system,
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);

//PrivilegeCount Specifies the number of entries in the Privileges array.
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;//激活privilege
//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.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
}
ExitWindowsEx(FFlag, 0);
return TRUE;
}
lixiaosan 2004-10-29
  • 打赏
  • 举报
回复
98下可以直接如你说,但是2000以上操作系统需要先取得权限。。


HANDLE hToken;
TOKEN_PRIVILEGES tkp;

// Get a token for this process.

if ( !OpenProcessToken( GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken) )
return FALSE;

// 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)
return FALSE;

// Shut down the system and force all applications to close.

if ( !ExitWindowsEx( EWX_POWEROFF | EWX_FORCE, 0) )
return FALSE;
return TRUE;
老夏Max 2004-10-29
  • 打赏
  • 举报
回复
2000以上的OS,需要获得关机权限!
edrftgyh 2004-10-29
  • 打赏
  • 举报
回复
HANDLE hToken;
TOKEN_PRIVILEGES tkp;

if (OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
if(LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid))
{
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

if(AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0))
{
if (ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0))
{
hr = S_OK;
}
}
}
}

16,547

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧