<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hacklog &#187; 服务器</title>
	<atom:link href="http://ihacklog.com/cat/server/feed" rel="self" type="application/rss+xml" />
	<link>http://ihacklog.com</link>
	<description>荒野无灯weblog</description>
	<lastBuildDate>Sat, 04 Feb 2012 12:30:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PostgreSQL</title>
		<link>http://ihacklog.com/server/postgresql/postgresql.html</link>
		<comments>http://ihacklog.com/server/postgresql/postgresql.html#comments</comments>
		<pubDate>Mon, 26 Dec 2011 11:29:09 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[PostgreSQL]]></category>

		<guid isPermaLink="false">http://ihacklog.com/?p=5240</guid>
		<description><![CDATA[phpPgAdmin 以下介绍摘自wikipedia http://zh.wikipedia.org/wiki/PostgreSQL PostgreSQL是自由的对象-关系数据库服务器（数据库管理系统），在灵活的BSD-风格许可证下发行。它在其他开放源代码数据库系统（比如MySQL和Firebird），和专有系统比如Oracle、Sybase、IBM的DB2和Microsoft SQL Server之外，为用户又提供了一种选择。 PostgreSQL不寻常的名字导致一些读者停下来尝试拼读它，特别是那些把S...]]></description>
			<content:encoded><![CDATA[<p><a href="http://phppgadmin.sourceforge.net/doku.php" target="_blank">phpPgAdmin</a></p>
<p>以下介绍摘自wikipedia <a href="http://zh.wikipedia.org/wiki/PostgreSQL" target="_blank">http://zh.wikipedia.org/wiki/PostgreSQL</a><br />
<a href="http://zh.wikipedia.org/wiki/PostgreSQL" target="_blank">PostgreSQL</a>是自由的对象-关系数据库服务器（数据库管理系统），在灵活的BSD-风格许可证下发行。它在其他开放源代码数据库系统（比如MySQL和Firebird），和专有系统比如Oracle、Sybase、IBM的DB2和Microsoft SQL Server之外，为用户又提供了一种选择。<br />
PostgreSQL不寻常的名字导致一些读者停下来尝试拼读它，特别是那些把SQL拼读为”sequel”的人。PostgreSQL开发者把它拼读为”post-gress-Q-L”。（Audio sample，5.6k MP3）。它也经常被简略念为”postgres”。</p>
<h2>描述</h2>
<p>如果很粗略地观察PostgreSQL，会觉得这个数据库系统和其它数据库很类似。因为PostgreSQL使用SQL语言来在执行资料的查询。这些资料通过连外键联系在一起，以一系列表格的形式存在。PostgreSQL相对于竞争者的主要优势，主要的特征为可编程性：对于使用数据库资料的实际应用，PostgreSQL让开发与使用的工作，变得更加容易。<br />
SQL数据在”平面表格”中存储简单的数据类型，需要用户使用查询把有关的信息收集在一起。这与应用和用户利用数据自身的方式相对立：典型的使用带有丰富数据类型的高级语言，在其中所有有关的数据作为它自己的一个完整单元来操作。典型的称呼为记录或对象（依据各自语言）。<br />
转换来自SQL世界的信息到面向对象编程世界体现得很困难，因为两者有非常不同的数据组织的模型。工业界把这个问题称为阻抗不匹配：从一个模型映射到另一个要花费项目开发者40%的时间。一些映射解决方案，典型的称为对象-关系映射，致力于这个问题，但是它们花费很多并有自身的问题，导致糟糕的性能或强制所有的数据访问通过映射所支持的一种语言来进行。<br />
PostgreSQL可以直接在数据库中解决很多这种问题。PostgreSQL允许用户定义基于正规的SQL类型的新类型，允许数据库自身理解复杂数据。例如，你可以定义一个address来组合一些事物如街道编号、城市和国度的字符串。从这一点上你可以轻易的创建把保存地址的所需要的所有字段包含在一个单一行列中的表。<br />
PostgreSQL还允许类型包括继承，这是在面向对象编程中的主要概念。例如，你可以定义post_code类型，并接着基于它创建us_zip_code和canadian_postal_code。在数据库中的address就可以采用us_address或者canadian_address形式，而特定的规则可以在各自情况下验证数据。在PostgreSQL的早期版本中，实现新类型需要写C扩展并把它们编译到数据库服务器中；在版本7.4中，通过CREATE DOMAIN创建和使用定制类型变得很容易了。<br />
数据库自身的编程可以从使用函数上获得巨大的利益。多数SQL系统允许用户写存储过程，它是其他SQL语句可以调用的一块SQL代码。但是SQL自身仍旧不适合作为编程语言，而且SQL用户在构造复杂逻辑时要经历巨大的困难。更糟糕的是，SQL自身不支持很多的编程语言中最基本的操作，比如分支和循环。每个厂商都转而写它们自己对SQL语言的扩展来增加这些特征，而这种扩展不是必须跨越数据库平台操作。<br />
在PostgreSQL中程序员可以用一组可观的支持语言中任何一种来写这种逻辑。<br />
类似于Oracle的过程语言PL/SQL的叫做PL/PgSQL的内置语言，在处理查询密集的过程时提供了独特的优势。<br />
流行脚本语言比如Perl，Python，Tcl，和Ruby的包装器，允许利用它们在字符串处理和连接到广阔的外部函数库的力量。<br />
需要把复杂逻辑编译到机器代码所能提供的高性能的过程可以利用C或C++。<br />
在更加深奥的方面，R统计语言的处理器允许数据库查询利用它的一组丰富的统计函数。<br />
程序员可以把代码作为函数插入服务器中，它是使代码类似于存储过程的一个小包装器。以这种方式SQL代码可以调用（比如）C代码或反之。<br />
性能增进，因为数据库引擎在一个时间一个地方调用所有的逻辑，减少了在客户和服务器之间的来回往返的次数。<br />
可靠性增进，因为数据验证代码集中到一个地方，就在服务器上，而不用依赖在多个客户应用中的同步逻辑，它们甚至可能以多种编程语言写成。<br />
通过向服务器增加有用的抽象，客户代码可以变得更短小和简单。<br />
这些优势合起来可以证实PostgreSQL从编程角度是最高级的数据库系统。使用PostgreSQL可以显著的减少很多项目的整体编程时间，这种优势随着项目复杂而增长。</p>
<h2>特征</h2>
<h3>函数</h3>
<p>通过函数，可以在数据库服务器端执行指令程序。仅管这样的指令程序可以使用基本的SQL语句写成，但是由于其缺乏流程控制等功能，所以在PostgreSQL中引入了使用其它程序语言编写函数的能力，包括：<br />
一个内置的名为PL/pgSQL的过程语言，类似于Oracle的PL/SQL；<br />
包括PL/Perl，plPHP，PL/Python，PL/Ruby，PL/sh，PL/Tcl与PL/Scheme在内的脚本语言；<br />
编译语言：C，C++，或Java（通过PL/Java）。<br />
R统计语言（PL/R）。<br />
以上部分的语言，甚至可以在触发器内执行。PostgreSQL支持行返回函数：它们的输出是一系列行类型数据的集合，可以在查询中当作表来使用。函数也可以被定义成以创建者或者调用者的身份运行。在某些场合，或者其他的数据库产品中，函数也会被称为“存储过程”，但技术上这两者并未有太大分别。</p>
<h3>索引</h3>
<p>在PostgreSQL中，用户可以自定义索引方法，或使用内置的B-tree，哈希表与GiST索引。PosrgreSQL的索引功能同时也具有以下功能：<br />
反向索引检索：无须额外的索引就能实现类似ORDER BY field DESC的操作。<br />
表达式索引：可以创建基于表达式值而非数值或列的索引。<br />
部分索引：仅索引表的部分，可以通过在CREATE INDEX语句口添加WHERE从句以创建更小的索引。<br />
位图索引扫描：从8.1版开始支持此功能。该功能将读取多个索引，生成表示它们之间符合查询标准的多元组交集的位图。这样解决了混合索引的问题。在一个具有20列的表中，理论上能创建20! 个索引，在实际应用中已不现实。使用位图索引扫描后，在每次查询时，它将能把约束条件中所涉及列各自的索引进行任意的排列组合。</p>
<h3>触发器</h3>
<p>触发器是由SQL语句查询所触发的事件。如：一个INSERT语句可能触发一个检查数据完整性的触发器。触发器通常由INSERT或UPDATE语句触发。<br />
在PostgreSQL中，可在数据表上设置触发器，但无法在视图中设置（对视图的UPDATE或者INSERT操作可以使用规则（RULE）定义）。多个触发器可依据字母顺序依次执行。此外，除了使用内嵌的PL/PgSQL语言之外，触发器的函数也可以用PL/Perl，PL/Python等语言编写。<br />
[编辑]多版本并发控制<br />
PostgreSQL使用多版本并发控制（MVCC，Multiversion concurrency control）系统进行并发控制，该系统向每个用户提供了一个数据库的“快照”，用户在事务内所作的每个修改，对于其他的用户都不可见，直到该事务成功提交。这从很大程度上减少了对读取锁的依赖，同时保证了数据库高效地符合ACID原则。</p>
<h3>规则</h3>
<p>规则（RULE）允许一个查询能被重写，通常被用作实现可更新的视图。<br />
[编辑]数据类型<br />
PostgreSQL内置丰富的数据类型，包括：<br />
任意精度的数值<br />
无限制长度文本<br />
几何图元<br />
IP地址与IPv6地址<br />
无类域间路由地址块，MAC地址<br />
数组<br />
此外，用户可以创建自定义数据类型，通常通过PostgreSQL的GiST机制，它们也能被很好得索引，比如PostGIS地理信息系统的数据类型。</p>
<h3>用户定义对象</h3>
<p>用户可以为数据库内几乎所有的对象定义新的类型，包括：<br />
索引<br />
操作符（可重载现有操作符。）<br />
聚合函数<br />
数据域<br />
数据类型转换<br />
会话（编码转换）</p>
<h3>继承</h3>
<p>数据表的结构及属性可从一个“父”表中继承，数据将在两者间共享。对子表中数据的插入或者删除也将在父表中体现，同样，对父表作出的修改，比如添加列等操作也会导致子表产生相应改动。该功能尚未完全实现，实际上，表的约束尚不能继承。比如，在一张外联参考了父表id字段的表中，插入一条具有子表中某条记录id数据的记录会导致失败，因为PostgreSQL在对父表的外键约束检查中不会检查子表的内容。<br />
[编辑]其他功能<br />
关系完整性约束：包括外联键，列约束与行检查<br />
视图：暂时没有实现可更新视图，但可使用规则系统实现相同功能<br />
Full, inner, outer (left/right) 联合查询<br />
子查询<br />
事务处理<br />
支持大部分SQL:2003标准的主要特征[2] 以及不支持部分未来版本的支持情况，可至已有版本手册中查询。<br />
SSL加密连接<br />
二进制／文本 大对象的存储<br />
在线备份<br />
数据域<br />
表分区<br />
表空间<br />
保存点<br />
时刻点恢复<br />
两段式提交<br />
TOAST（The Oversized-Attribute Storage Technique，超大属性存储技术）用以透明地将超大表属性（如大MIME附件或XML消息）压缩后存储至独立的区域<br />
正则表达式[3]</p>
<h2>数据库管理及开发工具</h2>
<p>Navicat导航猫 for PostgreSQL 是一套专为 PostgreSQL 设计的强大数据库管理及开发工具。它可以用于任何版本的 PostgreSQL 数据库，并支持大部份 PostgreSQL的功能，包括触发器、索引、查看等。<br />
phpPgAdmin 基于php语言写的用于管理PostgreSQL数据库的程序<br />
PgAdmin 另外一个用于管理PostgreSQL数据库的软件</p>
<h2>扩展</h2>
<p>地理数据对象：PostGIS GPL<br />
全文检索：通过Tsearch2或OpenFTS， 将在8.3版本中内嵌Tsearch2。GPL<br />
多种异步主／从复制方案，包括Slony-I（BSD授权），Mammoth Replicator<br />
XML/XSLT支持contrib软件包中的XPath扩展GPL</p>
<hr />
archlinux下安装pgsql扩展：</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">pacman <span style="color: #660033;">-S</span> php-pgsql</div></td></tr></tbody></table></div>
<p>数据库安装：</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">pacman <span style="color: #660033;">-S</span> <span style="color: #660033;">--needed</span> postgresql phppgadmin postgresql-docs</div></td></tr></tbody></table></div>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=5240";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=5240" title="Permanent Link to PostgreSQL" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=5240</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2011年07月11日  //  <a href="http://ihacklog.com/software/development-tool/netbeans-filetemplates.html" title="netbeans自定义文件模板(备忘)">netbeans自定义文件模板(备忘)</a> (2)</li><li>2011年12月6日  //  <a href="http://ihacklog.com/linux/archlinux/solution-to-opera-11-52-crash-problem-under-archlinux.html" title="opera 11.52 在archlinux 下crash问题解决办法">opera 11.52 在archlinux 下crash问题解决办法</a> (7)</li><li>2008年12月20日  //  <a href="http://ihacklog.com/software/tips/go-die-sinchv.html" title="去死吧，西橙传媒客户端！">去死吧，西橙传媒客户端！</a> (0)</li><li>2009年04月9日  //  <a href="http://ihacklog.com/php/skills/phpbufenchangjianwentizongjie.html" title="Php部分常见问题总结">Php部分常见问题总结</a> (0)</li><li>2009年10月28日  //  <a href="http://ihacklog.com/php/wordpress/wpskills/wordpress-comment-author-link-open-in-new-window.html" title="让wordpress评论者链接在新窗口中打开">让wordpress评论者链接在新窗口中打开</a> (16)</li><li>2010年06月16日  //  <a href="http://ihacklog.com/software/cool_software_share/real-desktop.html" title="real desktop——让桌面名副其实">real desktop——让桌面名副其实</a> (3)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/server/postgresql/postgresql.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>windows上安装memcached、apc</title>
		<link>http://ihacklog.com/server/install-memcached-for-php-5-3-on-windows.html</link>
		<comments>http://ihacklog.com/server/install-memcached-for-php-5-3-on-windows.html#comments</comments>
		<pubDate>Sat, 10 Dec 2011 12:41:09 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[服务器]]></category>

		<guid isPermaLink="false">http://ihacklog.com/?p=5079</guid>
		<description><![CDATA[在Linux下就简单了： 12pear install apc pear install memcached 蛋疼的win按官方的step不行。 先去这里 下载编译好的win32程序 win32 binary: memcached-1.2.6-win32-bin.zip ： http://code.jellycan.com/memcached/ 安装为win服务 1memcached.exe &#160;-d install 启动服务 1memcached -d start 或 1net start &#...]]></description>
			<content:encoded><![CDATA[<p>在Linux下就简单了：</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">pear <span style="color: #c20cb9; font-weight: bold;">install</span> apc<br />
pear <span style="color: #c20cb9; font-weight: bold;">install</span> memcached</div></td></tr></tbody></table></div>
<p>蛋疼的win按官方的step不行。<br />
先去<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fcode.jellycan.com%2Fmemcached%2F" target="_blank">这里</a></p>
<h3>下载编译好的win32程序</h3>
<p>win32 binary: memcached-1.2.6-win32-bin.zip ：<br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fcode.jellycan.com%2Fmemcached%2F" target="_blank">http://code.jellycan.com/memcached/</a></p>
<h3>安装为win服务</h3>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">memcached.exe &nbsp;<span style="color: #660033;">-d</span> <span style="color: #c20cb9; font-weight: bold;">install</span></div></td></tr></tbody></table></div>
<h3>启动服务</h3>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">memcached <span style="color: #660033;">-d</span> start</div></td></tr></tbody></table></div>
<p>或</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">net start <span style="color: #ff0000;">&quot;memcached Server&quot;</span></div></td></tr></tbody></table></div>
<p>现在memcached已经启动，且监听11211端口。</p>
<h3>调整内存限制</h3>
<p>默认是64Mb 内存限制，可以把它调整为更大：<br />
查找</p>
<div class="codecolorer-container ini default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="ini codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/memcached &nbsp;Server</div></td></tr></tbody></table></div>
<p>以 512 Mb 内存为例，将ImagePath修改为：</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff0000;">&quot;C:/memcached/memcached.exe&quot;</span> <span style="color: #660033;">-d</span> runservice <span style="color: #660033;">-m</span> <span style="color: #000000;">512</span></div></td></tr></tbody></table></div>
<p>如果只是开发调试用，可以写一个启动脚本start_memcached.bat：</p>
<div class="codecolorer-container winbatch default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="winbatch codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">@</span>echo <span style="color: #0080FF; font-weight: bold;">off</span><br />
memcached <span style="color: #66cc66;">-</span>d start<br />
wmic process get description, executablepath <span style="color: #66cc66;">|</span> findstr memcached.exe<br />
<span style="color: #0000FF;">pause</span></div></td></tr></tbody></table></div>
<h3>启用php_memcache 扩展</h3>
<p>php_memcache.dll可以从http://downloads.php.net/pierre/　下载。<br />
php_apc.dll也是一样。</p>
<blockquote><p>http://downloads.php.net/pierre/php_apc-20110109-5.3-vc9-x86.zip</p>
<p>http://downloads.php.net/pierre/php_memcache-2.2.6-5.3-vc9-x86.zip</p></blockquote>
<p>修改php.ini ,添加：</p>
<div class="codecolorer-container ini default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="ini codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000099;">extension</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">php_memcache.dll</span></div></td></tr></tbody></table></div>
<p>php相关文档：<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fphp.net%2Fmanual%2Fzh%2Fbook.memcache.php" target="_blank">http://php.net/manual/zh/book.memcache.php</a><br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fphp.net%2Fmanual%2Fzh%2Fbook.apc.php" target="_blank">http://php.net/manual/zh/book.apc.php</a></p>
<h3>测试</h3>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$memcache</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Memcache<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// instantiating memcache extension class</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$memcache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">11211</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// try 127.0.0.1 instead of localhost</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// if it is not working </span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Server's version: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$memcache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getVersion</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// we will create an array which will be stored in cache serialized</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$testArray</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'horse'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cow'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pig'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$tmp</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <a href="http://www.php.net/serialize"><span style="color: #990000;">serialize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$testArray</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$memcache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;key&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Data from the cache:&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/unserialize"><span style="color: #990000;">unserialize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$memcache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;key&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<h3>相关文档</h3>
<p><a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fmemcached.org%2F" target="_blank">http://memcached.org/</a><br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fdownloads.php.net%2Fpierre%2F" target="_blank">http://downloads.php.net/pierre/</a><br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.codeforest.net%2Fhow-to-install-memcached-on-windows-machine" target="_blank">http://www.codeforest.net/how-to-install-memcached-on-windows-machine</a><br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fshikii.net%2Fblog%2Finstalling-memcached-for-php-5-3-on-windows-7%2F" target="_blank">http://shikii.net/blog/installing-memcached-for-php-5-3-on-windows-7/</a></p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=5079";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=5079" title="Permanent Link to windows上安装memcached、apc" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=5079</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2010年03月3日  //  <a href="http://ihacklog.com/php/discuz/discuz-code-of-music-autoplay.html" title="discuz论坛中发帖之音乐自动播放方法">discuz论坛中发帖之音乐自动播放方法</a> (3)</li><li>2008年12月16日  //  <a href="http://ihacklog.com/php/wordpress/wpskills/wordpress-publish-weblog-via-windows-live-writer.html" title="wordpress通过windows live writer发布日志设置方法">wordpress通过windows live writer发布日志设置方法</a> (1)</li><li>2011年10月6日  //  <a href="http://ihacklog.com/linux/linux-copy-with-progress-bar.html" title="linux 带进度条的本地复制 ">linux 带进度条的本地复制 </a> (0)</li><li>2010年05月22日  //  <a href="http://ihacklog.com/php/wordpress/theme/wp-theme-the-web-news-theme.html" title="WP主题推荐：The Web News Theme">WP主题推荐：The Web News Theme</a> (9)</li><li>2011年07月13日  //  <a href="http://ihacklog.com/php/wordpress/upgrade/upgrade-to-wordpress-3-2-1.html" title="升级到WordPress 3.2.1 ">升级到WordPress 3.2.1 </a> (13)</li><li>2009年11月14日  //  <a href="http://ihacklog.com/software/cool_software_share/free-rising-antivirus-software.html" title="免费瑞星杀毒软件">免费瑞星杀毒软件</a> (6)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/server/install-memcached-for-php-5-3-on-windows.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>linux pptp链路vpn简单配置</title>
		<link>http://ihacklog.com/server/ubuntu-server/how-to-setup-linux-pptp-vpn-server.html</link>
		<comments>http://ihacklog.com/server/ubuntu-server/how-to-setup-linux-pptp-vpn-server.html#comments</comments>
		<pubDate>Mon, 14 Nov 2011 02:53:41 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[Ubuntu server]]></category>
		<category><![CDATA[pptp链路]]></category>
		<category><![CDATA[vpn]]></category>

		<guid isPermaLink="false">http://ihacklog.com/?p=4984</guid>
		<description><![CDATA[服务器：ubuntu 11.10 1sudo apt-get install pptpd 修改/etc/pptpd.conf 123456789option /etc/ppp/pptpd-options noipparam logwtmp localip 192.168.119.254 remoteip 192.168.119.2-100 netmask 255.255.255.0 再修改/etc/ppp/pptpd-options 123456789101112131415161718192021222...]]></description>
			<content:encoded><![CDATA[<p>服务器：ubuntu 11.10</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get install</span> pptpd</div></td></tr></tbody></table></div>
<p>修改/etc/pptpd.conf</p>
<div class="codecolorer-container ini default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="ini codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">option /etc/ppp/pptpd-options<br />
<br />
noipparam<br />
<br />
logwtmp<br />
<br />
localip 192.168.119.254<br />
remoteip 192.168.119.2-<span style="">100</span><br />
netmask 255.255.255.0</div></td></tr></tbody></table></div>
<p>再修改/etc/ppp/pptpd-options</p>
<div class="codecolorer-container ini default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="ini codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">###############################################################################<br />
# $Id$<br />
#<br />
# 这是一个样例Poptop PPP 选项文件，它位于 /etc/ppp/pptpd-options<br />
# 这个文件的选项是用于当有客户端连接时被ppp使用的.<br />
# 这个文件是通过/etc/pptpd.conf文件中的option关键字指向的.<br />
# 这里的改变将对下一个连接生效. &nbsp;See <span style="color: #933;">&quot;man pppd&quot;</span>.<br />
###############################################################################<br />
name pptpd<br />
<br />
refuse-pap<br />
refuse-chap<br />
refuse-mschap<br />
require-mschap-v2<br />
require-mppe-<span style="">128</span><br />
<br />
ms-dns 59.51.78.211<br />
ms-dns 8.8.8.8<br />
<br />
proxyarp<br />
nodefaultroute<br />
lock<br />
#nobsdcomp</div></td></tr></tbody></table></div>
<p>再修改 /etc/ppp/options</p>
<div class="codecolorer-container ini default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="ini codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">name pptpd <br />
auth <br />
require-chap <br />
-chap <br />
-mschap <br />
+mschap-v2 <br />
require-mppe <br />
lcp-echo-failure <span style="">30</span> <br />
lcp-echo-interval <span style="">5</span> <br />
ipcp-accept-local <br />
ipcp-accept-remote <br />
multilink <br />
noipx<br />
proxyarp <br />
logfd <span style="">2</span> <br />
logfile /var/log/pptpd.log <br />
debug <br />
dump <br />
lock</div></td></tr></tbody></table></div>
<p>最后，添加用户名和密码</p>
<div class="codecolorer-container ini default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="ini codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"># Secrets for authentication using CHAP<br />
# client &nbsp; &nbsp; &nbsp; &nbsp;server &nbsp;secret &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IP addresses<br />
hacklog &nbsp; &nbsp; &nbsp;pptpd &nbsp; <span style="color: #933;">&quot;mypassword&quot;</span> &nbsp; &nbsp; &nbsp;*</div></td></tr></tbody></table></div>
<p>用*表示不限制登录ip,可以通过指定ip地址来限制登录机器的Ip.防止账号被盗用。</p>
<p>ok,差不多了，先启动服务器看看：</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>pptpd start</div></td></tr></tbody></table></div>
<p>连接成功了，但是现在还不能通过此vpn通道上网。<br />
因为没有做NAT呢。</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>sysctl.d<br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">vim</span> <span style="color: #000000;">10</span>-network-security.conf</div></td></tr></tbody></table></div>
<p>在net.ipv4.tcp_syncookies=1后面加上：</p>
<div class="codecolorer-container ini default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="ini codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">net.ipv4.ip_forward<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">1</span></div></td></tr></tbody></table></div>
<p>即时生效的方法（要root权限）：</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>net<span style="color: #000000; font-weight: bold;">/</span>ipv4<span style="color: #000000; font-weight: bold;">/</span>ip_forward<br />
<br />
<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>iptables <span style="color: #660033;">-F</span> FORWARD <br />
<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>iptables <span style="color: #660033;">-P</span> FORWARD ACCEPT <br />
<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>iptables <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-A</span> POSTROUTING <span style="color: #660033;">-j</span> MASQUERADE <br />
<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>iptables <span style="color: #660033;">-A</span> FORWARD <span style="color: #660033;">-p</span> udp <span style="color: #660033;">-s</span> 192.168.119.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">24</span> <span style="color: #660033;">--dport</span> <span style="color: #000000;">53</span> <span style="color: #660033;">-j</span> ACCEPT <br />
<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>iptables <span style="color: #660033;">-A</span> FORWARD <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">-s</span> 192.168.119.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">24</span> <span style="color: #660033;">--dport</span> <span style="color: #000000;">1723</span> <span style="color: #660033;">-j</span> ACCEPT <br />
<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>iptables <span style="color: #660033;">-A</span> FORWARD <span style="color: #660033;">-p</span> gre <span style="color: #660033;">-s</span> 192.168.119.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">24</span> <span style="color: #660033;">-j</span> ACCEPT <br />
<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>iptables <span style="color: #660033;">-A</span> FORWARD <span style="color: #660033;">-m</span> state <span style="color: #660033;">--state</span> ESTABLISHED,RELATED <span style="color: #660033;">-j</span> ACCEPT</div></td></tr></tbody></table></div>
<p>1723是vpn server的端口，53是dns的tcp端口。<br />
这几条iptables命令在重启后又会失效的，可用iptables-save来解决。<br />
先保存一下。</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;"># </span>iptables-save <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>dsl.fw</div></td></tr></tbody></table></div>
<p>再编辑/etc/rc.local,加上：</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>iptables-restore <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>dsl.fw</div></td></tr></tbody></table></div>
<p>上面这个方法是对所有Linux通用的。<br />
如果是用的debian/Ubuntu 系统：<br />
把下面这行附加到/etc/network/interfaces文件中eth0 section的后面:</p>
<div class="codecolorer-container ini default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="ini codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">post-up iptables-restore</div></td></tr></tbody></table></div>
<p>然后保存文件，重启系统。</p>
<hr />
<h3>一些选项的解释</h3>
<h3>pptpd.confi配置文件参数详解</h3>
<p>option  filename  </p>
<p>指定一个选项文件，里面的内容作为pptpd进程启动时的命令参数。与执行pptpd命令时使用-o选项指定参数效果是一样的。</p>
<p>stimeout seconds</p>
<p>派生pptpctrl进程处理客户端连接以前，等客户端pptp包的时间，默认为10秒。这个选项主要用于防止Dos攻击。</p>
<p>debug</p>
<p>启用调试模式</p>
<p>bcrelay  internal-interface</p>
<p>是否启用广播包中继功能。如果启用将把从internal-interface接口收到的广播包转发给客户端。</p>
<p>connections   n</p>
<p>限制客户端连接数，默认100。</p>
<p>delegate</p>
<p>默认情况下，该选项不存在。此时由pptpd进程管理ip地址分配，它将把下一个可分的ip分配给客户端。如果存在该选项，则pptpd进程不负责ip地址的分配，由客户端对应的pppd进程采用radius或chap-secrets方式进行IP分配。</p>
<p>localip   ip-specification</p>
<p>在ppp连接隧道的本地端使用的IP地址。如果只指定一个，则所有的客户端对就的服务端都是这个地址。否则，每个客户端都必须要对应不同的服务器端地址。服务端地址用完后，客户端的连接将被拒绝。如果使用了delegate选项，则该选项失效。</p>
<p>remoteip  ip-specification</p>
<p>指定分配给远程客户端的IP地址。每个客户端都必须分配到一个IP地址，如果使用了delegate选项，则该选项失效。</p>
<p>noipparam</p>
<p>默认情况下，客户端原始的IP地址是传递给ip-up脚本。如果存在该选项，将不传递。</p>
<p>listen  ip-address</p>
<p>指定本地网络接口的IP地址，pptpd进程将只监听这个网络接口的pptpd连接。默认监听所有本地接口。</p>
<p>pidfile  pid-file</p>
<p>指定进程pid文件的位置和文件名</p>
<p>speed   speed</p>
<p>指定PPTP连接的速度，默认是115200bps。在linux系统中，该选项无效。</p>
<hr />
<h3>options.pptpd(在ubuntu里为 /etc/ppp/options)配置参数详解</h3>
<p>auth  #需要使用/etc/ppp/chap-secrets文件来验证<br />
lock  #锁定PTY设备文件<br />
debug<br />
Proxyarp  #启动ARP代理，如果分配给客户端的IP地址与内网网卡在一个子网就需要启用ARP代理。<br />
name pptpd  #VPN服务器的名字<br />
multilink<br />
refuse-pap  #拒绝pap身份验证<br />
refuse-chap  #拒绝chap身份验证<br />
refuse-mschap  #拒绝mschap身份验证<br />
require-mschap-v2  #注意在采用mschap-v2身份验证方式时可以同时使用MPPE进行加密<br />
require-mppe-128  #使用 128-bit MPPE 加密<br />
ms-wins 192.168.1.2  #在网络邻居中看到的机器的IP填写到这里<br />
ms-dns 192.168.1.2  #DNS服务器地址</p>
<p>ms-dns  202.102.224.68  #第二个DNS服务器地址<br />
dump<br />
logfile /var/log/pptpd.log  #日志存放的路径</p>
<p>参考：<br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.num123.com%2Fpost%2F88" target="_blank">LINUX系统下架设基于PPTP协议的VPN服务器</a><br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Ffanqiang.chinaunix.net%2Fapp%2Fvpn%2F2005-04-06%2F3112.shtml" target="_blank">Linux下架设VPN服务器指南</a><br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.cyberciti.biz%2Ffaq%2Fhow-do-i-save-iptables-rules-or-settings%2F" target="_blank">How Do I Save Iptables Rules or Settings?</a></p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4984";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4984" title="Permanent Link to linux pptp链路vpn简单配置" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4984</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2010年03月11日  //  <a href="http://ihacklog.com/php/framework/codeigniter-for-rapid-php-application-development.html" title="PHP 敏捷开发框架 CodeIgniter">PHP 敏捷开发框架 CodeIgniter</a> (2)</li><li>2009年08月14日  //  <a href="http://ihacklog.com/linux/ubuntu-desktop/ubuntuqiyong3dxiaoguohouchuxiancuowu.html" title="UBUNTU启用3d效果后出现错误">UBUNTU启用3d效果后出现错误</a> (1)</li><li>2009年09月22日  //  <a href="http://ihacklog.com/php/wordpress/problems/drangerous-blog-had-been-injected.html" title="险，博客被挂马">险，博客被挂马</a> (11)</li><li>2011年10月16日  //  <a href="http://ihacklog.com/security_optimization/gzip-compression-detect.html" title="检测Gzip压缩——由一句评论引发的文章">检测Gzip压缩——由一句评论引发的文章</a> (8)</li><li>2012年01月7日  //  <a href="http://ihacklog.com/php/wordpress/plugins/three-wp-debuging-plugins.html" title="WP调试插件三款">WP调试插件三款</a> (0)</li><li>2011年12月4日  //  <a href="http://ihacklog.com/software/development-tool/emeditor-color-scheme-keywords-what-the-fuck.html" title="令人蛋疼的EmEditor配色问题">令人蛋疼的EmEditor配色问题</a> (3)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/server/ubuntu-server/how-to-setup-linux-pptp-vpn-server.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Apache反向代理服务器配置</title>
		<link>http://ihacklog.com/server/apache-server/apache-reverse-proxy-server-setup.html</link>
		<comments>http://ihacklog.com/server/apache-server/apache-reverse-proxy-server-setup.html#comments</comments>
		<pubDate>Mon, 03 Oct 2011 15:34:18 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[反向代理服务器]]></category>
		<category><![CDATA[配置]]></category>

		<guid isPermaLink="false">http://ihacklog.com/?p=4892</guid>
		<description><![CDATA[近日新上线了一个流媒体类网站，由于必需用到IIS，而另外几个网站则都是基本PHP+MySQL的，又不想分开装在不同的服务器上，于是，用Aapache给它做个反向代理。 流媒体站： 用IIS做服务器，监听8088端口 http://10.a.b.c:8088/webmedia/ 其它网站：用apache做http服务器，监听80端口 httpd.conf 启用代理模块 1234LoadModule proxy_module modules/mod_proxy.so #LoadModule proxy_ftp...]]></description>
			<content:encoded><![CDATA[<p>近日新上线了一个流媒体类网站，由于必需用到IIS，而另外几个网站则都是基本PHP+MySQL的，又不想分开装在不同的服务器上，于是，用Aapache给它做个反向代理。<br />
流媒体站： 用IIS做服务器，监听8088端口 http://10.a.b.c:8088/webmedia/<br />
其它网站：用apache做http服务器，监听80端口</p>
<p>httpd.conf 启用代理模块</p>
<div class="codecolorer-container apache default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #00007f;">LoadModule</span> proxy_module modules/mod_proxy.so<br />
<span style="color: #adadad; font-style: italic;">#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so</span><br />
<span style="color: #00007f;">LoadModule</span> proxy_http_module modules/mod_proxy_http.so<br />
<span style="color: #00007f;">LoadModule</span> vhost_alias_module modules/mod_vhost_alias.so</div></td></tr></tbody></table></div>
<p>做基于名称的虚拟主机，配置反向代理，proxy_mtv.conf内容如下：</p>
<div class="codecolorer-container apache default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:<span style="color: #ff0000;">80</span>&gt;<br />
&nbsp; &nbsp; <span style="color: #00007f;">ServerAdmin</span> webmaster@mtv.my-domain.com<br />
&nbsp; &nbsp; <span style="color: #00007f;">ServerName</span> mtv.my-domain.com<br />
&nbsp; &nbsp; <span style="color: #00007f;">ServerAlias</span> www.mtv.my-domain.com<br />
&nbsp; &nbsp; <span style="color: #00007f;">ProxyPass</span> / http://<span style="color: #ff0000;">10</span>.a.b.c:<span style="color: #ff0000;">8088</span>/webmedia/<br />
&nbsp; &nbsp; <span style="color: #00007f;">ProxyPassReverse</span> / http://<span style="color: #ff0000;">10</span>.a.b.c:<span style="color: #ff0000;">8088</span>/webmedia/ &nbsp; <br />
&nbsp; &nbsp; <span style="color: #00007f;">ErrorLog</span> <span style="color: #7f007f;">&quot;logs/mtv.my-domain.com-error.log&quot;</span><br />
&nbsp; &nbsp; <span style="color: #00007f;">CustomLog</span> <span style="color: #7f007f;">&quot;logs/mtv.my-domain.com-access.log&quot;</span> common<br />
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</div></td></tr></tbody></table></div>
<p>现在访问mtv.my-domain.com，即是访问到http://10.a.b.c:8088/webmedia/了，去掉了那个难看的URL尾巴。</p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4892";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4892" title="Permanent Link to Apache反向代理服务器配置" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4892</a>
            </p>
             </div><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2011年10月3日  //  <a href="http://ihacklog.com/server/nginx/nginx-reverse-proxy-setup.html" title="nginx反向代理配置">nginx反向代理配置</a> (1)</li><li>2011年05月7日  //  <a href="http://ihacklog.com/linux/archlinux/install-win7-and-archlinux-part-2.html" title="win7/archlinux双系统安装手记（二）">win7/archlinux双系统安装手记（二）</a> (0)</li><li>2010年07月19日  //  <a href="http://ihacklog.com/server/ubuntu-server/setup-bind9-dns-master-slave-server.html" title="bind9 DNS主从服务器配置">bind9 DNS主从服务器配置</a> (0)</li><li>2009年11月9日  //  <a href="http://ihacklog.com/server/apache-server/method-to-solve-cannot-load-c-php-php5apache2_2-dll-into-server-problem.html" title="Cannot load C:/php/php5apache2_2.dll into server错误的解决办法">Cannot load C:/php/php5apache2_2.dll into server错误的解决办法</a> (2)</li><li>2009年11月4日  //  <a href="http://ihacklog.com/server/using-bind9-setup-cache-and-local-dns-server.html" title="bind9架設域名服务器及配置">bind9架設域名服务器及配置</a> (2)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/server/apache-server/apache-reverse-proxy-server-setup.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>nginx反向代理配置</title>
		<link>http://ihacklog.com/server/nginx/nginx-reverse-proxy-setup.html</link>
		<comments>http://ihacklog.com/server/nginx/nginx-reverse-proxy-setup.html#comments</comments>
		<pubDate>Mon, 03 Oct 2011 15:22:09 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[反向代理]]></category>

		<guid isPermaLink="false">http://ihacklog.com/?p=4891</guid>
		<description><![CDATA[目前就稳定性来说，Apache是没得比的。因此，用nginx做反向代理比较合适。 这里是给http://172.30.170.8:8088/vod/做代理，反向代理服务器的名称为vod.xx.xxx.cn ,监听80端口。 Apache httpd服务器监听8088端口（我这里apache与反向代理服务器在同一服务器）。 全局配置参数做些调整： 12345678910111213141516171819202122232425262728293031323334353637383940hacklog@hy...]]></description>
			<content:encoded><![CDATA[<p>目前就稳定性来说，Apache是没得比的。因此，用nginx做反向代理比较合适。<br />
这里是给http://172.30.170.8:8088/vod/做代理，反向代理服务器的名称为vod.xx.xxx.cn ,监听80端口。<br />
Apache httpd服务器监听8088端口（我这里apache与反向代理服务器在同一服务器）。</p>
<p>全局配置参数做些调整：</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">hacklog<span style="color: #000000; font-weight: bold;">@</span>hywd:<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>nginx$ <span style="color: #c20cb9; font-weight: bold;">cat</span> nginx.conf <br />
user www-data;<br />
worker_processes &nbsp;<span style="color: #000000;">4</span>;<br />
<br />
error_log &nbsp;<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>error.log;<br />
pid &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>nginx.pid;<br />
<br />
events <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; use epoll;<br />
&nbsp; &nbsp; worker_connections &nbsp;<span style="color: #000000;">2048</span>;<br />
<span style="color: #666666; font-style: italic;"># multi_accept on;</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<br />
http <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; include &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>mime.types;<br />
<br />
&nbsp; &nbsp; access_log&nbsp; <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>access.log;<br />
<br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">gzip</span> &nbsp;on;<br />
&nbsp; &nbsp; gzip_disable <span style="color: #ff0000;">&quot;MSIE [1-6]\.(?!.*SV1)&quot;</span>;<br />
<br />
&nbsp; &nbsp; server_names_hash_bucket_size <span style="color: #000000;">256</span>;<br />
&nbsp; &nbsp; client_header_buffer_size 256k;<br />
&nbsp; &nbsp; large_client_header_buffers <span style="color: #000000;">4</span> 256k;<br />
<br />
<span style="color: #666666; font-style: italic;">#size limits</span><br />
&nbsp; &nbsp; client_max_body_size &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 50m;<br />
&nbsp; &nbsp; client_body_buffer_size &nbsp; &nbsp; &nbsp; &nbsp;256k;<br />
&nbsp; &nbsp; client_header_timeout &nbsp; &nbsp; 3m;<br />
&nbsp; &nbsp; client_body_timeout 3m;<br />
&nbsp; &nbsp; send_timeout &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3m;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#参数都有所调整.目的是解决代理过程中出现的一些502 499错误 &nbsp; &nbsp;</span><br />
&nbsp; &nbsp; sendfile on;<br />
&nbsp; &nbsp; tcp_nopush &nbsp; &nbsp; &nbsp; &nbsp; on;<br />
&nbsp; &nbsp; keepalive_timeout <span style="color: #000000;">120</span>; <span style="color: #666666; font-style: italic;">#参数加大,以解决做代理时502错误</span><br />
&nbsp; &nbsp; tcp_nodelay on;<br />
<br />
&nbsp; &nbsp; include <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>conf.d<span style="color: #000000; font-weight: bold;">/*</span>.conf;<br />
&nbsp; &nbsp; include <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>sites-enabled<span style="color: #000000; font-weight: bold;">/*</span>;<br />
&nbsp;<span style="color: #7a0874; font-weight: bold;">&#125;</span></div></td></tr></tbody></table></div>
<p>然后是反向代理配置了，有些参数必须调整，如client_max_body_size 不调整的话，通过web上传东西会有问题：</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">hacklog<span style="color: #000000; font-weight: bold;">@</span>hywd:<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>sites-available$ <span style="color: #7a0874; font-weight: bold;">pwd</span><br />
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>sites-available<br />
hacklog<span style="color: #000000; font-weight: bold;">@</span>hywd:<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>sites-available$ <span style="color: #c20cb9; font-weight: bold;">cat</span> proxy_local_apache <br />
<span style="color: #666666; font-style: italic;"># You may add here your</span><br />
<span style="color: #666666; font-style: italic;"># server {</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; ...</span><br />
<span style="color: #666666; font-style: italic;"># }</span><br />
<span style="color: #666666; font-style: italic;"># statements for each of your virtual hosts</span><br />
<br />
server <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; listen &nbsp; <span style="color: #000000;">80</span> default;<br />
&nbsp; &nbsp; server_name &nbsp;vod.xx.xxx.cn;<br />
<br />
&nbsp; &nbsp; access_log &nbsp;<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>vod.xx.xxx.cn.access.log;<br />
<br />
&nbsp; &nbsp; location ~ ^<span style="color: #000000; font-weight: bold;">/</span>status<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; stub_status on;<br />
&nbsp; &nbsp; &nbsp; &nbsp; access_log off;<br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<br />
&nbsp; &nbsp; location <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; proxy_redirect off ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header Host <span style="color: #007800;">$host</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header X-Real-IP <span style="color: #007800;">$remote_addr</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header REMOTE-HOST <span style="color: #007800;">$remote_addr</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header X-Forwarded-For <span style="color: #007800;">$proxy_add_x_forwarded_for</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; client_max_body_size 50m;<br />
&nbsp; &nbsp; &nbsp; &nbsp; client_body_buffer_size 256k;<br />
&nbsp; &nbsp; &nbsp; &nbsp; proxy_connect_timeout <span style="color: #000000;">30</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; proxy_send_timeout <span style="color: #000000;">30</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; proxy_read_timeout <span style="color: #000000;">60</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; proxy_buffer_size 256k;<br />
&nbsp; &nbsp; &nbsp; &nbsp; proxy_buffers <span style="color: #000000;">4</span> 256k;<br />
&nbsp; &nbsp; &nbsp; &nbsp; proxy_busy_buffers_size 256k;<br />
&nbsp; &nbsp; &nbsp; &nbsp; proxy_temp_file_write_size 256k;<br />
&nbsp; &nbsp; &nbsp; &nbsp; proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;<br />
&nbsp; &nbsp; &nbsp; &nbsp; proxy_max_temp_file_size 128m; <br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; proxy_pass &nbsp; &nbsp;http:<span style="color: #000000; font-weight: bold;">//</span>172.30.170.8:<span style="color: #000000;">8088</span><span style="color: #000000; font-weight: bold;">/</span>vod<span style="color: #000000; font-weight: bold;">/</span>;<br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<br />
&nbsp; &nbsp; location <span style="color: #000000; font-weight: bold;">/</span>doc <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; root &nbsp; <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share;<br />
&nbsp; &nbsp; &nbsp; &nbsp; autoindex on;<br />
&nbsp; &nbsp; &nbsp; &nbsp; allow 127.0.0.1;<br />
&nbsp; &nbsp; &nbsp; &nbsp; deny all;<br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<br />
<br />
<br />
<span style="color: #666666; font-style: italic;">#error_page &nbsp;404 &nbsp;/404.html;</span><br />
<br />
hacklog<span style="color: #000000; font-weight: bold;">@</span>hywd:<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>sites-available$</div></td></tr></tbody></table></div>
<h3>nginx日志切割脚本</h3>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">root<span style="color: #000000; font-weight: bold;">@</span>hywd:<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #666666; font-style: italic;"># cat /usr/local/sbin/cut_nginx_log.sh</span><br />
<span style="color: #666666; font-style: italic;">#!/bin/bash</span><br />
<span style="color: #666666; font-style: italic;"># This script run at 00:00</span><br />
<br />
<span style="color: #666666; font-style: italic;"># The Nginx logs path</span><br />
<span style="color: #007800;">logs_path</span>=<span style="color: #ff0000;">&quot;/var/log/nginx/&quot;</span><br />
<br />
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #800000;">${logs_path}</span>$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;yesterday&quot;</span> +<span style="color: #ff0000;">&quot;%Y&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;yesterday&quot;</span> +<span style="color: #ff0000;">&quot;%m&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><br />
<span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #800000;">${logs_path}</span>vod.xx.xxx.cn.access.log <span style="color: #800000;">${logs_path}</span>$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;yesterday&quot;</span> +<span style="color: #ff0000;">&quot;%Y&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;yesterday&quot;</span> +<span style="color: #ff0000;">&quot;%m&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>vod.xx.xxx.cn.access_$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;yesterday&quot;</span> +<span style="color: #ff0000;">&quot;%Y%m%d&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.log<br />
<span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-USR1</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>nginx.pid<span style="color: #000000; font-weight: bold;">`</span></div></td></tr></tbody></table></div>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">crontab <span style="color: #660033;">-e</span><br />
<span style="color: #666666; font-style: italic;"># m h &nbsp;dom mon dow &nbsp; command</span><br />
00 00 <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">bash</span> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>cut_nginx_log.sh</div></td></tr></tbody></table></div>
<p>参考文档：<br />
守住每一天 <a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fliuyu.blog.51cto.com%2F183345%2F166381" target="_blank">《nginx反向代理配置及优化》</a></p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4891";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4891" title="Permanent Link to nginx反向代理配置" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4891</a>
            </p>
             </div><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2011年10月3日  //  <a href="http://ihacklog.com/server/apache-server/apache-reverse-proxy-server-setup.html" title="Apache反向代理服务器配置">Apache反向代理服务器配置</a> (3)</li><li>2011年05月7日  //  <a href="http://ihacklog.com/linux/archlinux/install-win7-and-archlinux-part-2.html" title="win7/archlinux双系统安装手记（二）">win7/archlinux双系统安装手记（二）</a> (0)</li><li>2010年07月16日  //  <a href="http://ihacklog.com/server/ubuntu-server/lnmp-platform-setup.html" title="LNMP平台搭建与配置">LNMP平台搭建与配置</a> (0)</li><li>2010年05月19日  //  <a href="http://ihacklog.com/server/lnmp-vps-optimization.html" title="优化低内存VPS">优化低内存VPS</a> (10)</li><li>2010年05月16日  //  <a href="http://ihacklog.com/server/setup-lnmp-platform.html" title="折腾VPS:LNMP+VSFTPD的搭建">折腾VPS:LNMP+VSFTPD的搭建</a> (2)</li><li>2009年11月9日  //  <a href="http://ihacklog.com/server/apache-server/method-to-solve-cannot-load-c-php-php5apache2_2-dll-into-server-problem.html" title="Cannot load C:/php/php5apache2_2.dll into server错误的解决办法">Cannot load C:/php/php5apache2_2.dll into server错误的解决办法</a> (2)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/server/nginx/nginx-reverse-proxy-setup.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>netbeans 连接 mysql数据库com.mysql.jdbc.CommunicationsException: Communications link failure解决办法</title>
		<link>http://ihacklog.com/server/mysql/com-mysql-jdbc-communicationsexception-communications-link-failure-solution.html</link>
		<comments>http://ihacklog.com/server/mysql/com-mysql-jdbc-communicationsexception-communications-link-failure-solution.html#comments</comments>
		<pubDate>Thu, 21 Jul 2011 19:38:48 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[开发工具]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[NetBeans]]></category>
		<category><![CDATA[tcp wrapper]]></category>

		<guid isPermaLink="false">http://ihacklog.com/?p=4672</guid>
		<description><![CDATA[这个异常一般发生在linux下。 今天我在nb里面连接Mysql数据库时发生此错误了： com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the serve...]]></description>
			<content:encoded><![CDATA[<p>这个异常一般发生在linux下。<br />
今天我在nb里面连接Mysql数据库时发生此错误了：</p>
<blockquote><p>com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure<br />
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.</p></blockquote>
<p>google了下，貌似很多问这个问题的解决办法的。在一个论坛里面找到<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fubuntuforums.org%2Fshowpost.php%3Fp%3D6255014%26postcount%3D3" target="_blank">解决办法</a>：<br />
一个名为pstiady的童鞋说：</p>
<blockquote><p>I have solved the problem.</p>
<p> I edited the file:<br />
 $ sudo vi /etc/mysql/hosts.allow</p>
<p> And add a line:<br />
 mysqld : 127.0.0.1 : allow</p>
<p> And that&#8217;s done the trick </p></blockquote>
<p>看到它的话，我立马想到了一个东西：tcp wrapper ，tcp_wrapper 就是tcp封装，是运行在介于防火墙和内部服务间的一个模块<br />
只要支持tcp封装的服务，我们都可以使用tcp封装来控制他。<br />
验证下mysqld是不是支持tcp封装：</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">ldd</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> mysqld<span style="color: #000000; font-weight: bold;">`</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> libwrap<br />
&nbsp;<span style="color: #c20cb9; font-weight: bold;">strings</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> mysqld<span style="color: #000000; font-weight: bold;">`</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> gethostname</div></td></tr></tbody></table></div>
<p>果然是支持的。<br />
于是</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">vim</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>hosts.allow</div></td></tr></tbody></table></div>
<p>添加一行：</p>
<div class="codecolorer-container ini default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="ini codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mysqld:127.0.0.1</div></td></tr></tbody></table></div>
<p>OK,连接成功。<br />
之所以会这样是因为mysqld本身是支持tcp wrapper的，然后，linux默认的<strong>/etc/hosts.deny</strong>文件中有这么一行：<br />
<strong>ALL: ALL</strong>.因此，如果不修改hosts.allow文件的话，netbeans用java版的mysql connector连接mysql数据库时，很杯具地被tcp wrapper给阻挡在墙外了。。。</p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4672";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4672" title="Permanent Link to netbeans 连接 mysql数据库com.mysql.jdbc.CommunicationsException: Communications link failure解决办法" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4672</a>
            </p>
             </div><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2011年09月16日  //  <a href="http://ihacklog.com/software/development-tool/netbeans-subversion-auto-props-svn-keywords.html" title="netbeans subversion自动添加Id,Revision等keywords">netbeans subversion自动添加Id,Revision等keywords</a> (1)</li><li>2011年07月11日  //  <a href="http://ihacklog.com/software/development-tool/netbeans-filetemplates.html" title="netbeans自定义文件模板(备忘)">netbeans自定义文件模板(备忘)</a> (2)</li><li>2011年07月10日  //  <a href="http://ihacklog.com/software/development-tool/install-cool-theme-for-netbeans-ide.html" title="给NetBeans IDE 换衣服">给NetBeans IDE 换衣服</a> (7)</li><li>2011年07月10日  //  <a href="http://ihacklog.com/php/skills/install-phpdocumentor-pear-package.html" title="安装PhpDocumentor PEAR包">安装PhpDocumentor PEAR包</a> (1)</li><li>2011年07月10日  //  <a href="http://ihacklog.com/software/development-tool/netbeans-ide.html" title="NetBeans IDE">NetBeans IDE</a> (6)</li><li>2011年05月7日  //  <a href="http://ihacklog.com/linux/archlinux/install-win7-and-archlinux-part-2.html" title="win7/archlinux双系统安装手记（二）">win7/archlinux双系统安装手记（二）</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/server/mysql/com-mysql-jdbc-communicationsexception-communications-link-failure-solution.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>win7 IIS 7.5 安装配置</title>
		<link>http://ihacklog.com/server/iis/win7-iis-7-5-setup.html</link>
		<comments>http://ihacklog.com/server/iis/win7-iis-7-5-setup.html#comments</comments>
		<pubDate>Tue, 12 Jul 2011 14:40:32 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[IIS]]></category>
		<category><![CDATA[win7]]></category>

		<guid isPermaLink="false">http://ihacklog.com/?p=4627</guid>
		<description><![CDATA[0&#215;01.安装 其它的不多说了，注意勾选上“应用程序开发功能”，看图： 0&#215;02.配置 从开始菜单打开“Internet 信息服务(IIS)管理器”（%windir%\system32\inetsrv\InetMgr.exe ），“管理区域”（Management Area），双击“功能委派”（Feature Delegation）： 然后选择错误页（Error Pages）并单击只读（Read Only），如下图 然后编辑站点目录下的web.config 删除httpErrors 节...]]></description>
			<content:encoded><![CDATA[<p>0&#215;01.<strong>安装</strong><br />
其它的不多说了，注意勾选上“应用程序开发功能”，看图：<br />
<a href="http://static.ihacklog.com/wp-files/2011/07/win7_IIS_install.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/07/win7_IIS_install-502x400.png" alt="" title="win7_IIS_install" width="502" height="400" class="alignnone size-medium wp-image-4628" /> </a></p>
<p>0&#215;02.<strong>配置</strong><br />
从开始菜单打开“Internet 信息服务(IIS)管理器”（%windir%\system32\inetsrv\InetMgr.exe ），“管理区域”（Management Area），双击“功能委派”（Feature Delegation）：<br />
<a href="http://static.ihacklog.com/wp-files/2011/07/Feature_Delegation.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/07/Feature_Delegation-550x241.png" alt="" title="Feature_Delegation" width="550" height="241" class="alignnone size-medium wp-image-4629" /> </a></p>
<p>然后选择<strong>错误页</strong>（Error Pages）并单击<strong>只读</strong>（Read Only），如下图<br />
<a href="http://static.ihacklog.com/wp-files/2011/07/Feature_Delegation_errorpages_readonly.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/07/Feature_Delegation_errorpages_readonly-550x205.png" alt="" title="Feature_Delegation_errorpages_readonly" width="550" height="205" class="alignnone size-medium wp-image-4630" /> </a></p>
<p>然后编辑站点目录下的<strong>web.config</strong><br />
删除<strong>httpErrors</strong> 节点，如果此配置文件没有其它特别的配置的话，可以直接删除由IIS为delegation Read/Write生成的<strong>web.config</strong>文件。<br />
如不进行此设置，遇到错误时全部会提示 500.19 错误。（此解决方法来自：<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fblogs.msdn.com%2Fb%2Fasiatech_zh-cn%2Farchive%2F2010%2F12%2F27%2Fiis7-404-500-19.aspx" target="_blank">IIS7中的自定义 404页面错误- 500.19</a>）</p>
<p>PS:<br />
经测试，添加<strong>allowAbsolutePathsWhenDelegated=”true”</strong> 属性的方法不可行。</p>
<p>其它的配置不多说了，说下怎么添加PHP<strong>映射</strong>和添加<strong>绑定</strong>：</p>
<p>a.<strong>配置 IIS 处理 PHP 请求 </strong>：<br />
FastCGI 正是从 IIS 7.0 开始成为内置组件的,推荐以FastCgi的方式运行PHP。</p>
<p>如果是 IIS7.0，微软已经发布了一系列升级补丁来修正其中内置的 FastCGI 模块已知的部分问题，你可以根据自己的系统下载对应的补丁进行升级：</p>
<li><a href="http://www.microsoft.com/downloads/info.aspx?na=22&amp;p=1&amp;SrcDisplayLang=en&amp;SrcCategoryId=&amp;SrcFamilyId=&amp;u=%2fdownloads%2fdetails.aspx%3fFamilyID%3dd0343911-1775-4aef-8c99-5f13862ac386%26DisplayLang%3den" target="_blank"><font color="#2970a6">Update for Windows Server 2008</font></a></li>
<li><a href="http://www.microsoft.com/downloads/info.aspx?na=22&amp;p=3&amp;SrcDisplayLang=en&amp;SrcCategoryId=&amp;SrcFamilyId=&amp;u=%2fdownloads%2fdetails.aspx%3fFamilyID%3d70278393-3291-4aa1-870b-0e9b0907bddf%26DisplayLang%3den" target="_blank"><font color="#2970a6">Update for Windows Server 2008 x64 Edition</font></a></li>
<li><a href="http://www.microsoft.com/downloads/info.aspx?na=22&amp;p=5&amp;SrcDisplayLang=en&amp;SrcCategoryId=&amp;SrcFamilyId=&amp;u=%2fdownloads%2fdetails.aspx%3fFamilyID%3d98e06637-0f00-45d5-83c5-ed1b41fd6a7b%26DisplayLang%3den" target="_blank"><font color="#2970a6">Update for Windows Server 2008 for Itanium-based Systems</font></a></li>
<li><a href="http://www.microsoft.com/downloads/info.aspx?na=22&amp;p=2&amp;SrcDisplayLang=en&amp;SrcCategoryId=&amp;SrcFamilyId=&amp;u=%2fdownloads%2fdetails.aspx%3fFamilyID%3d19600729-8470-4956-a276-200450d814bd%26DisplayLang%3den" target="_blank"><font color="#2970a6">Update for Windows Vista SP1</font></a></li>
<li><a href="http://www.microsoft.com/downloads/info.aspx?na=22&amp;p=4&amp;SrcDisplayLang=en&amp;SrcCategoryId=&amp;SrcFamilyId=&amp;u=%2fdownloads%2fdetails.aspx%3fFamilyID%3dc7066c3b-dcf7-4441-87bc-f7dcb51067d0%26DisplayLang%3den" target="_blank"><font color="#2970a6">Update for Windows Vista SP1 for x64 based Systems</font></a>
<p>关于PHP版本选择：<br />
PHP 5.3 For Windows有4个版本:<br />
 •VC9 x86 Non Thread Safe<br />
 •VC9 x86 Thread Safe<br />
 •VC6 x86 Non Thread Safe<br />
 •VC6 x86 Thread Safe</p>
<p>到底要选择哪个版本，<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwindows.php.net%2Fdownload%2F" target="_blank">官网</a>给出了答案：</p>
<blockquote><p>Which version do I choose?<br />
If you are using PHP with Apache 1 or Apache2 from apache.org you need to use the VC6 versions of PHP<br />
If you are using PHP with IIS you should use the VC9 versions of PHP<br />
VC6 Versions are compiled with the legacy Visual Studio 6 compiler<br />
VC9 Versions are compiled with the Visual Studio 2008 compiler and have improvements in performance and stability. The VC9 versions require you to have the <a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.microsoft.com%2Fdownloads%2Fdetails.aspx%3FFamilyID%3D9B2DA534-3E03-4391-8A4D-074B9F2BC1BF" target="_blank">Microsoft 2008 C++ Runtime (x86)</a> or the<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.microsoft.com%2Fdownloads%2Fdetails.aspx%3Ffamilyid%3Dbd2a6171-e2d6-4230-b809-9a8d7548c1b6%26displaylang%3Den" target="_blank"> Microsoft 2008 C++ Runtime (x64)</a> installed<br />
Do <strong>NOT</strong> use VC9 version with apache.org binaries.<br />
VC9 versions of Apache can be fetched at <a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.apachelounge.com%2Fdownload%2F" target="_blank">Apache Lounge</a>. We use their binaries to build the Apache SAPIs.
</p></blockquote>
<p>VC9的版本是给IIS用的，VC6的版本是给Apache用的。还提示说，<strong>不要</strong>将VC9的版本的PHP用于apache.org的二进制版本httpd，如果要VC9版本的httpd的话，可以到<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.apachelounge.com%2Fdownload%2F" target="_blank">Apache Lounge</a>下载。<br />
VC9版本是用Visual Studio 2008编译的，编译器对程序进行了更多的优化，因而性能较好。<strong>FastCGI</strong>是<strong>单线程</strong>执行的，因而不需要<strong>线程安全</strong>版本，去掉线程安全的防护后可以提高执行效率；而<strong>ISAPI</strong>方式则是<strong>多线程</strong>模式，所以需要采用<strong>线程安全</strong>版本。<br />
php.ini文件中确认以下设置：</p>
<div class="codecolorer-container ini default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="ini codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">fastcgi.impersonate <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 1</span><br />
cgi.fix_pathinfo<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">1</span><br />
cgi.force_redirect <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 0</span></div></td></tr></tbody></table></div>
<p>首先点击“<strong>FastCGI 设置</strong>”，添加PHP为FastCGI应用程序，在新建对话框的路径文本框中选择PHP目录下的“<strong>php_cgi.exe</strong>”，其他设置按默认的就行，如我这里是：<strong>D:\software\PHP\php-cgi.exe</strong>。<br />
<a href="http://static.ihacklog.com/wp-files/2011/07/iis_php.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/07/iis_php-550x237.png" alt="" title="iis_php" width="550" height="237" class="alignnone size-medium wp-image-4631" /> </a><br />
接着回到上级视图，打开“<strong>处理映射程序</strong>”，添加PHP的映射，配置如下图所示：<br />
<a href="http://static.ihacklog.com/wp-files/2011/07/php_FastCgiModule.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/07/php_FastCgiModule-550x261.png" alt="" title="php_FastCgiModule" width="550" height="261" class="alignnone size-medium wp-image-4632" /> </a><br />
单击“添加模块映射”，<br />
请求路径那里填写： <strong>*.php</strong><br />
模块选择：<strong>FastCgiModule</strong><br />
可执行文件那里填写php-cgi.exe的绝对路径，如： <strong>D:\software\PHP\php-cgi.exe</strong></p>
<p>接下来可以调整一下<strong>默认文档</strong>，也很简单，直接上图了：<br />
<a href="http://static.ihacklog.com/wp-files/2011/07/iis_default_document.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/07/iis_default_document-550x237.png" alt="" title="iis_default_document" width="550" height="237" class="alignnone size-medium wp-image-4636" /> </a></p>
<p>另外，如果要方便开发的话，可以打开“<strong>目录浏览</strong>”功能（此功能默认是禁用的）。</p>
<p>b.<strong>网站绑定</strong>：<br />
这里的绑定可以绑定IP或者绑定主机名。这里说下绑定主机名，这个也简单，直接上图吧：<br />
<a href="http://static.ihacklog.com/wp-files/2011/07/iis_hostname_bind.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/07/iis_hostname_bind-550x269.png" alt="" title="iis_hostname_bind" width="550" height="269" class="alignnone size-medium wp-image-4634" /> </a><br />
主要是“主机名”、“端口” 、“IP”，这里<strong> *</strong> 表示主机任意可用IP。如上配置，则通过访问：</p>
<blockquote><p>http://huangye-pc:8088/</p>
<p>http://t.com:8088/</p>
<p>http://localhost:8088/</p>
<p>http://127.0.0.1:8088/</p></blockquote>
<p>都能访问此站点（前提是这些主机名与IP的映射在hosts文件中要存在）。</p>
<p>c.测试<br />
asp测试：<br />
test.asp</p>
<div class="codecolorer-container asp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="asp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;html&gt;<br />
&lt;body&gt;<br />
<span style="color: #000000; font-weight: bold;">&lt;%</span><br />
<span style="color: #990099; font-weight: bold;">response</span>.<span style="color: #330066;">write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Hello World!&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">%&gt;</span><br />
&lt;/body&gt;<br />
&lt;/html&gt;</div></td></tr></tbody></table></div>
<p>访问此文件，正常情况下应该输出 <strong>Hello World!</strong><br />
asp.net 测试<br />
test.aspx</p>
<div class="codecolorer-container asp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="asp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">@</span> Page Language<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;C#&quot;</span><span style="color: #000000; font-weight: bold;">%&gt;</span><br />
<span style="color: #000000; font-weight: bold;">&lt;%</span> <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;hello world!&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></div></td></tr></tbody></table></div>
<p>php测试：<br />
phpinfo.php</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<a href="http://www.php.net/phpinfo"><span style="color: #990000;">phpinfo</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p><a href="http://static.ihacklog.com/wp-files/2011/07/iis_php_phpinfo.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/07/iis_php_phpinfo-550x255.png" alt="" title="iis_php_phpinfo" width="550" height="255" class="alignnone size-medium wp-image-4635" /> </a></p>
<p>参考：<br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.iis.net%2FConfigReference%2Fsystem.webServer%2FhttpErrors" target="_blank">http://www.iis.net/ConfigReference/system.webServer/httpErrors</a><br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fheeroluo.net%2FShowPost30.aspx" target="_blank">IIS 7.5 下PHP（FastCGI模式）配置手记</a></p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4627";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4627" title="Permanent Link to win7 IIS 7.5 安装配置" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4627</a>
            </p>
             </div><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2011年11月16日  //  <a href="http://ihacklog.com/software/tips/opnet-14-5-vs2008-win7-trouble-and-the-solution.html" title="opnet 14.5 + VS2008 + win7＝麻烦">opnet 14.5 + VS2008 + win7＝麻烦</a> (6)</li><li>2011年08月29日  //  <a href="http://ihacklog.com/software/cool_software_share/setup-win7-custom-file-extension-icons.html" title="Win7文件类型图标自定义">Win7文件类型图标自定义</a> (3)</li><li>2011年07月14日  //  <a href="http://ihacklog.com/software/tips/things-to-do-after-win7-installation.html" title="win7安装完后要做的16件事">win7安装完后要做的16件事</a> (24)</li><li>2011年07月14日  //  <a href="http://ihacklog.com/software/tips/get-quicklunch-bar-back-for-windows-7.html" title="找回WIN7中消失的快速启动栏">找回WIN7中消失的快速启动栏</a> (2)</li><li>2011年05月7日  //  <a href="http://ihacklog.com/linux/archlinux/install-win7-and-archlinux-part-2.html" title="win7/archlinux双系统安装手记（二）">win7/archlinux双系统安装手记（二）</a> (0)</li><li>2011年05月4日  //  <a href="http://ihacklog.com/linux/archlinux/install-win7-and-archlinux-part-1.html" title="win7/archlinux双系统安装手记（一）">win7/archlinux双系统安装手记（一）</a> (7)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/server/iis/win7-iis-7-5-setup.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>关于vsftpd匿名+虚拟用户的一些认识</title>
		<link>http://ihacklog.com/server/vsftpd/about-vsftpd-anoymous-and-virtual-user.html</link>
		<comments>http://ihacklog.com/server/vsftpd/about-vsftpd-anoymous-and-virtual-user.html#comments</comments>
		<pubDate>Sat, 25 Jun 2011 19:44:10 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[vsftpd]]></category>
		<category><![CDATA[anoymous]]></category>
		<category><![CDATA[virtual user]]></category>

		<guid isPermaLink="false">http://ihacklog.com/?p=4580</guid>
		<description><![CDATA[先前在ubuntu 10.04 server 上配置好的vsftpd 匿名+虚拟用户认证，把配置copy到 现用的Archlinux下面来做测试，发现匿名用户居然不启作用。。。 用xp来登录ftp ,发现提示 500 错误： 不能更改目录到 /srv/ftp 为什么我明明配置了 anon_root=/home/ftp/public 却不起作用呢？ 123&#91;root@huangye etc&#93;# cat /etc/passwd &#124; grep &#34;ftp&#34; vsftpd:...]]></description>
			<content:encoded><![CDATA[<p>先前在ubuntu 10.04 server 上配置好的vsftpd 匿名+虚拟用户认证，把配置copy到<br />
现用的Archlinux下面来做测试，发现匿名用户居然不启作用。。。</p>
<p>用xp来登录ftp ,发现提示 500 错误： 不能更改目录到 /srv/ftp<br />
为什么我明明配置了<br />
<strong>anon_root=/home/ftp/public</strong><br />
却不起作用呢？</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>huangye etc<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># cat /etc/passwd | grep &quot;ftp&quot;</span><br />
vsftpd:x:<span style="color: #000000;">1001</span>:<span style="color: #000000;">1001</span>::<span style="color: #000000; font-weight: bold;">/</span>srv<span style="color: #000000; font-weight: bold;">/</span>ftp:<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">false</span><br />
ftp:x:<span style="color: #000000;">1002</span>:<span style="color: #000000;">1002</span>::<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>ftp:<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">false</span></div></td></tr></tbody></table></div>
<p>发现默认的匿名ftp用户的根目录居然是 /srv/ftp ,难怪提示这个错误。<br />
不过，在ubuntu 10.04 server 上，passwd 文件中同样如此，匿名用户<br />
却能正常使用。<br />
在我的archlinux中，我把上面的/srv/ftp 修改为 /home/ftp后匿名访问就<br />
OK了。真是奇怪了。<br />
还有就是，vsftpd匿名用户的root不能让其它用户也有写权限，即777是肯定不行的。</p>
<p>根据vsftpd的man手册，anoymous 和 ftp 都可作用匿名ftp登录的用户名<br />
由于ftp是默认的匿名用户的username ,因此系统中必须存在这个用户，<br />
如不存在可用命令建立：</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> useradd <span style="color: #c20cb9; font-weight: bold;">ftp</span> <span style="color: #660033;">-m</span> <span style="color: #660033;">-U</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">false</span></div></td></tr></tbody></table></div>
<p>ftp 这个用户名也可以指定为别的，配置参数是 ftp_username </p>
<blockquote><p>
<strong>ftp_username</strong><br />
  This is the name of the user we use for handling anonymous  FTP.<br />
               The home directory of this user is the root of the anonymous FTP<br />
               area.<br />
               Default: ftp
 </p></blockquote>
<p>关于vsftpd里面的用户<br />
其实vsftpd里面分三种用户：匿名/虚拟/本地<br />
当启用了虚拟用户后，本地用户的登录默认就被屏蔽了。<br />
但是虚拟用户实际上是以本地用户(由guest_username这个配置项来指定用户名）来登录的。</p>
<p>要启用虚拟用户（guest_enable=YES），本地用户必须被启用（local_enable=YES）<br />
，虚拟用户默认是同匿名用户拥有一样的权限。<br />
若要改变这个，则virtual_use_local_privs=YES<br />
让虚拟用户拥有和本地用户一样的权限，即本地用户拥有读写权限的文件，虚拟用户<br />
也照样拥有。<br />
如果默认我们给虚拟用户所有的权限，那么我们要限制单个虚拟用户的权限就得通过<br />
指定用户配置文件目录，然后在此目录下新建与用户名相同的配置文件。<br />
vsftpd这一点很强大，可以用白名单或者黑名单，基于黑白名单同时存在来控制用户<br />
可以执行的ftp命令。（ cmds_allowed 和 cmds_denied )</p>
<p><strong>可以通过tcp_wrapper来限制用户的登录区域（IP范围）</strong><br />
tcp_wrapper 就是tcp封装，是运行在介于防火墙和内部服务间的一个模块<br />
只要支持tcp封装的服务，我们都可以使用tcp封装来控制他<br />
查看一个服务是不是支持tcp_wrapper的方法：<br />
ldd `which vsftpd` | grep libwrap<br />
看是否有libwrap.so.0</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #7a0874; font-weight: bold;">&#91;</span>admin<span style="color: #000000; font-weight: bold;">@</span>huangye etc<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">ldd</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> vsftpd<span style="color: #000000; font-weight: bold;">`</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;libwrap&quot;</span><br />
&nbsp; &nbsp; libwrap.so.0 =<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>libwrap.so.0 <span style="color: #7a0874; font-weight: bold;">&#40;</span>0xb7828000<span style="color: #7a0874; font-weight: bold;">&#41;</span></div></td></tr></tbody></table></div>
<p>strings `which vsftpd` | grep host</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #7a0874; font-weight: bold;">&#91;</span>admin<span style="color: #000000; font-weight: bold;">@</span>huangye etc<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">strings</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> vsftpd<span style="color: #000000; font-weight: bold;">`</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;host&quot;</span><br />
sock_host<br />
hosts_access<br />
gethostbyname<br />
gethostbyname<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>: neither IPv4 nor IPv6<br />
cannot resolve host:</div></td></tr></tbody></table></div>
<p>确定你的编译版支持tcp wrapper后，开启此功能：<br />
<strong>tcp_wrappers=YES</strong><br />
使用tcp_wrapper就需要两个配置文件:</p>
<blockquote><p>
/etc/hosts.deny<br />
/etc/hosts.allow
</p></blockquote>
<p>详细介绍请man hosts.deny<br />
默认hosts.deny中有一条：<br />
<strong>ALL: ALL</strong><br />
这样是禁用所有IP访问所有DAEMONS。<br />
因此，启动tcp wrapper后，要能正常访问，<br />
必须添加相应的daemon,client 对到 hosts.allow 文件中。<br />
如允许所有ip访问vsftpd:<br />
<strong>vsftpd:ALL</strong></p>
<p><strong>可以通过chroot来禁用ftp用户跳转到不应该去的地方，比如 /etc</strong><br />
要使用chroot功能，有两种办法，一是开启chroot_list 功能，二是开启<br />
chroot_local_user功能。<br />
默认的secure_chroot_dir为<strong>/usr/share/empty</strong> ,我们可以手动指定。</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>vsftpd</div></td></tr></tbody></table></div>
<p>注意此目录一定不能让vsftpd这个用户有写权限。<br />
<strong>secure_chroot_dir=/var/run/vsftpd</strong><br />
1.开启chroot_list功能，且新建chroot_list文件放在 /etc/vsftpd/目录下面</p>
<blockquote><p>
chroot_list_enable=YES<br />
chroot_list_file=/etc/vsftpd/chroot_list
</p></blockquote>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">cut</span> &nbsp;<span style="color: #660033;">-d</span> &nbsp;: &nbsp;<span style="color: #660033;">-f</span> &nbsp;<span style="color: #000000;">1</span> &nbsp;<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">passwd</span> &nbsp;<span style="color: #000000; font-weight: bold;">&gt;&gt;</span> chroot_list</div></td></tr></tbody></table></div>
<p>-d 是列分隔符，-f 是指定第几列（从1开始）<br />
这样所有用户就进去了。<br />
2.直接<br />
<strong>chroot_local_user=YES</strong></p>
<p>参考资料：<br />
<a href="http://redking.blog.51cto.com/27212/136218">RHCE课程-RH253Linux服务器架设笔记一-VSFTPD的配置（2） &#8211; 王乾De技术Blog[爱生活，爱学习] &#8211; 51CTO技术博客</a></p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4580";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4580" title="Permanent Link to 关于vsftpd匿名+虚拟用户的一些认识" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4580</a>
            </p>
             </div><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2010年05月15日  //  <a href="http://ihacklog.com/server/vsftpd-530-login-incorrect-libpam-not-exists.html" title="安装vsftpd再次遇到530 login incorrect">安装vsftpd再次遇到530 login incorrect</a> (0)</li><li>2010年05月1日  //  <a href="http://ihacklog.com/server/vsftpd-530-login-incorrect.html" title="vsftpd 530 login incorrect">vsftpd 530 login incorrect</a> (1)</li><li>2009年11月2日  //  <a href="http://ihacklog.com/server/vsftpd-configure.html" title=" vsftpd安装配置"> vsftpd安装配置</a> (3)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/server/vsftpd/about-vsftpd-anoymous-and-virtual-user.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>锐捷开机启动认证修改版</title>
		<link>http://ihacklog.com/server/ruijie-client-startup-as-service-version2.html</link>
		<comments>http://ihacklog.com/server/ruijie-client-startup-as-service-version2.html#comments</comments>
		<pubDate>Thu, 14 Apr 2011 13:01:39 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[服务器]]></category>
		<category><![CDATA[开机启动]]></category>
		<category><![CDATA[认证]]></category>
		<category><![CDATA[锐捷]]></category>

		<guid isPermaLink="false">http://www.ihacklog.com/?p=4352</guid>
		<description><![CDATA[继上一篇《折腾锐捷开机自动启动认证》，今修改代码若干。 运行环境：winxp/2003 (WIN 7 没有测试过） 可执行文件下载： 用途：让锐捷客户端在桌面用户没有登录系统之前就启动认证。 前提条件：务必在锐捷客户端里设置里勾选“启动软件后自动认证”，“保存密码” 。 由于时间有限，就写简单点了，如停止服务时，服务状态（是否已经停止了？）的判断什么的就没有理会了。 用法： 安装服务： ruijieservice.exe install 卸载服务（重启后会彻底删除服务）： ruijieservice.ex...]]></description>
			<content:encoded><![CDATA[<p>继上一篇《<a target="_blank" href="http://www.ihacklog.com/l.php?url=www.ihacklog.com/orz__p=4350">折腾锐捷开机自动启动认证</a>》，今修改代码若干。<br />
运行环境：winxp/2003 (WIN 7  没有测试过）<br />
可执行文件下载：<br />
注意：该页面嵌入了下载文件，请访问 <a href="http://ihacklog.com/server/ruijie-client-startup-as-service-version2.html">该页面</a>下载该文件。</p>
<p>用途：<strong>让锐捷客户端在桌面用户没有登录系统之前就启动认证</strong>。<br />
前提条件：务必在锐捷客户端里设置里勾选“<strong>启动软件后自动认证</strong>”，“<strong>保存密码</strong>” 。</p>
<p>由于时间有限，就写简单点了，如停止服务时，服务状态（是否已经停止了？）的判断什么的就没有理会了。</p>
<blockquote><p>用法：<br />
安装服务：<br />
ruijieservice.exe install<br />
卸载服务（重启后会彻底删除服务）：<br />
ruijieservice.exe uninstall<br />
启动服务：<br />
ruijieservice.exe start<br />
停止服务：<br />
ruijieservice.exe stop
</p></blockquote>
<p><strong>config.txt</strong> 文件内容为锐捷客户端的可执行文件绝对路径，如：<br />
<strong>C:\Program Files\锐捷网络\Ruijie Supplicant\8021x.exe</strong><br />
请自行修改。<br />
<a href="http://static.ihacklog.com/wp-files/2011/04/config.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/04/config-550x220.png" alt="" title="config" width="550" height="220" class="alignnone size-medium wp-image-4353" /> </a></p>
<p><a href="http://static.ihacklog.com/wp-files/2011/04/service.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/04/service-550x105.png" alt="" title="service" width="550" height="105" class="alignnone size-medium wp-image-4354" /> </a></p>
<p>头文件：stdafx.h</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// stdafx.h : 标准系统包含文件的包含文件，</span><br />
<span style="color: #666666; font-style: italic;">// 或是经常使用但不常更改的</span><br />
<span style="color: #666666; font-style: italic;">// 特定于项目的包含文件</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
<br />
<span style="color: #339933;">#pragma once</span><br />
<br />
<span style="color: #339933;">#include &quot;targetver.h&quot;</span><br />
<br />
<span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;tchar.h&gt;</span></div></td></tr></tbody></table></div>
<p>源文件ruijieservice.cpp</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br />165<br />166<br />167<br />168<br />169<br />170<br />171<br />172<br />173<br />174<br />175<br />176<br />177<br />178<br />179<br />180<br />181<br />182<br />183<br />184<br />185<br />186<br />187<br />188<br />189<br />190<br />191<br />192<br />193<br />194<br />195<br />196<br />197<br />198<br />199<br />200<br />201<br />202<br />203<br />204<br />205<br />206<br />207<br />208<br />209<br />210<br />211<br />212<br />213<br />214<br />215<br />216<br />217<br />218<br />219<br />220<br />221<br />222<br />223<br />224<br />225<br />226<br />227<br />228<br />229<br />230<br />231<br />232<br />233<br />234<br />235<br />236<br />237<br />238<br />239<br />240<br />241<br />242<br />243<br />244<br />245<br />246<br />247<br />248<br />249<br />250<br />251<br />252<br />253<br />254<br />255<br />256<br />257<br />258<br />259<br />260<br />261<br />262<br />263<br />264<br />265<br />266<br />267<br />268<br />269<br />270<br />271<br />272<br />273<br />274<br />275<br />276<br />277<br />278<br />279<br />280<br />281<br />282<br />283<br />284<br />285<br />286<br />287<br />288<br />289<br />290<br />291<br />292<br />293<br />294<br />295<br />296<br />297<br />298<br />299<br />300<br />301<br />302<br />303<br />304<br />305<br />306<br />307<br />308<br />309<br />310<br />311<br />312<br />313<br />314<br />315<br />316<br />317<br />318<br />319<br />320<br />321<br />322<br />323<br />324<br />325<br />326<br />327<br />328<br />329<br />330<br />331<br />332<br />333<br />334<br />335<br />336<br />337<br />338<br />339<br />340<br />341<br />342<br />343<br />344<br />345<br />346<br />347<br />348<br />349<br />350<br />351<br />352<br />353<br />354<br />355<br />356<br />357<br />358<br />359<br />360<br />361<br />362<br />363<br />364<br />365<br />366<br />367<br />368<br />369<br />370<br />371<br />372<br />373<br />374<br />375<br />376<br />377<br />378<br />379<br />380<br />381<br />382<br />383<br />384<br />385<br />386<br />387<br />388<br />389<br />390<br />391<br />392<br />393<br />394<br />395<br />396<br />397<br />398<br />399<br />400<br />401<br />402<br />403<br />404<br />405<br />406<br />407<br />408<br />409<br />410<br />411<br />412<br />413<br />414<br />415<br />416<br />417<br />418<br />419<br />420<br />421<br />422<br />423<br />424<br />425<br />426<br />427<br />428<br />429<br />430<br />431<br />432<br />433<br />434<br />435<br />436<br />437<br />438<br />439<br />440<br />441<br />442<br />443<br />444<br />445<br />446<br />447<br />448<br />449<br />450<br />451<br />452<br />453<br />454<br />455<br />456<br />457<br />458<br />459<br />460<br />461<br />462<br />463<br />464<br />465<br />466<br />467<br />468<br />469<br />470<br />471<br />472<br />473<br />474<br />475<br />476<br />477<br />478<br />479<br />480<br />481<br />482<br />483<br />484<br />485<br />486<br />487<br />488<br />489<br />490<br />491<br />492<br />493<br />494<br />495<br />496<br />497<br />498<br />499<br />500<br />501<br />502<br />503<br />504<br />505<br />506<br />507<br />508<br />509<br />510<br />511<br />512<br />513<br />514<br />515<br />516<br />517<br />518<br />519<br />520<br />521<br />522<br />523<br />524<br />525<br />526<br />527<br />528<br />529<br />530<br />531<br />532<br />533<br />534<br />535<br />536<br />537<br />538<br />539<br />540<br />541<br />542<br />543<br />544<br />545<br />546<br />547<br />548<br />549<br />550<br />551<br />552<br />553<br />554<br />555<br />556<br />557<br />558<br />559<br />560<br />561<br />562<br />563<br />564<br />565<br />566<br />567<br />568<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// ruijieservice.cpp : 定义控制台应用程序的入口点。</span><br />
<span style="color: #666666; font-style: italic;">//description:锐捷开机启动认证服务</span><br />
<span style="color: #666666; font-style: italic;">//code by by 荒野无灯</span><br />
<span style="color: #666666; font-style: italic;">//url http://www.ihacklog.com</span><br />
<br />
<span style="color: #339933;">#include &quot;stdafx.h&quot;</span><br />
<span style="color: #339933;">#include &lt;windows.h&gt;</span><br />
<br />
<span style="color: #339933;">#define SVCNAME TEXT(&quot;RuijieSvc&quot;)</span><br />
<span style="color: #339933;">#define SVCNAME_DISPLAY TEXT(&quot;Ruijie startup before login&quot;)</span><br />
<span style="color: #339933;">#define SVC_DESCRIPTION TEXT(&quot;用于在桌面用户登录前启动锐捷认证&quot;)</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #666666; font-style: italic;">// MessageId: SVC_ERROR</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #666666; font-style: italic;">// MessageText:</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp;An error has occurred (%2).</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp;</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #339933;">#define SVC_ERROR &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;((DWORD)0xC0020001L)</span><br />
<br />
<br />
SERVICE_STATUS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;gSvcStatus<span style="color: #339933;">;</span> <br />
SERVICE_STATUS_HANDLE &nbsp; gSvcStatusHandle<span style="color: #339933;">;</span> <br />
HANDLE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ghSvcStopEvent <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span><br />
<br />
PROCESS_INFORMATION &nbsp;lpProcessInfo<span style="color: #339933;">;</span><br />
<br />
<br />
VOID SvcInstall<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
VOID WINAPI SvcCtrlHandler<span style="color: #009900;">&#40;</span> DWORD <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
VOID WINAPI SvcMain<span style="color: #009900;">&#40;</span> DWORD<span style="color: #339933;">,</span> LPTSTR <span style="color: #339933;">*</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<br />
VOID ReportSvcStatus<span style="color: #009900;">&#40;</span> DWORD<span style="color: #339933;">,</span> DWORD<span style="color: #339933;">,</span> DWORD <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
VOID SvcInit<span style="color: #009900;">&#40;</span> DWORD<span style="color: #339933;">,</span> LPTSTR <span style="color: #339933;">*</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
VOID SvcReportEvent<span style="color: #009900;">&#40;</span> LPTSTR <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//删除服务</span><br />
VOID DoDeleteSvc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">//启动服务</span><br />
VOID DoStartSvc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">//停止服务</span><br />
VOID DoStopSvc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">//显示用法</span><br />
VOID DisplayUsage<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">//干活的</span><br />
DWORD WINAPI do_action<span style="color: #009900;">&#40;</span>LPVOID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; Entry point for the process</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #993333;">int</span> _tmain<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> _TCHAR<span style="color: #339933;">*</span> argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>lstrcmpi<span style="color: #009900;">&#40;</span> argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> TEXT<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;/?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; DisplayUsage<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> lstrcmpi<span style="color: #009900;">&#40;</span> argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> TEXT<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;install&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SvcInstall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> lstrcmpi<span style="color: #009900;">&#40;</span> argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> TEXT<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;delete&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> &nbsp;<span style="color: #339933;">||</span> &nbsp;lstrcmpi<span style="color: #009900;">&#40;</span> argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> TEXT<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;uninstall&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; DoDeleteSvc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> lstrcmpi<span style="color: #009900;">&#40;</span> argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> TEXT<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;start&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; DoStartSvc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> lstrcmpi<span style="color: #009900;">&#40;</span> argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> TEXT<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;stop&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; DoStopSvc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// TO_DO: Add any additional services for the process to this table.</span><br />
&nbsp; &nbsp; SERVICE_TABLE_ENTRY DispatchTable<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> SVCNAME<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>LPSERVICE_MAIN_FUNCTION<span style="color: #009900;">&#41;</span> SvcMain <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> NULL<span style="color: #339933;">,</span> NULL <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// This call returns when the service has stopped. </span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// The process should simply terminate when the call returns.</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>StartServiceCtrlDispatcher<span style="color: #009900;">&#40;</span> DispatchTable <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; SvcReportEvent<span style="color: #009900;">&#40;</span>TEXT<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;StartServiceCtrlDispatcher&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<br />
<br />
<span style="color: #666666; font-style: italic;">//服务安装</span><br />
VOID SvcInstall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; SC_HANDLE schSCManager<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; SC_HANDLE schService<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; TCHAR szPath<span style="color: #009900;">&#91;</span>MAX_PATH<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>GetModuleFileName<span style="color: #009900;">&#40;</span> NULL<span style="color: #339933;">,</span> szPath<span style="color: #339933;">,</span> MAX_PATH <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Cannot install service (%d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> GetLastError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get a handle to the SCM database. </span><br />
&nbsp;<br />
&nbsp; &nbsp; schSCManager <span style="color: #339933;">=</span> OpenSCManager<span style="color: #009900;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; NULL<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// local computer</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NULL<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// ServicesActive database </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SC_MANAGER_ALL_ACCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #666666; font-style: italic;">// full access rights </span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>NULL <span style="color: #339933;">==</span> schSCManager<span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;OpenSCManager failed (%d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> GetLastError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Create the service</span><br />
<br />
&nbsp; &nbsp; &nbsp;TCHAR lpDependencies<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">20</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;Tcpip&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; schService <span style="color: #339933;">=</span> CreateService<span style="color: #009900;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; schSCManager<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// SCM database </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SVCNAME<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// name of service </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SVCNAME_DISPLAY<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// service name to display </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SERVICE_ALL_ACCESS<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// desired access </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SERVICE_WIN32_OWN_PROCESS<span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// 服务类型为独占进程类型 </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SERVICE_AUTO_START<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// 启动类型设置为自启动</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SERVICE_ERROR_NORMAL<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// error control type </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; szPath<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// path to service's binary </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NULL<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// no load ordering group </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NULL<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// no tag identifier </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; lpDependencies<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// 依存关系 </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NULL<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// LocalSystem account </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// no password </span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>schService <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;CreateService failed (%d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> GetLastError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; CloseServiceHandle<span style="color: #009900;">&#40;</span>schSCManager<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Service installed successfully<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//服务描述</span><br />
&nbsp; &nbsp; LPTSTR szDesc <span style="color: #339933;">=</span> SVC_DESCRIPTION<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; SERVICE_DESCRIPTION sd<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; sd.<span style="color: #202020;">lpDescription</span><span style="color: #339933;">=</span>szDesc<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ChangeServiceConfig2<span style="color: #009900;">&#40;</span> schService<span style="color: #339933;">,</span> &nbsp;SERVICE_CONFIG_DESCRIPTION<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>sd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; CloseServiceHandle<span style="color: #009900;">&#40;</span>schService<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; CloseServiceHandle<span style="color: #009900;">&#40;</span>schSCManager<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; Entry point for the service</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
VOID WINAPI SvcMain<span style="color: #009900;">&#40;</span> DWORD dwArgc<span style="color: #339933;">,</span> LPTSTR <span style="color: #339933;">*</span>lpszArgv <span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Register the handler function for the service</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//调用RegisterServiceCtrlHandler函数注册一个SvcCtrlHandler函数来向服务发送控制请求信息，</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//返回值是服务状态句柄用来向服务控制管理器传送服务状态。</span><br />
<br />
&nbsp; &nbsp; gSvcStatusHandle <span style="color: #339933;">=</span> RegisterServiceCtrlHandler<span style="color: #009900;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; SVCNAME<span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; SvcCtrlHandler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>gSvcStatusHandle <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; SvcReportEvent<span style="color: #009900;">&#40;</span>TEXT<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;RegisterServiceCtrlHandler&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// These SERVICE_STATUS members remain as set here</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//设置服务类型为：独占进程服务</span><br />
&nbsp; &nbsp; gSvcStatus.<span style="color: #202020;">dwServiceType</span> <span style="color: #339933;">=</span> SERVICE_WIN32_OWN_PROCESS<span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; gSvcStatus.<span style="color: #202020;">dwServiceSpecificExitCode</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Report initial status to the SCM</span><br />
<br />
&nbsp; &nbsp; ReportSvcStatus<span style="color: #009900;">&#40;</span> SERVICE_START_PENDING<span style="color: #339933;">,</span> NO_ERROR<span style="color: #339933;">,</span> <span style="color: #0000dd;">3000</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Perform service-specific initialization and work.</span><br />
<br />
&nbsp; &nbsp; SvcInit<span style="color: #009900;">&#40;</span> dwArgc<span style="color: #339933;">,</span> lpszArgv <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; The service code</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
VOID SvcInit<span style="color: #009900;">&#40;</span> DWORD dwArgc<span style="color: #339933;">,</span> LPTSTR <span style="color: #339933;">*</span>lpszArgv<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// TO_DO: Declare and set any required variables.</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// &nbsp; Be sure to periodically call ReportSvcStatus() with </span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// &nbsp; SERVICE_START_PENDING. If initialization fails, call</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// &nbsp; ReportSvcStatus with SERVICE_STOPPED.</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Create an event. The control handler function, SvcCtrlHandler,</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// signals this event when it receives the stop control code.</span><br />
<br />
&nbsp; &nbsp; ghSvcStopEvent <span style="color: #339933;">=</span> CreateEvent<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NULL<span style="color: #339933;">,</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// default security attributes</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TRUE<span style="color: #339933;">,</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// manual reset event</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FALSE<span style="color: #339933;">,</span> &nbsp; <span style="color: #666666; font-style: italic;">// not signaled</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; <span style="color: #666666; font-style: italic;">// no name</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> ghSvcStopEvent <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ReportSvcStatus<span style="color: #009900;">&#40;</span> SERVICE_STOPPED<span style="color: #339933;">,</span> NO_ERROR<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Report running status when initialization is complete.</span><br />
<br />
&nbsp; &nbsp; ReportSvcStatus<span style="color: #009900;">&#40;</span> SERVICE_RUNNING<span style="color: #339933;">,</span> NO_ERROR<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// TO_DO: Perform work until service stops.</span><br />
&nbsp; &nbsp; HANDLE hThread<span style="color: #339933;">=</span>CreateThread<span style="color: #009900;">&#40;</span>NULL<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>do_action<span style="color: #339933;">,</span>NULL<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>hThread<span style="color: #339933;">==</span>NULL<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; WaitForSingleObject<span style="color: #009900;">&#40;</span>hThread<span style="color: #339933;">,</span>INFINITE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Check whether to stop the service.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; WaitForSingleObject<span style="color: #009900;">&#40;</span>ghSvcStopEvent<span style="color: #339933;">,</span> INFINITE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; ReportSvcStatus<span style="color: #009900;">&#40;</span> SERVICE_STOPPED<span style="color: #339933;">,</span> NO_ERROR<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #666666; font-style: italic;">// Purpose: </span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; Sets the current service status and reports it to the SCM.</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #666666; font-style: italic;">// Parameters:</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; dwCurrentState - The current state (see SERVICE_STATUS)</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; dwWin32ExitCode - The system error code</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; dwWaitHint - Estimated time for pending operation, </span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; &nbsp; in milliseconds</span><br />
<span style="color: #666666; font-style: italic;">// </span><br />
<span style="color: #666666; font-style: italic;">// Return value:</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; None</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
VOID ReportSvcStatus<span style="color: #009900;">&#40;</span> DWORD dwCurrentState<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DWORD dwWin32ExitCode<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DWORD dwWaitHint<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">static</span> DWORD dwCheckPoint <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Fill in the SERVICE_STATUS structure.</span><br />
<br />
&nbsp; &nbsp; gSvcStatus.<span style="color: #202020;">dwCurrentState</span> <span style="color: #339933;">=</span> dwCurrentState<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gSvcStatus.<span style="color: #202020;">dwWin32ExitCode</span> <span style="color: #339933;">=</span> dwWin32ExitCode<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gSvcStatus.<span style="color: #202020;">dwWaitHint</span> <span style="color: #339933;">=</span> dwWaitHint<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>dwCurrentState <span style="color: #339933;">==</span> SERVICE_START_PENDING<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; gSvcStatus.<span style="color: #202020;">dwControlsAccepted</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span> gSvcStatus.<span style="color: #202020;">dwControlsAccepted</span> <span style="color: #339933;">=</span> SERVICE_ACCEPT_STOP<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>dwCurrentState <span style="color: #339933;">==</span> SERVICE_RUNNING<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#40;</span>dwCurrentState <span style="color: #339933;">==</span> SERVICE_STOPPED<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; gSvcStatus.<span style="color: #202020;">dwCheckPoint</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span> gSvcStatus.<span style="color: #202020;">dwCheckPoint</span> <span style="color: #339933;">=</span> dwCheckPoint<span style="color: #339933;">++;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Report the status of the service to the SCM.</span><br />
&nbsp; &nbsp; SetServiceStatus<span style="color: #009900;">&#40;</span> gSvcStatusHandle<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>gSvcStatus <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #666666; font-style: italic;">// Purpose: </span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; Called by SCM whenever a control code is sent to the service</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; using the ControlService function.</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #666666; font-style: italic;">// Parameters:</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; dwCtrl - control code</span><br />
<span style="color: #666666; font-style: italic;">// </span><br />
<span style="color: #666666; font-style: italic;">// Return value:</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; None</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
VOID WINAPI SvcCtrlHandler<span style="color: #009900;">&#40;</span> DWORD dwCtrl <span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; HANDLE hProcess<span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Handle the requested control code. </span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span>dwCtrl<span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">case</span> SERVICE_CONTROL_STOP<span style="color: #339933;">:</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">case</span> SERVICE_CONTROL_SHUTDOWN<span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ReportSvcStatus<span style="color: #009900;">&#40;</span>SERVICE_STOP_PENDING<span style="color: #339933;">,</span> NO_ERROR<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//kill the process</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; hProcess <span style="color: #339933;">=</span> <span style="color: #339933;">::</span><span style="color: #202020;">OpenProcess</span><span style="color: #009900;">&#40;</span>PROCESS_TERMINATE<span style="color: #339933;">,</span>FALSE<span style="color: #339933;">,</span>lpProcessInfo.<span style="color: #202020;">dwProcessId</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// 等待1秒 </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>WaitForSingleObject<span style="color: #009900;">&#40;</span> hProcess<span style="color: #339933;">,</span> <span style="color: #0000dd;">1000</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> WAIT_OBJECT_0<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">::</span><span style="color: #202020;">TerminateProcess</span><span style="color: #009900;">&#40;</span>hProcess<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CloseHandle<span style="color: #009900;">&#40;</span>hProcess<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Signal the service to stop.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetEvent<span style="color: #009900;">&#40;</span>ghSvcStopEvent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">case</span> SERVICE_CONTROL_INTERROGATE<span style="color: #339933;">:</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Fall through to send current status.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ReportSvcStatus<span style="color: #009900;">&#40;</span>gSvcStatus.<span style="color: #202020;">dwCurrentState</span><span style="color: #339933;">,</span> NO_ERROR<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <br />
<br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; Logs messages to the event log</span><br />
<span style="color: #666666; font-style: italic;">// Parameters:</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; szFunction - name of function that failed</span><br />
<span style="color: #666666; font-style: italic;">// Return value:</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; None</span><br />
<span style="color: #666666; font-style: italic;">// Remarks:</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; The service must have an entry in the Application event log.</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
VOID SvcReportEvent<span style="color: #009900;">&#40;</span>LPTSTR szFunction<span style="color: #009900;">&#41;</span> <br />
<span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; HANDLE hEventSource<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; LPCTSTR lpszStrings<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; TCHAR Buffer<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">80</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; hEventSource <span style="color: #339933;">=</span> RegisterEventSource<span style="color: #009900;">&#40;</span>NULL<span style="color: #339933;">,</span> SVCNAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> NULL <span style="color: #339933;">!=</span> hEventSource <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; sprintf_s<span style="color: #009900;">&#40;</span>Buffer<span style="color: #339933;">,</span> <span style="color: #0000dd;">80</span><span style="color: #339933;">,</span> TEXT<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s failed with %d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> szFunction<span style="color: #339933;">,</span> GetLastError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; lpszStrings<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> SVCNAME<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; lpszStrings<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> Buffer<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; ReportEvent<span style="color: #009900;">&#40;</span>hEventSource<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// event log handle</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EVENTLOG_ERROR_TYPE<span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// event type</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// event category</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SVC_ERROR<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// event identifier</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NULL<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// no security identifier</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// size of lpszStrings array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// no binary data</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lpszStrings<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// array of strings</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// no binary data</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; DeregisterEventSource<span style="color: #009900;">&#40;</span>hEventSource<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; Deletes a service from the SCM database</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
VOID DoDeleteSvc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; SC_HANDLE schSCManager<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; SC_HANDLE schService<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get a handle to the SCM database. </span><br />
&nbsp;<br />
<br />
&nbsp; &nbsp; schSCManager <span style="color: #339933;">=</span> OpenSCManager<span style="color: #009900;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; NULL<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// local computer</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NULL<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// ServicesActive database </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SC_MANAGER_ALL_ACCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #666666; font-style: italic;">// full access rights </span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>NULL <span style="color: #339933;">==</span> schSCManager<span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;OpenSCManager failed (%d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> GetLastError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get a handle to the service.</span><br />
<br />
&nbsp; &nbsp; schService <span style="color: #339933;">=</span> OpenService<span style="color: #009900;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; schSCManager<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// SCM database </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SVCNAME<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// name of service </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; DELETE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// need delete access </span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>schService <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;OpenService failed (%d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> GetLastError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; CloseServiceHandle<span style="color: #009900;">&#40;</span>schSCManager<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Delete the service.</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> DeleteService<span style="color: #009900;">&#40;</span>schService<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;DeleteService failed (%d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> GetLastError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Service deleted successfully<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp;<br />
&nbsp; &nbsp; CloseServiceHandle<span style="color: #009900;">&#40;</span>schService<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; CloseServiceHandle<span style="color: #009900;">&#40;</span>schSCManager<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; start a service from the SCM database</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
VOID DoStartSvc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; SC_HANDLE schSCManager<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; SC_HANDLE schService<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get a handle to the SCM database. </span><br />
&nbsp;<br />
<br />
&nbsp; &nbsp; schSCManager <span style="color: #339933;">=</span> OpenSCManager<span style="color: #009900;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; NULL<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// local computer</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NULL<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// ServicesActive database </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SC_MANAGER_ALL_ACCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #666666; font-style: italic;">// full access rights </span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>NULL <span style="color: #339933;">==</span> schSCManager<span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;OpenSCManager failed (%d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> GetLastError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get a handle to the service.</span><br />
<br />
&nbsp; &nbsp; schService <span style="color: #339933;">=</span> OpenService<span style="color: #009900;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; schSCManager<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// SCM database </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SVCNAME<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// name of service </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SERVICE_START <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; <span style="color: #666666; font-style: italic;">// Required the SERVICE_START access right </span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>schService <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;OpenService failed!Error num:%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> GetLastError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; CloseServiceHandle<span style="color: #009900;">&#40;</span>schSCManager<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// start the service.</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> StartService<span style="color: #009900;">&#40;</span>schService<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;StartService failed (%d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> GetLastError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Service started successfully<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp;<br />
&nbsp; &nbsp; CloseServiceHandle<span style="color: #009900;">&#40;</span>schService<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; CloseServiceHandle<span style="color: #009900;">&#40;</span>schSCManager<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<br />
<br />
VOID DoStopSvc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; SC_HANDLE schSCManager<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; SC_HANDLE schService<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get a handle to the SCM database. </span><br />
&nbsp;<br />
<br />
&nbsp; &nbsp; schSCManager <span style="color: #339933;">=</span> OpenSCManager<span style="color: #009900;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; NULL<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// local computer</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NULL<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// ServicesActive database </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SC_MANAGER_ALL_ACCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #666666; font-style: italic;">// full access rights </span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>NULL <span style="color: #339933;">==</span> schSCManager<span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;OpenSCManager failed (%d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> GetLastError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get a handle to the service.</span><br />
<br />
&nbsp; &nbsp; schService <span style="color: #339933;">=</span> OpenService<span style="color: #009900;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; schSCManager<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// SCM database </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SVCNAME<span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// name of service </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SERVICE_STOP<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; <span style="color: #666666; font-style: italic;">// must have the SERVICE_STOP access right </span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>schService <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;OpenService failed!Error num:%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> GetLastError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; CloseServiceHandle<span style="color: #009900;">&#40;</span>schSCManager<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// stop the service.</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> &nbsp;ControlService<span style="color: #009900;">&#40;</span> &nbsp;schService<span style="color: #339933;">,</span> &nbsp;SERVICE_CONTROL_STOP<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>gSvcStatus<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;StopService failed (%d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> GetLastError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Service stoped successfully<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp;<br />
&nbsp; &nbsp; CloseServiceHandle<span style="color: #009900;">&#40;</span>schService<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; CloseServiceHandle<span style="color: #009900;">&#40;</span>schSCManager<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
<br />
VOID DisplayUsage<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>bin<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;##################ruijie client startup as service ####################<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; by 荒野无灯<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; admin#ihacklog.com<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Description:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>Command-line tool that configures the ruijie service.<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>Please create a text file named config.txt under current path.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>The content of the file should be the full path of the Ruijie Supplicant program.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Usage:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>%s [command]<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>bin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span> &nbsp;[command]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span> &nbsp;install<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span> &nbsp;uninstall<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span> &nbsp;start<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span> &nbsp;stop<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
DWORD WINAPI do_action<span style="color: #009900;">&#40;</span>LPVOID<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; STARTUPINFO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lpStartupInfo<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> ret<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; GetStartupInfo<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>lpStartupInfo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; lpStartupInfo.<span style="color: #202020;">cb</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>lpStartupInfo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; lpStartupInfo.<span style="color: #202020;">dwFlags</span> &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=</span> STARTF_USESHOWWINDOW <span style="color: #339933;">|</span> STARTF_USESTDHANDLES<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//lpStartupInfo.wShowWindow &nbsp;= SW_HIDE;</span><br />
&nbsp; &nbsp; lpStartupInfo.<span style="color: #202020;">wShowWindow</span> &nbsp;<span style="color: #339933;">=</span> SW_SHOW<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; lpStartupInfo.<span style="color: #202020;">lpDesktop</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;WinSta0<span style="color: #000099; font-weight: bold;">\\</span>Default&quot;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//lpStartupInfo.lpDesktop = &quot;NewDesktop&quot;; </span><br />
<br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> cmdline<span style="color: #009900;">&#91;</span>MAX_PATH<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; FILE <span style="color: #339933;">*</span>fp<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> path<span style="color: #009900;">&#91;</span>MAX_PATH<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//::GetCurrentDirectory(100,path);</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>GetModuleFileName<span style="color: #009900;">&#40;</span>NULL<span style="color: #339933;">,</span>path<span style="color: #339933;">,</span>MAX_PATH<span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span> <span style="color: #339933;">*</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/strrchr.html"><span style="color: #000066;">strrchr</span></a><span style="color: #009900;">&#40;</span>path<span style="color: #339933;">,</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\'</span>) )= '</span>\<span style="color: #0000dd;">0</span><span style="color: #ff0000;">';//丢掉文件名，得到路径 <br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; strcat_s(path,&quot;<span style="color: #000099; font-weight: bold;">\\</span>config.txt&quot;);<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; if( (fp=fopen(path,&quot;rb&quot;) ) != NULL )<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; fgets(cmdline,MAX_PATH,fp);<br />
&nbsp; &nbsp; &nbsp; &nbsp; fclose(fp);<br />
&nbsp; &nbsp; &nbsp; &nbsp; ( *strrchr(cmdline, '</span>\x0D<span style="color: #ff0000;">') )= '</span>\<span style="color: #0000dd;">0</span><span style="color: #ff0000;">';<br />
&nbsp; &nbsp; &nbsp; &nbsp; /*<br />
&nbsp; &nbsp; &nbsp; &nbsp; char *p;<br />
&nbsp; &nbsp; &nbsp; &nbsp; p=cmdline+strlen(cmdline)-1;<br />
&nbsp; &nbsp; &nbsp; &nbsp; // cmdline <span style="color: #660099; font-weight: bold;">\x0D</span> <span style="color: #660099; font-weight: bold;">\x0A</span> <span style="color: #006699; font-weight: bold;">\0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; p--; // <span style="color: #660099; font-weight: bold;">\x0D</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; *p='</span>\<span style="color: #0000dd;">0</span><span style="color: #ff0000;">'; &nbsp;// <span style="color: #660099; font-weight: bold;">\x0D</span> =&gt; <span style="color: #006699; font-weight: bold;">\0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; */<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; else<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; strcpy_s(cmdline,MAX_PATH,&quot;C:<span style="color: #000099; font-weight: bold;">\\</span>Program Files<span style="color: #000099; font-weight: bold;">\\</span>锐捷网络<span style="color: #000099; font-weight: bold;">\\</span>Ruijie Supplicant<span style="color: #000099; font-weight: bold;">\\</span>8021x.exe&quot;);<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; ret = CreateProcess(cmdline, NULL, NULL,NULL,TRUE,0,NULL,NULL,&amp;lpStartupInfo,&amp;lpProcessInfo);<br />
&nbsp; &nbsp; return 0;<br />
}</span></div></td></tr></tbody></table></div>
<p>参考文档和相关WIN API：<br />
<a target="_blank" href="http://www.ihacklog.com/l.php?url=msdn.microsoft.com/zh-cn/library/ms685942">Service Functions</a></p>
<p><a target="_blank" href="http://www.ihacklog.com/l.php?url=msdn.microsoft.com/zh-cn/library/ms684330">OpenService Function</a></p>
<p><a target="_blank" href="http://www.ihacklog.com/l.php?url=msdn.microsoft.com/zh-cn/library/ms685981">Service Security and Access Rights</a></p>
<p><a target="_blank" href="http://www.ihacklog.com/l.php?url=msdn.microsoft.com/zh-cn/library/ms682108">ControlService function</a><br />
<a target="_blank" href="http://www.ihacklog.com/l.php?url=msdn.microsoft.com/zh-cn/library/ms686321">StartService Function<br />
</a></p>
<p><a target="_blank" href="http://www.ihacklog.com/l.php?url=msdn.microsoft.com/zh-cn/library/ms681381">System Error Codes</a></p>
<p><a target="_blank" href="http://www.ihacklog.com/l.php?url=msdn.microsoft.com/zh-cn/library/ms682450">CreateService Function</a></p>
<p><a target="_blank" href="http://www.ihacklog.com/l.php?url=msdn.microsoft.com/zh-cn/library/ms682006">Changing a Service&#8217;s Configuration</a></p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4352";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4352" title="Permanent Link to 锐捷开机启动认证修改版" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4352</a>
            </p>
             </div><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2011年04月13日  //  <a href="http://ihacklog.com/server/ruijie-startup-as-servcie.html" title="折腾锐捷开机自动启动认证">折腾锐捷开机自动启动认证</a> (7)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/server/ruijie-client-startup-as-service-version2.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>折腾锐捷开机自动启动认证</title>
		<link>http://ihacklog.com/server/ruijie-startup-as-servcie.html</link>
		<comments>http://ihacklog.com/server/ruijie-startup-as-servcie.html#comments</comments>
		<pubDate>Wed, 13 Apr 2011 14:33:56 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[服务器]]></category>
		<category><![CDATA[开机启动]]></category>
		<category><![CDATA[认证]]></category>
		<category><![CDATA[锐捷]]></category>

		<guid isPermaLink="false">http://www.ihacklog.com/?p=4350</guid>
		<description><![CDATA[学校用的是锐捷 802.1x 认证，其客户端支持开机自动启动、启动软件后自动认证。 由于我这里需要的是客户端在桌面用户没有登录系统之前就启动认证，使机器具有访问外部网络的功能。因此靠软件本身应该行不通。 想到WINDOWS系统的服务是在用户登录之前可以加载启动的，因此这里主要是把锐捷注册为服务来实现开机即认证。 首先，在锐捷客户端里设置“启动软件后自动认证”，“保存密码”等。 然后，用VC写个东东，作为服务，由它来创建锐捷认证的进程。就把生成后的程序命名为ruijie_service.exe　吧。 代码如...]]></description>
			<content:encoded><![CDATA[<p>学校用的是锐捷 802.1x 认证，其客户端支持开机自动启动、启动软件后自动认证。<br />
由于我这里需要的是<strong>客户端在桌面用户没有登录系统之前就启动认证</strong>，使机器具有访问外部网络的功能。因此靠软件本身应该行不通。<br />
想到WINDOWS系统的服务是在用户登录之前可以加载启动的，因此这里主要是把锐捷注册为服务来实现开机即认证。</p>
<p>首先，在锐捷客户端里设置“启动软件后自动认证”，“保存密码”等。<br />
然后，用VC写个东东，作为服务，由它来创建锐捷认证的进程。就把生成后的程序命名为ruijie_service.exe　吧。<br />
代码如下：<br />
“<strong>C:\\Program Files\\锐捷网络\\Ruijie Supplicant\\8021x.exe</strong>“为锐捷认证客户端的程序路径。</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//////////////////////////////////////////////////////////////////////////////////////</span><br />
<span style="color: #666666; font-style: italic;">//程序原作者：冷风</span><br />
<span style="color: #666666; font-style: italic;">//修改：荒野无灯 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //</span><br />
<span style="color: #666666; font-style: italic;">//////////////////////////////////////////////////////////////////////////////////////</span><br />
<br />
<span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;string.h&gt;</span><br />
<span style="color: #339933;">#include &lt;windows.h&gt;</span><br />
<span style="color: #339933;">#include &lt;tlhelp32.h&gt;</span><br />
<span style="color: #339933;">#include &lt;winsvc.h&gt;</span><br />
<br />
<br />
SERVICE_STATUS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ServiceStatus<span style="color: #339933;">;</span><br />
SERVICE_STATUS_HANDLE &nbsp; hStatus<span style="color: #339933;">;</span><br />
<br />
<br />
VOID&nbsp; &nbsp; WINAPI ServiceMain<span style="color: #009900;">&#40;</span> DWORD dwArgc<span style="color: #339933;">,</span> LPTSTR <span style="color: #339933;">*</span>lpszArgv <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
VOID&nbsp; &nbsp; WINAPI ServiceHandler<span style="color: #009900;">&#40;</span> DWORD fdwControl <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
DWORD &nbsp; WINAPI mainfun<span style="color: #009900;">&#40;</span>LPVOID lpParam<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; SERVICE_TABLE_ENTRY ServiceTable<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ServiceTable<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">lpServiceName</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;8021x&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ServiceTable<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">lpServiceProc</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>LPSERVICE_MAIN_FUNCTION<span style="color: #009900;">&#41;</span>ServiceMain<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ServiceTable<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">lpServiceName</span> <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ServiceTable<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">lpServiceProc</span> <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; StartServiceCtrlDispatcher<span style="color: #009900;">&#40;</span>ServiceTable<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// 启动服务的控制分派机线程</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<br />
<br />
VOID WINAPI ServiceHandler<span style="color: #009900;">&#40;</span>DWORD fdwControl<span style="color: #009900;">&#41;</span> <br />
<span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span>fdwControl<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> SERVICE_CONTROL_PAUSE<span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwCurrentState</span> <span style="color: #339933;">=</span> SERVICE_PAUSED<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> SERVICE_CONTROL_CONTINUE<span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwCurrentState</span> <span style="color: #339933;">=</span> SERVICE_RUNNING<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> SERVICE_CONTROL_STOP<span style="color: #339933;">:</span> <br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> SERVICE_CONTROL_SHUTDOWN<span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwCurrentState</span> &nbsp;<span style="color: #339933;">=</span> SERVICE_STOPPED<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwWin32ExitCode</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwCheckPoint</span> &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwWaitHint</span> &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SetServiceStatus<span style="color: #009900;">&#40;</span>hStatus<span style="color: #339933;">,&amp;</span>ServiceStatus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> SERVICE_CONTROL_INTERROGATE<span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; SetServiceStatus<span style="color: #009900;">&#40;</span>hStatus<span style="color: #339933;">,&amp;</span>ServiceStatus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
VOID WINAPI ServiceMain<span style="color: #009900;">&#40;</span> DWORD dwArgc<span style="color: #339933;">,</span> LPTSTR <span style="color: #339933;">*</span>lpszArgv <span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; DWORD &nbsp; status <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; DWORD &nbsp; specificError <span style="color: #339933;">=</span> <span style="color: #208080;">0xfffffff</span><span style="color: #339933;">;</span> <br />
&nbsp;<br />
&nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwServiceType</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=</span> SERVICE_WIN32<span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwCurrentState</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> SERVICE_START_PENDING<span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwControlsAccepted</span> &nbsp; <span style="color: #339933;">=</span> SERVICE_ACCEPT_STOP <span style="color: #339933;">|</span> SERVICE_ACCEPT_SHUTDOWN <span style="color: #339933;">|</span> SERVICE_ACCEPT_PAUSE_CONTINUE<span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwWin32ExitCode</span> &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwServiceSpecificExitCode</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwCheckPoint</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwWaitHint</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <br />
&nbsp;<br />
&nbsp; &nbsp; hStatus <span style="color: #339933;">=</span> RegisterServiceCtrlHandler<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;8021x&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span>LPHANDLER_FUNCTION<span style="color: #009900;">&#41;</span>ServiceHandler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>hStatus<span style="color: #339933;">==</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span> <br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Handle error condition </span><br />
&nbsp; &nbsp; status <span style="color: #339933;">=</span> GetLastError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>status<span style="color: #339933;">!=</span>NO_ERROR<span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwCurrentState</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> SERVICE_STOPPED<span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwCheckPoint</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwWaitHint</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwWin32ExitCode</span> &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=</span> status<span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwServiceSpecificExitCode</span> <span style="color: #339933;">=</span> specificError<span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; SetServiceStatus<span style="color: #009900;">&#40;</span>hStatus<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>ServiceStatus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Initialization complete - report running status </span><br />
&nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwCurrentState</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> SERVICE_RUNNING<span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwCheckPoint</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; ServiceStatus.<span style="color: #202020;">dwWaitHint</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; SetServiceStatus<span style="color: #009900;">&#40;</span>hStatus<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>ServiceStatus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">//do my job</span><br />
&nbsp; &nbsp;HANDLE hThread<span style="color: #339933;">=</span>CreateThread<span style="color: #009900;">&#40;</span>NULL<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>mainfun<span style="color: #339933;">,</span>NULL<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>hThread<span style="color: #339933;">==</span>NULL<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; WaitForSingleObject<span style="color: #009900;">&#40;</span>hThread<span style="color: #339933;">,</span>INFINITE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<br />
DWORD WINAPI mainfun<span style="color: #009900;">&#40;</span>LPVOID lpParam<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<br />
<br />
&nbsp; &nbsp; STARTUPINFO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lpStartupInfo<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; PROCESS_INFORMATION &nbsp;lpProcessInfo<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> ret<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; GetStartupInfo<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>lpStartupInfo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; lpStartupInfo.<span style="color: #202020;">cb</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>lpStartupInfo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; lpStartupInfo.<span style="color: #202020;">dwFlags</span> &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=</span> STARTF_USESHOWWINDOW <span style="color: #339933;">|</span> STARTF_USESTDHANDLES<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; lpStartupInfo.<span style="color: #202020;">wShowWindow</span> &nbsp;<span style="color: #339933;">=</span> SW_HIDE<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; lpStartupInfo.<span style="color: #202020;">lpDesktop</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;WinSta0<span style="color: #000099; font-weight: bold;">\\</span>Default&quot;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//lpStartupInfo.lpDesktop = &quot;NewDesktop&quot;; </span><br />
<br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> cmdline<span style="color: #009900;">&#91;</span>MAX_PATH<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #808080; font-style: italic;">/*&nbsp; char path[MAX_PATH];<br />
&nbsp; &nbsp; ::GetCurrentDirectory(100,path);<br />
&nbsp; &nbsp; strcat(path,&quot;\\config.txt&quot;);<br />
&nbsp; &nbsp; FILE *fp;<br />
&nbsp; &nbsp; fp=fopen(path,&quot;r&quot;);<br />
&nbsp; &nbsp; if(( fp=fopen(path,&quot;r&quot;) ) !=NULL )<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; fread(path,MAX_PATH,1,fp);<br />
&nbsp; &nbsp; fclose(fp);<br />
&nbsp; &nbsp; strcpy(cmdline,path);<br />
&nbsp; &nbsp; ret = CreateProcess(cmdline, NULL, NULL,NULL,TRUE,0,NULL,NULL,&amp;lpStartupInfo,&amp;lpProcessInfo);<br />
&nbsp; &nbsp; }<br />
*/</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcpy.html"><span style="color: #000066;">strcpy</span></a><span style="color: #009900;">&#40;</span>cmdline<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;C:<span style="color: #000099; font-weight: bold;">\\</span>Program Files<span style="color: #000099; font-weight: bold;">\\</span>锐捷网络<span style="color: #000099; font-weight: bold;">\\</span>Ruijie Supplicant<span style="color: #000099; font-weight: bold;">\\</span>8021x.exe&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ret <span style="color: #339933;">=</span> CreateProcess<span style="color: #009900;">&#40;</span>cmdline<span style="color: #339933;">,</span> NULL<span style="color: #339933;">,</span> NULL<span style="color: #339933;">,</span>NULL<span style="color: #339933;">,</span>TRUE<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>NULL<span style="color: #339933;">,</span>NULL<span style="color: #339933;">,&amp;</span>lpStartupInfo<span style="color: #339933;">,&amp;</span>lpProcessInfo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>把生成的程序置于：D:\ruijie_service.exe<br />
最后，写一个BAT脚本。内容如下：<br />
其中<strong>D:\ruijie_service.exe</strong> 为上一步生成的程序路径。</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">echo</span> off<br />
color 2f<br />
sc create 8021x <span style="color: #007800;">binpath</span>= <span style="color: #ff0000;">&quot;D:<span style="color: #000099; font-weight: bold;">\r</span>uijie_service.exe&quot;</span> <span style="color: #007800;">displayname</span>= <span style="color: #ff0000;">&quot;8021x&quot;</span> <span style="color: #007800;">depend</span>= Tcpip<br />
sc config 8021x <span style="color: #007800;">start</span>= auto<br />
<span style="color: #7a0874; font-weight: bold;">echo</span> 设置成功 <br />
net start 8021x<br />
pause</div></td></tr></tbody></table></div>
<p>此脚本只需运行一遍即可。<br />
如要删除该服务，运行如下命令即可：</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sc delete 8021x</div></td></tr></tbody></table></div>
<p>然后重启即可。</p>
<p>这样以后，只要电脑开机了，不用登录进去，机子就已经连网了。<br />
在WIN2003 enterprise简体中文版 SP2 下测试成功。</p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4350";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4350" title="Permanent Link to 折腾锐捷开机自动启动认证" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4350</a>
            </p>
             </div><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2011年04月14日  //  <a href="http://ihacklog.com/server/ruijie-client-startup-as-service-version2.html" title="锐捷开机启动认证修改版">锐捷开机启动认证修改版</a> (6)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/server/ruijie-startup-as-servcie.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

