在windows下禁止程序运行的方法
来源:互联网 发布:matlab多目标优化算法 编辑:程序博客网 时间:2024/05/16 10:19
禁止程序运行的方式有很多种,第一种方法是写一个单独的程序并且能够在开机的时候自动运行,而这个程序的作用就是监视进程信息,如果发现目标进程则立即把它干掉,从而达到禁止程序运行的目的。第二种方法是写一个服务,这种方法个人觉得比较隐蔽。下面拿第二种方法实现。
下面就给出windows下的服务程序的源代码。
下面就给出windows下的服务程序的源代码。
- #include <windows.h>
- #include <stdio.h>
- #include<tlhelp32.h>
- #include<stdlib.h>
- #include<string.h>
- #define SLEEP_TIME 5000
- #define LOGFILE "C://MemoryStatus//memstatus.txt"
- ////////////////////////////////////////////////////////////
- // Declare several global variables to share
- // their values across multiple functions of your program.
- ////////////////////////////////////////////////////////////
- SERVICE_STATUS ServiceStatus;
- SERVICE_STATUS_HANDLE hStatus;
- ////////////////////////////////////////////////////////////
- // Make the forward definitions of functions prototypes.
- //
- ////////////////////////////////////////////////////////////
- void ServiceMain(int argc, char** argv);
- void ControlHandler(DWORD request);
- int InitService();
- int ScanProcess();
- int WriteToLog(char* str)
- {
- FILE* log;
- log = fopen(LOGFILE, "a+");
- if (log == NULL){
- OutputDebugString("Log file open failed.");
- return -1;
- }
- fprintf(log, "%s/n", str);
- fclose(log);
- return 0;
- }
- // Service initialization
- int InitService()
- {
- OutputDebugString("Monitoring started.");
- int result;
- result = WriteToLog("Monitoring started.");
- return(result);
- }
- // Control Handler
- void ControlHandler(DWORD request)
- {
- switch(request)
- {
- case SERVICE_CONTROL_STOP:
- OutputDebugString("Monitoring stopped.");
- WriteToLog("Monitoring stopped.");
- ServiceStatus.dwWin32ExitCode = 0;
- ServiceStatus.dwCurrentState = SERVICE_STOPPED;
- SetServiceStatus (hStatus, &ServiceStatus);
- return;
- case SERVICE_CONTROL_SHUTDOWN:
- OutputDebugString("Monitoring stopped.");
- WriteToLog("Monitoring stopped.");
- ServiceStatus.dwWin32ExitCode = 0;
- ServiceStatus.dwCurrentState = SERVICE_STOPPED;
- SetServiceStatus (hStatus, &ServiceStatus);
- return;
- default:
- break;
- }
- // Report current status
- SetServiceStatus (hStatus, &ServiceStatus);
- return;
- }
- void ServiceMain(int argc, char** argv)
- {
- int error;
- ServiceStatus.dwServiceType =
- SERVICE_WIN32;
- ServiceStatus.dwCurrentState =
- SERVICE_START_PENDING;
- ServiceStatus.dwControlsAccepted =
- SERVICE_ACCEPT_STOP |
- SERVICE_ACCEPT_SHUTDOWN;
- ServiceStatus.dwWin32ExitCode = 0;
- ServiceStatus.dwServiceSpecificExitCode = 0;
- ServiceStatus.dwCheckPoint = 0;
- ServiceStatus.dwWaitHint = 0;
- hStatus = RegisterServiceCtrlHandler(
- "MemoryStatus",
- (LPHANDLER_FUNCTION)ControlHandler);
- if (hStatus == (SERVICE_STATUS_HANDLE)0)
- {
- // Registering Control Handler failed
- return;
- }
- // Initialize Service
- error = InitService();
- if (error)
- {
- // Initialization failed
- ServiceStatus.dwCurrentState =
- SERVICE_STOPPED;
- ServiceStatus.dwWin32ExitCode = -1;
- SetServiceStatus(hStatus, &ServiceStatus);
- return;
- }
- // We report the running status to SCM.
- ServiceStatus.dwCurrentState =
- SERVICE_RUNNING;
- SetServiceStatus (hStatus, &ServiceStatus);
- // MEMORYSTATUS memory;
- // The worker loop of a service
- while (ServiceStatus.dwCurrentState ==
- SERVICE_RUNNING)
- {
- int flag;
- if(ScanProcess())
- flag=1;
- else
- flag=0;
- if (flag==0)
- {
- ServiceStatus.dwCurrentState = SERVICE_STOPPED;
- ServiceStatus.dwWin32ExitCode = -1;
- SetServiceStatus(hStatus, &ServiceStatus);
- return;
- }
- Sleep(SLEEP_TIME);
- }
- return;
- }
- int ScanProcess()
- {
- PROCESSENTRY32 pe;
- char *name=(char *)malloc(sizeof(char)*128);
- if(name==NULL)
- {
- WriteToLog("无法分配内存!");
- return 0;
- }
- FILE *fp;
- HANDLE process;
- fp=fopen("C://MemoryStatus//ScrutinyProcess.txt","rb");
- if(!fp)
- {
- WriteToLog("无法打开文件");
- return 0;
- }
- fgets(name,128,fp);
- HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
- Process32First(hSnapshot,&pe);
- do{
- if(!strcmp(name,pe.szExeFile))
- {
- process=OpenProcess(PROCESS_TERMINATE,FALSE,pe.th32ProcessID);
- if(process)
- {
- TerminateProcess(process,0);
- WriteToLog(name);
- }
- }
- }while(Process32Next(hSnapshot,&pe));
- free(name);
- CloseHandle(hSnapshot);
- fclose(fp);
- return 1;
- }
- void main(int argc, char* argv[])
- {
- SERVICE_TABLE_ENTRY ServiceTable[2];
- ServiceTable[0].lpServiceName = "MemoryStatus";
- ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
- ServiceTable[1].lpServiceName = NULL;
- ServiceTable[1].lpServiceProc = NULL;
- // Start the control dispatcher thread for our service
- StartServiceCtrlDispatcher(ServiceTable);
- }
把想要禁止运行的进程名字写在日志文件里就可以达到目的,接下来安装服务器。
转自:http://www.chinaitlab.com/desktop/vc/36343.html
- 在windows下禁止程序运行的方法
- windows下禁止程序运行
- 在windows下和Linux下运行python程序的方法
- C# 禁止windows程序重复运行的两种基本方法
- C# 禁止windows程序重复运行的两种基本方法
- C# 禁止windows程序重复运行的两种基本方法
- 在windows下运行Hadoop程序的环境配置
- Windows平台下简单运行Java程序的方法
- 在windows下运行CGI程序
- 在Windows下运行C语言程序
- 在Windows安全模式下运行MySQL的方法
- Twisted-在Windows下运行trial的方法
- Windows XP系统禁止运行组策略的方法
- 在Ubuntu 64下运行32位程序的方法
- 禁止程序运行的几种方法(整理)
- Delphi禁止重复运行同一程序的方法
- 使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
- 使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
- EL(Expression Language)
- 男人应该注意的16点
- VS2008: 如何在64位机器上使用Edit and Continue功能
- uboot启动命令设置详解
- 我们一个程序员的爱情书
- 在windows下禁止程序运行的方法
- [多图]全球范围内Linux用户普及度数据
- 美国科学家研制出由病毒构成的微型电池
- 管理人才的七大方法
- FireFox 继续优化 JS处理性能再度大幅提升
- Windows Sysinternals Desktops v1.0 发布 - 超简单的虚拟桌面程序
- 微软对键盘上的Page Up Page Down按钮享有专利权
- path
- USB1.1协议学习经验之5---- USB设备请求