CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

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

楼主newman0708(nch)2005-01-24 12:46:09 在 C/C++ / C++ 语言 提问

//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中的指定机器?  
   
  谢谢!  
   
   
  问题点数:20、回复次数:11Top

1 楼dayforever(小胖)回复于 2005-01-24 13:05:44 得分 2

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

2 楼daylove(爱晶如梦)(昨夜西风调碧树,独上高楼,望尽天涯路……)回复于 2005-01-24 14:16:40 得分 1

LAN中的pc,可怕办不到吧!Top

3 楼baqiao1211(一船明月一帆风)回复于 2005-01-24 15:15:47 得分 0

markTop

4 楼dayforever(小胖)回复于 2005-01-24 15:54:59 得分 4

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.Top

5 楼dayforever(小胖)回复于 2005-01-24 15:55:16 得分 0

msdn上这么写的,我也不清楚Top

6 楼hsz8250(terry)回复于 2005-01-24 15:58:07 得分 5

如果你可以写一个简单的网络程序,LAN中的是你的CLIENT,Server发出一个关机的命令,立刻关闭远程的计算机Top

7 楼newman0708(nch)回复于 2005-01-24 16:00:19 得分 0

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

8 楼oyljerry(【勇敢的心】→ ㊣提拉米苏√㊣)回复于 2005-01-24 16:12:17 得分 2

要目标机器上作个服务端,接收消息,然后关机Top

9 楼braveconf()回复于 2005-01-24 16:17:52 得分 2

仅仅该个计算机名是不够的,还有改变privilege的。使用SE_REMOTE_SHUTDOWN_NAME查找Top

10 楼dayforever(小胖)回复于 2005-01-24 16:22:03 得分 3

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.  
   
  Top

11 楼paybfly(me)回复于 2005-01-24 22:44:53 得分 1

shutdown不就行了吗Top

相关问题

  • 机器关机故障!急!!!
  • C#实现关机
  • 有关机器码的问题
  • C#关机的代码怎样?
  • 如何检测网络中某台机器是关机状态?
  • 如何让程序知道现在机器关机了呢?
  • 能否控制局域网中机器的开关机?
  • 怎样在程序中区分机器重启还是关机?
  • 我的机器是不是不支持关机 我都快疯了
  • 正常关机 为什么开机器老是要检测d盘

关键词

  • openprocesstoken
  • htoken
  • token
  • privileges

得分解答快速导航

  • 帖主:newman0708
  • dayforever
  • daylove
  • dayforever
  • hsz8250
  • oyljerry
  • braveconf
  • dayforever
  • paybfly

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo