C#编写WINNT服务
C#编写WINNT服务,随便解决安卓开发遇到的5037被众多程序无节操占用的问题
需求分析:
最近重新开始学习安卓开发,好久不用的ADT集成开发环境频繁遇到不能在仿真机和真机上调试的问题,也就是本人另一篇博文描述的ADB(Android Debug Bridge)监控的5037被金山词霸暗自集成的金山手机助手、腾讯手机管家、豌豆荚等众多软件围攻的情形。需要详细了解,请移步:金山词霸你占我5037端口干嘛,费了你。那些流氓软件给我们安卓开发人员带来了很多烦恼啊,它们凭什么那么没节操?手动结束进程还真麻烦,有时还不知道是那个程序在作怪呢。所以想了下,写个WINNT服务检查当前活动的进程,如果有?adb这样的进程,且进程的运行目录有adb的必须依赖的几个dll文件时,就说明一定要费了它。
WINNT服务科普:
NT服务程序可随操作系统启动而启动,没有图形界面,资源占用小。注册安装好服务后可在系统服务控制台启动、停止等。
关键代码说明:
1、工作线程每隔1秒检查一下,系统当前活动的进程,把非adb的假adb进程终止掉。

/// <summary>
/// 检测当前进程
/// </summary>
private void Guard_adb_Work()
{
while (true)
{
var processes = Process.GetProcesses().Where(p => p.ProcessName.Contains("adb") && p.ProcessName.Length > 3).ToList();
if (processes.Count > 0)
{
processes.ForEach(delegate(Process p)
{
var fileInfo = new FileInfo(p.MainModule.FileName);
//检查程序目录是否有adb的api dll类库,防止误杀
bool apiLibExists = fileInfo.Directory.GetFiles().Where(f => f.Name.Equals("AdbWinApi.dll")).Count() > 0;
if (apiLibExists)
{
WinCommand.startcmd(fileInfo.FullName, "kill-server");
//p.Kill();
//你可恶我比你更可恶,但是Win7一般拿不到管理员权限,可能会删除失败
try
{
if (!EventLog.SourceExists(ServiceName))
{
EventLog.CreateEventSource(ServiceName, "应用程序");
}
EventLog.WriteEntry(string.Format("进程【{0}】非法占用5037端口。已经被消灭。", fileInfo.Name), EventLogEntryType.Warning);
fileInfo.Delete();
}
catch
{ }
}
});
}
System.Threading.Thread.Sleep(1000);
}
}

