There are many ways to prohibit program running. The first method is to write a separate program and run it automatically at startup. The role of this program is to monitor process information, if a target process is found, it is immediately killed to prevent the program from running. The second method is to write a service, which is relatively hidden. Next I will explain the second method.
There are also many ways to write services. I prefer to write services in C, which is more intuitive and free to write services in C. If some readers do not know how to write service programs in windows, please check related information on your own. The source code of the program is given below.
# 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 implements SS 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 ("memory cannot be allocated! ");
Return 0;
}
File * FP;
Handle process;
Fp = fopen ("C: // memorystatus // scrutinyprocess.txt", "rb ");
If (! FP)
{
Writetolog ("file cannot be opened ");
Return 0;
}
Fgets (name, 128, FP );
Handle hsnapshot = createconlhelp32snapshot (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 );
}
You can write the name of a process that you want to disable in a log file. If you do not know how to install the service, you have to learn it.
By: yyjw
Indicate the source for reprinting.