
C++远程关机API的学习过程
发布时间:2006-05-05 19:02:59 来源:CSDN 网友评论 0 条
记得刚学C++的时候,喜欢研究API,当时同事有一个高手,写了段代码,我在写程序的时候,莫明妙的,机器突然关掉了!我正在纳闷的时候,我听到了他的奸笑!
原来是他干的,后来我研究了好久InitiateSystemShutdown这个API函数,了解被作弄的原理了,因为我的机器加入了Windows的域,而且域的超级用户我也设置成对我本机有Administrator权限,所以,他才有机可乘!后来写了以下这段代码,让他也在工作的时候被我远程关机,爽啊!学了新东西,又以其人之道还施彼身!
原来是他干的,后来我研究了好久InitiateSystemShutdown这个API函数,了解被作弄的原理了,因为我的机器加入了Windows的域,而且域的超级用户我也设置成对我本机有Administrator权限,所以,他才有机可乘!后来写了以下这段代码,让他也在工作的时候被我远程关机,爽啊!学了新东西,又以其人之道还施彼身!
//ShutDownSystem函数是关本地,自己的机器
BOOL CAlarmClockDlg::ShutDownSystem()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
AfxMessageBox("OpenProcessToken");
// 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);
if(GetLastError()!= ERROR_SUCCESS)
AfxMessageBox("AdjustTokenPrivileges");
// Shut down the system and force all applications to close.
if(!ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE, 0))
{
return FALSE;
}
else
{
return TRUE;
}
}
//shutdownHost这个就是远程关机的C++函数了!hostName可以是机器IP,也可以是机器名字!
BOOL CAlarmClockDlg::shutdownHost(CString hostName)
{
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
BOOL fResult; // system shutdown flag
// Get the current process token handle so we can get shutdown
// privilege.
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
AfxMessageBox("OpenProcessToken failed.");
// Get the LUID for 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 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)
AfxMessageBox("AdjustTokenPrivileges enable failed.");
// Display the shutdown dialog box and start the time-out countdown.
fResult = InitiateSystemShutdown("192.168.100.245", // shut down local computer
"Click on the main window and press the Escape key to cancel shutdown.", // message to user
1, // time-out period
FALSE, // ask user to close apps //注意这一段API调用!
FALSE); // reboot after shutdown
if (!fResult)
{
AfxMessageBox("InitiateSystemShutdown failed.");
}
// Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
{
AfxMessageBox("AdjustTokenPrivileges disable failed.");
}
return TRUE;
}
推荐阅讯
- C++类对象的复制-拷贝构造函数(深拷贝,浅
- 基于VC的WinSock网络编程实用宝典
- MFC中几个有用的字符串操作函数
- 保持C/C++程序代码的可伸缩性
- 用Visual C++实现屏幕抓程序
- VC下调用ACM音频编程接口压缩Wave音频
- 理解c++面向对象程序设计中的抽象理论
- 类模拟的性能分析
- Windows下的函数hook技术
- C++ STL编程轻松入门
阅读排行
- 1.Borland 发布C++ Builder 2006 RAD 环境
- 2.C/C++程序员应聘常见面试题深入剖析
- 3.Visual C++常用数据类型转换详解
- 4.C++中的 static 关键字
- 5.利用VC++实现局域网实时视频传输
- 6.浅谈C/C++内存泄漏及其检测工具
- 7.英国投票否决C++/CLI 微软强攻ISO标准受挫
- 8.VC++下用MSComm控件实现串口通讯
- 9.伪随机数生成及在VC++中的实现
- 10.VC++编程实现对波形数据的频谱分析
专题教程
- 大话G游 专题:手机病毒揭密
- ARP攻击防范与解决方案 路由故障处理手册
- Picasa中文版_Picasa教程 专题:清除流氓软件
- Firefox专题 seo搜索引擎优化专区
- 重装Windows必知的事情 装机之必备软件大行动
病毒专杀栏