2、adb命名终止adb server端,狡猾的众流氓软件会检查adb.exe是否运行,如果运行了,就直接使用adb的api获取当前已经通过USB等接口连接到PC的安卓手机。
可是ADT就不行了,它只认adb.exe这个进程。所以奇怪的是我把腾讯的手机管家的tadb.exe进程干掉了之后,插上手机腾讯手机管家还是会弹框说已经连接了安卓手机,是否启用手机管家这样的提示框。
C#运行命名行:
/// <summary> /// 执行命令行 /// </summary> public class WinCommand { public static string startcmd( string command) { string output = "" ; try { Process cmd = new Process(); cmd.StartInfo.FileName = command; cmd.StartInfo.UseShellExecute = false ; cmd.StartInfo.RedirectStandardInput = true ; cmd.StartInfo.RedirectStandardOutput = true ; cmd.StartInfo.CreateNoWindow = true ; cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; cmd.Start(); output = cmd.StandardOutput.ReadToEnd(); Console.WriteLine(output); cmd.WaitForExit(); cmd.Close(); } catch (Exception e) { Console.WriteLine(e); } return output; } public static string startcmd( string command, string argument) { string output = "" ; try { Process cmd = new Process(); cmd.StartInfo.FileName = command; cmd.StartInfo.Arguments = argument; cmd.StartInfo.UseShellExecute = false ; cmd.StartInfo.RedirectStandardInput = true ; cmd.StartInfo.RedirectStandardOutput = true ; cmd.StartInfo.CreateNoWindow = true ; cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; cmd.Start(); output = cmd.StandardOutput.ReadToEnd(); Console.WriteLine(output); cmd.WaitForExit(); cmd.Close(); } catch (Exception e) { Console.WriteLine(e); } return output; } } |
执行adb kill-server命名之后,流氓软件启动的?adb.exe的进程会停止。
3、WINNT服务程序的调试
a.使用“附加到进程”功能来调试以及部署的服务程序,这里不解释。这个比较烦。
b.将服务的OnStart()方法改成public new 关键字标识,然后在Program应用程序入口处将 "ServiceBase.Run(ServicesToRun)"相关代码注释掉,改为new ????Service().OnStart()。这样就是变成普通控制台程序的调试了。
4、WINNT服务程序的安装
a、启动服务程序带传递参数的办法:
/// <summary> /// 应用程序的主入口点。 /// </summary> static void Main( string [] args) { #region 处理传参数,安装、卸载服务 if (args.Length > 0) { AssemblyInstaller myAssemblyInstaller; myAssemblyInstaller = new AssemblyInstaller(); myAssemblyInstaller.UseNewContext = true ; myAssemblyInstaller.Path = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + System.AppDomain.CurrentDomain.FriendlyName; System.Collections.Hashtable mySavedState = new System.Collections.Hashtable(); switch (args[0].ToLower()) { case "-i" : myAssemblyInstaller.Install(mySavedState); myAssemblyInstaller.Commit(mySavedState); myAssemblyInstaller.Dispose(); return ; case "-u" : myAssemblyInstaller.CommandLine = new string [1] { "/u" }; myAssemblyInstaller.Uninstall( null ); myAssemblyInstaller.Dispose(); return ; default : System.Console.WriteLine( "------参数说明------" ); System.Console.WriteLine( "- i 安装服务!" ); System.Console.WriteLine( "- u 卸载服务!" ); System.Console.ReadKey(); return ; } } #endregion ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new AdbGuardWinntService() }; ServiceBase.Run(ServicesToRun); //new AdbGuardWinntService().OnStart(null); } |
b、Installutil.exe:安装程序(Installer)工具,该工具允许你在一个指定的程序集中执行安装程序组件来安装和卸载服务器资源。Installutil命令方式安装也很麻烦不推荐。
c、使用bat加传参数方法:
安装服务就是“服务程序.exe -i”,卸载就是“服务程序.exe -u”,启动服务是:“net start 服务名。”,停止服务是::“net stop 服务名。"将这些命令写成bat文件即可。如果是Win7需要以管理员权限运行。
补充(服务程序支持安装操作还需要执行的步骤):
设置服务运行时的系统账户类型,自动启动还是手动启动还是禁用等。
最后晒成果:
全部源码及可执行文件下载(需要.net 4 vs2010环境):
C#编写WINNT服务的更多相关文章
- C#编写WINNT服务,随便解决安卓开发遇到的5037被众多程序无节操占用的问题
需求分析: 最近重新开始学习安卓开发,好久不用的ADT集成开发环境频繁遇到不能在仿真机和真机上调试的问题,也就是本人另一篇博文描述的ADB(Android Debug Bridge)监控的5037被金 ...
- C#编写window服务,一步一步(1)
Window服务是啥,这里就不废话了,如何用在哪里用也不废话了,这里我这篇文章只是详述了我在vs2012中创建window服务的经过,希望对你有所帮助. 另外:我在编写服务过程中参考了 Profess ...
- C#编写windows服务
项目要求: 数据库用有一张表,存放待下载文件的地址,服务需要轮训表将未下载的文件下载下来. 表结构如下: 过程: VS--文件-->新建项目-->windows-->windows服 ...
- 在python中编写socket服务端模块(二):使用poll或epoll
在linux上编写socket服务端程序一般可以用select.poll.epoll三种方式,本文主要介绍使用poll和epoll编写socket服务端模块. 使用poll方式的服务器端程序代码: i ...
- 编写WCF服务时右击配置文件无“Edit WCF Configuration”(编辑 WCF 配置)远程的解决办法
原文:编写WCF服务时右击配置文件无“Edit WCF Configuration”远程的解决办法 今天在看<WCF揭秘>书中看到作者提出可以在一个WCF Host应用程序的App.Con ...
- 使用C语言编写windows服务一般框架
原文:使用C语言编写windows服务一般框架 编写windows服务和编写windows应用程序一样,有一些回调函数必须填写且向windows 服务管理器(service manager)进行注册, ...
- C#编写Windows 服务
C#编写Windows 服务 Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时 ...
- python实现编写windows服务
使用python编写windows服务 最近测试服务器上经常发生磁盘空间不足,每次手动清除比较麻烦,所以写个windows服务定时清理下.中间也遇到过几个坑,一起记录下来. 1.python实现win ...
- 如何在Ruby中编写微服务?
[编者按]本文作者为 Pierpaolo Frasa,文章通过详细的案例,介绍了在Ruby中编写微服务时所需注意的方方面面.系国内 ITOM 管理平台 OneAPM 编译呈现. 最近,大家都认为应当采 ...
随机推荐
- 【转】UIAutomator定位Android控件的方法实践和建议(Appium姊妹篇)
原文地址:http://blog.csdn.net/zhubaitian/article/details/39777951 在本人之前的一篇文章<<Appium基于安卓的各种FindEle ...
- zsh的安装与配置
参考: http://cnbin.github.io/blog/2015/06/01/mac-zsh-an-zhuang-he-shi-yong/ http://www.cnblogs.com/ios ...
- 《Windows游戏编程技巧大师》就DirectDraw而创建DirectDraw知识笔记
1.DirectDraw 这可能是Directx中最重要的技术,由于它是2D图形赖以实现的渠道.也是Direct3D构建于其上的帧缓冲层. 2.DirectDraw是由非常多借口组成的.共同拥有5个接 ...
- java 集装箱 arraylist 用法
1. ArrayList概述: ArrayList 是一个数组队列.相当于 动态数组. 与Java中的数组相比.它的容量能动态增长.它继承于AbstractList.实现了List, RandomAc ...
- asp.net 操作XML
using System.Xml; using System.Data; using System.IO; string xmlpath = HttpRuntime.AppDomainAppPat ...
- Kafka的常用管理命令
1. 查看kafka都有那些topic a. list/usr/hdp/current/kafka-broker/bin/kafka-topics.sh --list --zookeeper test ...
- thinkphp 支付宝错误 Class 'Think' not found
Class 'Think' not found D:\www\DonatePlatform\ThinkPHP\Extend\Vendor\alipay\lib\alipay_submit.class. ...
- struts2注解redirect传递参数解决方案时,中国的垃圾问题
struts2注解redirect传递参数解决方案时,中国的垃圾问题 试过很多方法 tomcat 编码 .字符串转换 .URLEncoder .. 但是,没有解决方案,然后仔细阅读 stru ...
- PHP 8: PHP的运算符
原文:PHP 8: PHP的运算符 本章将介绍PHP的运算符.运算符这个问题在每种语言里都有,因为我们已经熟悉了编程语言里的一种或是多种,所以只需要了解一下就行了.概括一下吧.PHP运算符有很多种,看 ...
- ACM-光滑最小生成树project——hdu1863
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...