国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C++遠程關機API的學習過程

2019-11-17 05:27:05
字體:
來源:轉載
供稿:網友
    記得剛學C++的時候,喜歡研究API,當時同事有一個高手,寫了段代碼,我在寫程序的時候,莫明妙的,機器忽然關掉了!我正在納悶的時候,我聽到了他的奸笑!

   原來是他干的,后來我研究了好久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_SHUTDOWNEWX_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;
   }



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁南县| 古浪县| 高碑店市| 美姑县| 巴林右旗| 永泰县| 绥阳县| 乳山市| 威宁| 金门县| 毕节市| 永新县| 延津县| 鱼台县| 墨竹工卡县| 宜城市| 江川县| 江都市| 澳门| 德惠市| 荔浦县| 潞城市| 米林县| 瑞安市| 武夷山市| 陆川县| 精河县| 泰兴市| 鄂尔多斯市| 承德县| 鄂托克旗| 镇沅| 信宜市| 平阳县| 通海县| 通许县| 那坡县| 许昌市| 桂林市| 灵山县| 泰兴市|