运行守护指定程序(c#)
[i=s] 本帖最后由 ivor 于 2019-5-31 07:40 编辑 [/i][code]using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
/**
读取第一个参数启动程序(支持守护*.bat脚本,*.py等等),守护进程防止退出,需要安装.net 2.0 or later
如果不结束守护程序,是无法退出被守护的进程,不要做非法用途。
启动例子:deamon "notepad.exe"
*/
namespace Deamon
{
class Program
{
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
private extern static bool ShowWindow(IntPtr hWnd, Int16 nCmdShow);
public static String fn { get; set; }
static void Main(string[] args)
{
Console.Title = "deamon";
IntPtr hWnd= FindWindow("ConsoleWindowClass", "deamon");
if(hWnd != IntPtr.Zero)
{
ShowWindow(hWnd, 0);
}
if(args.Length > 0)
{
fn = args[0];
Program p = new Program();
p.Launch(fn);
Console.ReadKey();
}
}
public void Launch(String FileName)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = FileName;
Process p = new Process();
p.StartInfo = psi;
p.EnableRaisingEvents = true;
p.Exited += LaunchAgain;
p.Start();
}
public void LaunchAgain(object o, EventArgs e)
{
Launch(fn);
}
}
}
[/code]编译文件:[url]https://pan.baidu.com/s/1kWqLWR9[/url] :lol 弄几个守护进程互相守护,然后再放到任务计划定时触运行。 [b]回复 [url=http://bbs.bathome.net/redirect.php?goto=findpost&pid=206816&ptid=47419]1#[/url] [i]ivor[/i] [/b]
用法是什么呢? :P 这是重复启动吧……
emm不过想搞成真正的“守护”估计需要360那种的驱动防护
页:
[1]