如何防止普通用户终止进程?

时间:2014-06-07 13:01:30

标签: c++ windows console-application

如何阻止用户终止进程?

   static BOOL WINAPI console_ctrl_handler(DWORD dwCtrlType)
    {
      switch (dwCtrlType)
      {
      case CTRL_C_EVENT: // Ctrl+C
          { 

              break;
            //return TRUE;
          }

      case CTRL_BREAK_EVENT: // Ctrl+Break
        break;
      case CTRL_CLOSE_EVENT: // Closing the console window //event was caught
//But I guess call exitProcess ()
        break;
        //return TRUE;  
      case CTRL_LOGOFF_EVENT: // User logs off. Passed only to services!
        break;
      case CTRL_SHUTDOWN_EVENT: // System is shutting down. Passed only to services!
        break;
      }

      // Return TRUE if handled this message, further handler functions won't be called.
      // Return FALSE to pass this message to further handlers until default handler calls ExitProcess().
      return TRUE;
    }

这是我的SetConsoleCtrlHandler处理程序。 当我调试时,CTRL_C_EVENT成功返回true,因此被忽略了。

但是CTRL_CLOSE_EVENT也被捕获,并返回TRUE但退出

问题是什么?

如果处理程序捕获CLOSE_WINDOW事件,

是我的解决方案

然后是相同的CreateProcess。

所以任何想要我想要的东西的人,只需将它应用到你的程序中。

BOOL WINAPI console_ctrl_handler(DWORD dwCtrlType){

    HANDLE current_process= GetCurrentProcess();//

    char filePath[MAX_PATH]="";

    DWORD size=MAX_PATH;

    QueryFullProcessImageName(current_process,0,filePath,&size);//

    STARTUPINFO startupInfo = {0};
    startupInfo.cb = sizeof(startupInfo);

    PROCESS_INFORMATION processInformation;

    switch (dwCtrlType){

    case CTRL_C_EVENT: // Ctrl+C

          break;

    case CTRL_CLOSE_EVENT: // Closing the console window //
      //
    system("cls");//

    CreateProcess(
    filePath,
  NULL,
  NULL,
  NULL,
  FALSE,
  NORMAL_PRIORITY_CLASS,
  NULL,
  NULL,
  &startupInfo,
  &processInformation
);  

      break; //

    case CTRL_BREAK_EVENT: // Ctrl+Break
    break;

    case CTRL_LOGOFF_EVENT: // User logs off. Passed only to services!
    break;

    case CTRL_SHUTDOWN_EVENT: // System is shutting down. Passed only to services!
    break;
  }//switch

  // Return TRUE if handled this message, further handler functions won't be called.
  // Return FALSE to pass this message to further handlers until default handler calls ExitProcess().
  return TRUE;

}

1 个答案:

答案 0 :(得分:0)

将程序变为Windows服务[1]。 Windows服务在后台运行,因此没有关闭按钮。它们也可以作为另一个用户运行,因此常规用户无法终止它。

许多服务都在高权限下运行。也可以将该服务作为管理员之外的其他用户运行,这可能是期望的。

[1] http://msdn.microsoft.com/en-us/library/windows/desktop/bb540476%28v=vs.85%29.aspx