在windows下禁止程序运行的方法
2008-08-23 08:41
253 查看
禁止程序运行的方式有很多种,第一种方法是写一个单独的程序并且能够在开机的时候自动运行,而这个程序的作用就是监视进程信息,如果发现目标进程则立即把它干掉,从而达到禁止程序运行的目的。第二种方法是写一个服务,这种方法个人觉得比较隐蔽。下面拿第二种方法实现。
下面就给出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下的服务程序的源代码。
#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服务运行方法
- 在Eclipse中运行Android程序报 Failed to write core dump. Minidumps are not enabled by default on client versions of Windows 解决方法
- windows服务编写和“以管理员运行”程序的方法
- 在windows下和Linux下运行python程序的方法
- Win8.1、Win8、Win7怎么禁止某程序运行?组策略禁止程序运行的方法
- windows安装程序无法将windows配置为在此计算机的硬件上运行 解决方法
- VS 中引入app.manifest(c#程序在win7及以上windows版本下以管理员权限运行方法)
- windows程序 UAC设置,程序运行提示使用管理员权限运行的方法
- 在windows电脑上运行linux程序软件的简单方法
- 在WINDOWS中用户未登录的情况下运行程序的方法
- Delphi禁止重复运行同一程序的方法
- windows中运行qt5构建的程序提示 无法启动此程序,因为计算机中丢失qt5Cored.dll 解决方法
- C# winform 禁止程序重复运行方法
- linux 运行 windows 程序的五个方法
- Windows下 Win+R 快速运行程序设定方法
- 以windows service方式运行Python程序的方法
- 使用Windows下的intellij IDEA 运行MapReduce程序远程调用Hadoop的hdfs(非Maven方法)
- Windows平台下简单运行Java程序的方法
- golang 另类方法后台运行程序(linux、windows)
- iOS_禁止程序在后台挂起或运行的方法