人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當(dāng)前位置:蘿卜系統(tǒng)下載站 > 技術(shù)開發(fā)教程 > 詳細(xì)頁面

C++遠(yuǎn)程關(guān)機(jī)API的學(xué)習(xí)過程

C++遠(yuǎn)程關(guān)機(jī)API的學(xué)習(xí)過程

更新時間:2022-07-14 文章作者:未知 信息來源:網(wǎng)絡(luò) 閱讀次數(shù):

記得剛學(xué)C++的時候,喜歡研究API,當(dāng)時同事有一個高手,寫了段代碼,我在寫程序的時候,莫明妙的,機(jī)器突然關(guān)掉了!我正在納悶的時候,我聽到了他的奸笑!

   原來是他干的,后來我研究了好久InitiateSystemShutdown這個API函數(shù),了解被作弄的原理了,因?yàn)槲业臋C(jī)器加入了Windows的域,而且域的超級用戶我也設(shè)置成對我本機(jī)有Administrator權(quán)限,所以,他才有機(jī)可乘!后來寫了以下這段代碼,讓他也在工作的時候被我遠(yuǎn)程關(guān)機(jī),爽啊!學(xué)了新東西,又以其人之道還施彼身!

  //ShutDownSystem函數(shù)是關(guān)本地,自己的機(jī)器
  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這個就是遠(yuǎn)程關(guān)機(jī)的C++函數(shù)了!hostName可以是機(jī)器IP,也可以是機(jī)器名字!
  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調(diào)用!
   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;
  }

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統(tǒng)下載排行

網(wǎng)站地圖xml | 網(wǎng)站地圖html
主站蜘蛛池模板: 邮箱| 麻城市| 平安县| 淮阳县| 宜兰市| 陆丰市| 增城市| 东莞市| 金阳县| 恩平市| 吉水县| 驻马店市| 平塘县| 广东省| 呼和浩特市| 莱西市| 乌拉特中旗| 锦屏县| 济宁市| 武乡县| 晴隆县| 庆元县| 嵊泗县| 庄浪县| 晋江市| 涪陵区| 玉田县| 绍兴市| 蓬溪县| 普格县| 卢氏县| 保康县| 巩义市| 广水市| 屏南县| 二手房| 清徐县| 大竹县| 江陵县| 宜兰市| 上蔡县|