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

How to prevent Normal users from terminating process?

本文关键字:终止 进程 用户 何防止      更新时间:2023-10-16

如何阻止用户终止进程??

   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;
}

把你的程序变成Windows服务[1]。Windows服务在后台运行,所以没有关闭按钮。它们还可以作为另一个用户运行,这样普通用户就不能终止它。

许多服务在高权限下运行。还可以以管理员以外的其他用户运行服务,这可能是需要的。

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