c++关机,怎么关LAN中的机器。

newman0708 2005-01-24 12:46:09
//ShutDown.cpp

#include "stdafx.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[]){

TOKEN_PRIVILEGES tkp;
HANDLE hToken;

if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
//MessageBox("OpenProcessToken failed!");
cout<<"OpenProcessToken failed!";
}

LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid); //获得本地机唯一的标识
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0); //调整获得的权限

if (GetLastError() != ERROR_SUCCESS)
{
//MessageBox("AdjustTokenPrivileges enable failed!");
cout<<"AdjustTokenPrivileges enable failed!";

}

BOOL fResult =InitiateSystemShutdown(
NULL, // 要关的计算机用户名,可在局域网网中关掉对方的机器,NULL表示关本机
"由于系统不稳定,WINDOWS将在上面的时间内关机,请做好保存工作!", // 显示的消息
10, // 关机所需的时间
TRUE,
TRUE); //设为TRUE为重起,设为FALSE为关机

if(!fResult)
{
//MessageBox("InitiateSystemShutdown failed.");
cout<<"InitiateSystemShutdown failed.";
}

tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0);

if (GetLastError() != ERROR_SUCCESS)
{
//MessageBox("AdjustTokenPrivileges disable failed.");
cout<<"AdjustTokenPrivileges disable failed.";
}

ExitWindowsEx(EWX_SHUTDOWN,0); //开始关机


return 0;
}


以上程序可以关机,但怎么关LAN中的指定机器?

谢谢!


...全文
167 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
paybfly 2005-01-24
  • 打赏
  • 举报
回复
shutdown不就行了吗
dayforever 2005-01-24
  • 打赏
  • 举报
回复
Remarks
To shut down the local computer, the calling thread must have the SE_SHUTDOWN_NAME privilege. To shut down a remote computer, the calling thread must have the SE_REMOTE_SHUTDOWN_NAME privilege on the remote computer. By default, users can enable the SE_SHUTDOWN_NAME privilege on the computer they are logged onto, and administrators can enable the SE_REMOTE_SHUTDOWN_NAME privilege on remote computers. For more information, see Running with Special Privileges.

Common reasons for failure include an invalid or inaccessible computer name or insufficient privilege. The error ERROR_SHUTDOWN_IN_PROGRESS is returned if a shutdown is already in progress on the specified computer.

A non-zero return value does not mean the logoff was or will be successful. The shutdown is an asynchronous process, and it can occur long after the API call has returned, or not at all. Even if the timeout value is zero, the shutdown can still be aborted by applications, services or even the system. The non-zero return value indicates that the validation of the rights and parameters was successful and that the system accepted the shutdown request.

When this function is called, the caller must specify whether or not applications with unsaved changes should be forcibly closed. If the caller chooses not to force these applications closed, and an application with unsaved changes is running on the console session, the shutdown will remain in progress until the user logged into the console session aborts the shutdown, saves changes, closes the application, or forces the application to close. During this period, the shutdown may not be aborted except by the console user, and another shutdown may not be initiated.

Note that calling this function with the value of the bForceAppsClosed parameter set to TRUE avoids this situation. Remember that doing this may result in loss of data.


Windows Server 2003, Windows XP: If the computer is locked and the bForceAppsClosed parameter is FALSE, the last error code is ERROR_MACHINE_LOCKED. If the system is not ready to handle the request, the last error code is ERROR_NOT_READY. The application should wait a short while and retry the call.

braveconf 2005-01-24
  • 打赏
  • 举报
回复
仅仅该个计算机名是不够的,还有改变privilege的。使用SE_REMOTE_SHUTDOWN_NAME查找
oyljerry 2005-01-24
  • 打赏
  • 举报
回复
要目标机器上作个服务端,接收消息,然后关机
newman0708 2005-01-24
  • 打赏
  • 举报
回复



BOOL fResult =InitiateSystemShutdown(
"10.23.4.11", //把这个NULL变成你要关掉的计算机的名字,比如"Server" // 要关的计算机用户名,可在局域网网中关掉对方的机器,NULL表示关本机
"由于系统不稳定,WINDOWS将在上面的时间内关机,请做好保存工作!", // 显示的消息
10, // 关机所需的时间
TRUE,
TRUE); //设为TRUE为重起,设为FALSE为关机


不能成功,出现报错“InitiateSystemShutdown failed.”


hsz8250 2005-01-24
  • 打赏
  • 举报
回复
如果你可以写一个简单的网络程序,LAN中的是你的CLIENT,Server发出一个关机的命令,立刻关闭远程的计算机
dayforever 2005-01-24
  • 打赏
  • 举报
回复
msdn上这么写的,我也不清楚
dayforever 2005-01-24
  • 打赏
  • 举报
回复
lpMachineName
[in] Pointer to the null-terminated string that specifies the network name of the computer to shut down. If lpMachineName is NULL or an empty string, the function shuts down the local computer.
baqiao1211 2005-01-24
  • 打赏
  • 举报
回复
mark
daylove 2005-01-24
  • 打赏
  • 举报
回复
LAN中的pc,可怕办不到吧!
dayforever 2005-01-24
  • 打赏
  • 举报
回复
BOOL fResult =InitiateSystemShutdown(
NULL, //把这个NULL变成你要关掉的计算机的名字,比如"Server" // 要关的计算机用户名,可在局域网网中关掉对方的机器,NULL表示关本机
"由于系统不稳定,WINDOWS将在上面的时间内关机,请做好保存工作!", // 显示的消息
10, // 关机所需的时间
TRUE,
TRUE); //设为TRUE为重起,设为FALSE为关机

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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