在windows下禁止程序运行的方法

来源:互联网 发布:matlab多目标优化算法 编辑:程序博客网 时间:2024/05/16 10:19
        禁止程序运行的方式有很多种,第一种方法是写一个单独的程序并且能够在开机的时候自动运行,而这个程序的作用就是监视进程信息,如果发现目标进程则立即把它干掉,从而达到禁止程序运行的目的。第二种方法是写一个服务,这种方法个人觉得比较隐蔽。下面拿第二种方法实现。
      下面就给出windows下的服务程序的源代码。
  1.   #include <windows.h>
  2.   #include <stdio.h>
  3.   #include<tlhelp32.h>
  4.   #include<stdlib.h>
  5.   #include<string.h>
  6.   #define SLEEP_TIME 5000
  7.   #define LOGFILE "C://MemoryStatus//memstatus.txt"
  8.   
  9.   ////////////////////////////////////////////////////////////
  10.   // Declare several global variables to share
  11.   // their values across multiple functions of your program.
  12.   ////////////////////////////////////////////////////////////
  13.   SERVICE_STATUS     ServiceStatus;
  14.   SERVICE_STATUS_HANDLE  hStatus;
  15.   
  16.   ////////////////////////////////////////////////////////////
  17.   // Make the forward definitions of functions prototypes.
  18.   //
  19.   ////////////////////////////////////////////////////////////
  20.   void ServiceMain(int argc, char** argv);
  21.   void ControlHandler(DWORD request);
  22.   int InitService();
  23.   int ScanProcess();
  24.   
  25.   int WriteToLog(char* str)
  26.   {
  27.   FILE* log;
  28.   log = fopen(LOGFILE, "a+");
  29.   if (log == NULL){
  30.   OutputDebugString("Log file open failed.");
  31.   return -1;
  32.   }
  33.   fprintf(log, "%s/n", str);
  34.   fclose(log);
  35.   return 0;
  36.   }
  37.   
  38.   // Service initialization
  39.   int InitService()
  40.   {
  41.   OutputDebugString("Monitoring started.");
  42.   int result;
  43.   result = WriteToLog("Monitoring started.");
  44.   return(result);
  45.   }
  46.   
  47.   // Control Handler
  48.   void ControlHandler(DWORD request)
  49.   {
  50.   switch(request)
  51.   {
  52.   case SERVICE_CONTROL_STOP:
  53.   OutputDebugString("Monitoring stopped.");
  54.   WriteToLog("Monitoring stopped.");
  55.   
  56.   ServiceStatus.dwWin32ExitCode = 0;
  57.   ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  58.   SetServiceStatus (hStatus, &ServiceStatus);
  59.   return;
  60.   
  61.   case SERVICE_CONTROL_SHUTDOWN:
  62.   OutputDebugString("Monitoring stopped.");
  63.   WriteToLog("Monitoring stopped.");
  64.   
  65.   ServiceStatus.dwWin32ExitCode = 0;
  66.   ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  67.   SetServiceStatus (hStatus, &ServiceStatus);
  68.   return;
  69.   
  70.   default:
  71.   break;
  72.   }
  73.   
  74.   // Report current status
  75.   SetServiceStatus (hStatus, &ServiceStatus);
  76.   
  77.   return;
  78.   }
  79.   
  80.   void ServiceMain(int argc, char** argv)
  81.   {
  82.   int error;
  83.   
  84.   ServiceStatus.dwServiceType =
  85.   SERVICE_WIN32;
  86.   ServiceStatus.dwCurrentState =
  87.   SERVICE_START_PENDING;
  88.   ServiceStatus.dwControlsAccepted  =
  89.   SERVICE_ACCEPT_STOP |
  90.   SERVICE_ACCEPT_SHUTDOWN;
  91.   ServiceStatus.dwWin32ExitCode = 0;
  92.   ServiceStatus.dwServiceSpecificExitCode = 0;
  93.   ServiceStatus.dwCheckPoint = 0;
  94.   ServiceStatus.dwWaitHint = 0;
  95.   
  96.   hStatus = RegisterServiceCtrlHandler(
  97.   "MemoryStatus",
  98.   (LPHANDLER_FUNCTION)ControlHandler);
  99.   if (hStatus == (SERVICE_STATUS_HANDLE)0)
  100.   {
  101.   // Registering Control Handler failed
  102.   return;
  103.   }
  104.   
  105.   // Initialize Service
  106.   error = InitService();
  107.   if (error)
  108.   {
  109.   // Initialization failed
  110.   ServiceStatus.dwCurrentState =
  111.   SERVICE_STOPPED;
  112.   ServiceStatus.dwWin32ExitCode = -1;
  113.   SetServiceStatus(hStatus, &ServiceStatus);
  114.   return;
  115.   }
  116.   // We report the running status to SCM.
  117.   ServiceStatus.dwCurrentState =
  118.   SERVICE_RUNNING;
  119.   SetServiceStatus (hStatus, &ServiceStatus);
  120.   
  121.   // MEMORYSTATUS memory;
  122.   // The worker loop of a service
  123.   while (ServiceStatus.dwCurrentState ==
  124.   SERVICE_RUNNING)
  125.   {
  126.   int flag;
  127.   
  128.   if(ScanProcess())
  129.   flag=1;
  130.   else
  131.   flag=0;
  132.   
  133.   
  134.   if (flag==0)
  135.   {
  136.   ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  137.   ServiceStatus.dwWin32ExitCode   = -1;
  138.   SetServiceStatus(hStatus, &ServiceStatus);
  139.   return;
  140.   }
  141.   Sleep(SLEEP_TIME);
  142.   }
  143.   return;
  144.   }
  145.   int ScanProcess()
  146.   {
  147.   PROCESSENTRY32 pe;
  148.   char *name=(char *)malloc(sizeof(char)*128);
  149.   if(name==NULL)
  150.   {
  151.   WriteToLog("无法分配内存!");
  152.   return 0;
  153.   }
  154.   FILE *fp;
  155.   HANDLE process;
  156.   fp=fopen("C://MemoryStatus//ScrutinyProcess.txt","rb");
  157.   if(!fp)
  158.   {
  159.   WriteToLog("无法打开文件");
  160.   return 0;
  161.   }
  162.   fgets(name,128,fp);
  163.   HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
  164.   Process32First(hSnapshot,&pe);
  165.   do{
  166.   if(!strcmp(name,pe.szExeFile))
  167.   {
  168.   process=OpenProcess(PROCESS_TERMINATE,FALSE,pe.th32ProcessID);
  169.   if(process)
  170.   {
  171.   TerminateProcess(process,0);
  172.   WriteToLog(name);
  173.   
  174.   }
  175.   }
  176.   
  177.   }while(Process32Next(hSnapshot,&pe));
  178.   free(name);
  179.   CloseHandle(hSnapshot);
  180.   fclose(fp);
  181.   return 1;
  182.   }
  183.   void main(int argc, char* argv[])
  184.   {
  185.   SERVICE_TABLE_ENTRY ServiceTable[2];
  186.   ServiceTable[0].lpServiceName = "MemoryStatus";
  187.   ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
  188.   
  189.   ServiceTable[1].lpServiceName = NULL;
  190.   ServiceTable[1].lpServiceProc = NULL;
  191.   // Start the control dispatcher thread for our service
  192.   StartServiceCtrlDispatcher(ServiceTable);
  193.   }

  把想要禁止运行的进程名字写在日志文件里就可以达到目的,接下来安装服务器。

转自:http://www.chinaitlab.com/desktop/vc/36343.html

原创粉丝点击