﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-小明思考-随笔分类-Win32</title><link>http://www.cppblog.com/sandy/category/768.html</link><description>高性能服务器端计算</description><language>zh-cn</language><lastBuildDate>Tue, 18 Nov 2008 09:17:55 GMT</lastBuildDate><pubDate>Tue, 18 Nov 2008 09:17:55 GMT</pubDate><ttl>60</ttl><item><title>When program crashed (windows)</title><link>http://www.cppblog.com/sandy/archive/2008/11/17/67108.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Mon, 17 Nov 2008 05:54:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2008/11/17/67108.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/67108.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2008/11/17/67108.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/67108.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/67108.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: When program crashed ...&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2008/11/17/67108.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/67108.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2008-11-17 13:54 <a href="http://www.cppblog.com/sandy/archive/2008/11/17/67108.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>IOCP Tips</title><link>http://www.cppblog.com/sandy/archive/2008/03/12/44248.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Wed, 12 Mar 2008 03:51:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2008/03/12/44248.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/44248.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2008/03/12/44248.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/44248.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/44248.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 使用完成端口的一些基本技巧&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2008/03/12/44248.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/44248.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2008-03-12 11:51 <a href="http://www.cppblog.com/sandy/archive/2008/03/12/44248.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>多进程服务端实现-共享socket</title><link>http://www.cppblog.com/sandy/archive/2008/03/10/44080.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Mon, 10 Mar 2008 06:09:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2008/03/10/44080.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/44080.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2008/03/10/44080.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/44080.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/44080.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 多进程服务端实现-共享socket&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2008/03/10/44080.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/44080.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2008-03-10 14:09 <a href="http://www.cppblog.com/sandy/archive/2008/03/10/44080.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用完成端口写的echo server</title><link>http://www.cppblog.com/sandy/archive/2007/06/06/25670.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Wed, 06 Jun 2007 09:44:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2007/06/06/25670.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/25670.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2007/06/06/25670.html#Feedback</comments><slash:comments>9</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/25670.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/25670.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2007/06/06/25670.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/25670.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2007-06-06 17:44 <a href="http://www.cppblog.com/sandy/archive/2007/06/06/25670.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>WinDBG(CDB)实践1-新手指南</title><link>http://www.cppblog.com/sandy/archive/2007/03/13/19723.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Tue, 13 Mar 2007 06:39:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2007/03/13/19723.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/19723.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2007/03/13/19723.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/19723.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/19723.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Windbg(CDB) 新手指南&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2007/03/13/19723.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/19723.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2007-03-13 14:39 <a href="http://www.cppblog.com/sandy/archive/2007/03/13/19723.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SendMessage 和 SendMessageTimeout</title><link>http://www.cppblog.com/sandy/archive/2006/07/21/10288.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Fri, 21 Jul 2006 01:50:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/07/21/10288.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/10288.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/07/21/10288.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/10288.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/10288.html</trackback:ping><description><![CDATA[SendMessageTimeout并不是简单在SendMessage加上Timeout的功能。<br /><br />MSDN上面有一段文字是这样说的<br /><br />If the specified window was created by the calling thread, the window procedure is called immediately as a subroutine. If the specified window was created by a different thread, the system switches to that thread and calls the appropriate window procedure. Messages sent between threads are processed only when the receiving thread executes message retrieval code. The sending thread is blocked until the receiving thread processes the message. However, the sending thread will process incoming nonqueued messages while waiting for its message to be processed. To prevent this, use <?XML:NAMESPACE PREFIX = MSHelp NS = "http://msdn.microsoft.com/mshelp" /?><mshelp:link tabindex="0" keywords="_win32_SendMessageTimeout_cpp" xmlns:mshelp="http://msdn.microsoft.com/mshelp"><u><font color="#0000ff">SendMessageTimeout</font></u></mshelp:link> with SMTO_BLOCK set. For more information on nonqueued messages, see <mshelp:link tabindex="0" keywords="_win32_About_Messages_and_Message_Queues_cpp" xmlns:mshelp="http://msdn.microsoft.com/mshelp"><u><font color="#0000ff">Nonqueued Messages</font></u></mshelp:link>. <br /><br /><br />翻译一下：<br /><br />SendMessage : 如果指定窗口由调用线程创建，那么窗口过程会被当成一个子程序立即调用。如果指定窗口由另外一个线程创建，那么系统会切换到那个线程，并且调用合适的窗口过程。在线程之间传递的消息仅仅当接收线程执行message retrieval code才会被处理。发送线程会被堵塞直到接收线程处理完消息。<strong>但是，发送线程在等待的同时会处理收到的nonqueued messages 。为了阻止这一点，使用带有SMTO_BLOCK参数 的SendMessageTimeout .<br /><br /></strong>=================================华丽的分割线===========================<br /><br />我曾经遇到这个问题，我调用SendMessage向另外一个线程窗口发message，本来以为他会一直block住，但是他却调用了另外一个消息的处理程序，导致了行为不正确。<strong>所以一定要小心使用SendMessage发给其他线程的窗口。</strong><br /><br />我修改了一下，把<br />pWnd-&gt;SendMessage(MSG_LOG_MESSAGE, nMsgType, (LPARAM)(LPCTSTR)m_cstrMessage);<br />改成了<br />HWND hWnd = pWnd-&gt;GetSafeHwnd();<br />::SendMessageTimeout(hWnd,MSG_LOG_MESSAGE, nMsgType, (LPARAM)(LPCTSTR)m_cstrMessage,SMTO_BLOCK,15000,0);<br />解决了这个bug.<br /><img src ="http://www.cppblog.com/sandy/aggbug/10288.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-07-21 09:50 <a href="http://www.cppblog.com/sandy/archive/2006/07/21/10288.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Win32的时间类型</title><link>http://www.cppblog.com/sandy/archive/2006/03/29/4725.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Wed, 29 Mar 2006 03:59:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/03/29/4725.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/4725.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/03/29/4725.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/4725.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/4725.html</trackback:ping><description><![CDATA[在Win32时间类型有这样几种类型<font face="Courier New">FILETIME/</font><code>SYSTEMTIME/WINDOWSTIME<br /><br /><font style="BACKGROUND-COLOR: #ffffff" color="#0000ff"><font face="Verdana" size="4">先提个问题，如何计算当前时间100天以后的时间，在win32中。</font><br /></font><br />1.SYSTEMTIME <br /><br /></code>SYSTEMTIME的定义是这样<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">typedef </span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">  _SYSTEMTIME<br />    {<br />    WORD wYear;<br />    WORD wMonth;<br />    WORD wDayOfWeek;<br />    WORD wDay;<br />    WORD wHour;<br />    WORD wMinute;<br />    WORD wSecond;<br />    WORD wMilliseconds;<br />    } SYSTEMTIME;</span></div><br />很大，总共16个字节。你可以调用GetSystemTime来取得当前的时间<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">SYSTEMTIME st;<br />::GetSystemTime(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">st);</span></div>但是如果你调用 st.wDay += 100;是达不到效果的，将会得到一个无效的时间格式<br /><br />2. FILETIME<br />FILETIME的定义是这样的<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">typedef </span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">  _FILETIME<br />    {<br />    DWORD dwLowDateTime;<br />    DWORD dwHighDateTime;<br />    } FILETIME;</span></div>FILETIME这个名字很奇怪，其实他跟FILE并没有直接关系，只是很多File的API中，都以这个为时间的类型，比如你可以调用GetFileTime来取得File的创建/修改/访问时间。<br />FILETIME是一个__int64。可以这样转化成__int64<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">__int64 d </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(__int64 </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">st;<br /><br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">or</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000"><br />__int64 d2;<br />memcpy(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">d2,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">st,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(d2));</span></div><br />转化为__int64是一个以100纳秒为单位的值<br />补习一下时间单位(<a href="http://blog.vckbase.com/zaboli/archive/2005/06/29/8969.aspx">http://blog.vckbase.com/zaboli/archive/2005/06/29/8969.aspx</a>)<br /><table style="WIDTH: 528px; HEIGHT: 116px" width="528" border="0"><tbody><tr><td width="108"><font face="黑体, 华文中宋, 隶书, 新宋体" color="#330099" size="2">1ms (毫秒)</font></td><td width="269"><font face="黑体, 华文中宋, 隶书, 新宋体" color="#330099" size="2">1毫秒=0.001秒=10-3秒(millisecond)</font></td></tr><tr><td width="108"><font face="黑体, 华文中宋, 隶书, 新宋体" color="#330099" size="2">1μs (微秒) </font></td><td width="269"><font face="黑体, 华文中宋, 隶书, 新宋体" color="#330099" size="2">1微秒=0.000001=10-6秒(microsecond)</font></td></tr><tr><td width="108"><font face="黑体, 华文中宋, 隶书, 新宋体" color="#330099" size="2">1ns (纳秒)</font></td><td width="269"><font face="黑体, 华文中宋, 隶书, 新宋体" color="#330099" size="2">1纳秒=0.000000001秒=10-9秒（<font color="#000000" size="3">nanosecond</font>)</font></td></tr><tr><td width="108"><font face="黑体, 华文中宋, 隶书, 新宋体" color="#330099" size="2">1ps (皮秒)</font></td><td width="269"><font face="黑体, 华文中宋, 隶书, 新宋体" color="#330099" size="2">1皮秒=0.000000000001秒=10-12秒</font></td></tr><tr><td width="108"><font face="黑体, 华文中宋, 隶书, 新宋体" color="#330099" size="2">1fs (飞秒)</font></td><td width="269"><font face="黑体, 华文中宋, 隶书, 新宋体" color="#330099" size="2">1飞秒=0.00000000000001秒=10-15秒</font></td></tr></tbody></table><br />SYSTEMTIME可以通过SystemTimeToFileTime来转化为FILETIME<br /><br />3.Window Time<br />::GetTickCount()可以返回当前的以微秒为单位的时间，用在精度要求不高的场合，返回的只是一个DWORD，四字节。高一点的使用timeGetTime<br /><br /><font face="Courier New" color="#0000ff">如何计算当前时间100天以后的时间，在win32中。<br />应该这样写<br /></font><br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> __int64 nano100SecInDay</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(__int64)</span><span style="COLOR: #000000">10000000</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">60</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">60</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">24</span><span style="COLOR: #000000">;<br /><br />SYSTEMTIME st;<br />::GetSystemTime(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">st);<br />FILETIME f;<br />::SystemTimeToFileTime(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">st,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">f);<br />ULARGE_INTEGER now;<br />memcpy(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">now,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">f,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(now));<br />now </span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">nano100SecInDay;<br />memcpy(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">f,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">now,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(f));<br />FileTimeToSystemTime(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">f,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">st);<br /></span></div><br /><br />最后：附上我写的一个小工具，删除指定文件夹下面的过期文件的一个工具，并可以备份，支持子文件夹嵌套。<br /><br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">windows.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br />#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstdio</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br />#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> std;<br /><br /></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> __int64 nano100SecInDay</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(__int64)</span><span style="COLOR: #000000">10000000</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">60</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">60</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">24</span><span style="COLOR: #000000">;<br />ULARGE_INTEGER now;<br /></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> days </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">14</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000"> backup </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> dst_dir;<br /></span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> backup_dir;<br /><br /></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> make_sure_dir_exist(</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> file)<br />{<br />    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> tag </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;<br />    </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">)<br />    {<br />        tag </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> file.find_first_of(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,tag</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br />        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(tag</span><span style="COLOR: #000000">!=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br />        {<br />            </span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> tmp </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> file.substr(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,tag);<br />            ::CreateDirectory(tmp.c_str(),NULL);<br />        }<br />        </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br />        {<br />            </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br />        }<br />    }<br />}<br /><br /></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> xdel(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">dir)<br />{<br />    </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> tempFileFind[</span><span style="COLOR: #000000">1024</span><span style="COLOR: #000000">];<br />    sprintf(tempFileFind,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s\\*.*</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,dir);<br />    <br />    WIN32_FIND_DATA ffd;<br />    HANDLE hFind;<br />    hFind </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> ::FindFirstFile(tempFileFind,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">ffd);<br />    <br />    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(hFind </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> INVALID_HANDLE_VALUE)<br />    {<br />        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">can't find %s\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,dir);<br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">;<br />    }<br />    <br />    </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">)<br />    {<br />        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">printf("find %s\n",ffd.cFileName);</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">        FILETIME </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">ft </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> ffd.ftLastWriteTime;<br />        ULARGE_INTEGER ui;<br />        memcpy(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">ui,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">ft,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(ui));<br />        __int64 t </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> now.QuadPart</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">ui.QuadPart;<br />        <br />        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(ffd.dwFileAttributes</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">FILE_ATTRIBUTE_DIRECTORY)<br />        {<br />            </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(strcmp(ffd.cFileName, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">.</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">) </span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000"> strcmp(ffd.cFileName, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">..</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">))<br />            {<br />                </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> temp[</span><span style="COLOR: #000000">1024</span><span style="COLOR: #000000">];<br />                sprintf(temp,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s\\%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,dir,ffd.cFileName);<br />                xdel(temp);<br />                <br />                </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(t</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">nano100SecInDay</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">days)<br />                {<br />                    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(::RemoveDirectory(temp))<br />                    {<br />                        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">del dir %s ok\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,temp);<br />                    }<br />                    </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br />                    {<br />                        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">del dir %s failed\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,temp);<br />                    }<br />                }<br />            }<br />        }<br />        </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br />        {<br />            </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> temp[</span><span style="COLOR: #000000">1024</span><span style="COLOR: #000000">];<br />            sprintf(temp,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s\\%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,dir,ffd.cFileName);<br />            <br />            </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(t</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">nano100SecInDay</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">days)<br />            {<br />                </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(backup)<br />                {<br />                    </span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> backup_file </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> temp;<br />                    backup_file.replace(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,dst_dir.length(),backup_dir.c_str());<br />                    make_sure_dir_exist(backup_file);<br />                    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(::MoveFile(temp,backup_file.c_str()))<br />                    {<br />                        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">backup file %s ok\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,temp);<br />                    }<br />                    </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br />                    {<br />                        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">backup file %s failed\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,temp);<br />                    }<br />                }<br />                </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br />                {<br />                    SetFileAttributes(temp,FILE_ATTRIBUTE_NORMAL);<br />                    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(::DeleteFile(temp))<br />                    {<br />                        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">del file %s ok\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,temp);<br />                    }<br />                    </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br />                    {<br />                        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">del file %s failed\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,temp);<br />                    }<br />                }<br />            }<br />        }<br />        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">FindNextFile(hFind, </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">ffd)) <br />        {<br />            </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br />        }<br />    }<br />    FindClose(hFind);<br />}<br /><br /></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> argc,</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">**</span><span style="COLOR: #000000"> argv)<br />{<br />    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(argc</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)<br />    {<br />        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">usage: xdel directory /d= /m=\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">[optional] /d: config the expired days,default is 14 days\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">[optional] /m: config the backup directory\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); <br />        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">for example: xdel d:\\test /d=10 /m=d:\\backup</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br />    }<br />    <br />    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">argc;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br />    {<br />        </span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> tmp </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> argv[i];<br />        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(tmp.find(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">/d=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">!=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br />        {<br />            </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> d </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> atoi(tmp.substr(</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">).c_str());<br />            </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(d</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br />            {<br />                days </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">d;<br />            }<br />        }<br />        </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(tmp.find(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">/m=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">!=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br />        {<br />            backup </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br />            backup_dir </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tmp.substr(</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">);<br />        }<br />        </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br />        {<br />            dst_dir </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tmp;<br />        }<br />    }<br />    <br />    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Get system time</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    SYSTEMTIME st;<br />    ::GetSystemTime(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">st);<br />    FILETIME f;<br />    ::SystemTimeToFileTime(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">st,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">f);<br />    memcpy(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">now,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">f,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(now));<br />    <br />    xdel(argv[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);<br />    <br />    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br />}</span></div><br /><br /><br /><img src ="http://www.cppblog.com/sandy/aggbug/4725.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-03-29 11:59 <a href="http://www.cppblog.com/sandy/archive/2006/03/29/4725.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最简单的MFC程序</title><link>http://www.cppblog.com/sandy/archive/2006/01/24/3003.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Tue, 24 Jan 2006 04:01:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/01/24/3003.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/3003.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/01/24/3003.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/3003.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/3003.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 不用AppWizard写MFC程序<br>&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2006/01/24/3003.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/3003.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-01-24 12:01 <a href="http://www.cppblog.com/sandy/archive/2006/01/24/3003.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>什么才是多线程安全的</title><link>http://www.cppblog.com/sandy/archive/2006/01/12/2690.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Thu, 12 Jan 2006 09:45:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/01/12/2690.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/2690.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/01/12/2690.html#Feedback</comments><slash:comments>17</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/2690.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/2690.html</trackback:ping><description><![CDATA[对于多线程编程，很多人概念不清，写代码的时候要么是处处加锁，影响性能不说，还容易莫名其妙的死锁，还有人对多线程敬而远之。<BR><BR>所以学习多线程编程最重要的不是学习API,而是理解什么才是多线程安全的代码<BR><BR>从例子说起<BR><BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">windows.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">process.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><BR></SPAN><SPAN style="COLOR: #0000ff">long</SPAN><SPAN style="COLOR: #000000">&nbsp;global1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #0000ff">volatile</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">long</SPAN><SPAN style="COLOR: #000000">&nbsp;global2&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR><BR></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;MyClass<BR>{<BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;MyClass()&nbsp;:&nbsp;m(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">m;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;fun(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;v)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;m</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">v;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-----------9</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;v)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;v;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------------10</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;m;<BR>};<BR><BR>MyClass&nbsp;global_object;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------------8</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000"><BR>unsigned&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;__stdcall&nbsp;thread_fun1(</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">param)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;static2&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;MyClass&nbsp;static_object;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">--------6</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;local1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">local1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------1</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">static2;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------2</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">global1;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------3</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">global2;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------4</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;InterlockedIncrement(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">global1);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">--------5</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;local1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;global_object.fun(local1);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">----------7</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;global_object.</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">(local1);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">---------------11</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>}<BR><BR><BR>unsigned&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;__stdcall&nbsp;thread_fun2(</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">param)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">global1;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------3</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">global2;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------4</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;InterlockedIncrement(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">global1);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">--------5</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;global_object.</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-----------11</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>}<BR><BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;HANDLE&nbsp;thread1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(HANDLE)_beginthreadex(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">thread_fun1,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">thread&nbsp;1</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;HANDLE&nbsp;thread2&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(HANDLE)_beginthreadex(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">thread_fun1,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">thread&nbsp;2</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;HANDLE&nbsp;thread3&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(HANDLE)_beginthreadex(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">thread_fun2,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">thread&nbsp;3</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;WaitForSingleObject(thread1,INFINITE);<BR>&nbsp;&nbsp;&nbsp;&nbsp;WaitForSingleObject(thread2,INFINITE);<BR>&nbsp;&nbsp;&nbsp;&nbsp;WaitForSingleObject(thread3,INFINITE);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>}<BR><BR></SPAN></DIV><BR><BR><STRONG>1.局部变量局部使用是安全的</STRONG><BR>为什么?因为每个thread 都有自己的运行堆栈，而局部变量是生存在堆栈中,大家不干扰。<BR>所以代码1<BR>int local1;<BR>++local1;<BR>是安全的<BR><BR><STRONG>2.全局原生变量多线程读写是不安全的<BR></STRONG>全局变量是在堆(heap)中<BR>long global1 = 0;<BR>++global2;<BR>++这个操作其实分为两部，一个是读，另外一个是写<BR>&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ecx,global<BR>&nbsp;add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ecx,1<BR>&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; global,ecx<BR>所以代码3处是不安全的<BR><BR><STRONG>3.函数静态变量多线程读写也是不安全的<BR></STRONG>道理同2<BR>所以代码2处也是不安全的<BR><BR><STRONG>4.volatile能保证全局整形变量是多线程安全的么</STRONG><BR>不能。<BR>volatile仅仅是告诫compiler不要对这个变量作优化，每次都要从memory取数值，而不是从register<BR>所以代码4也不是安全<BR><BR><STRONG>5.InterlockedIncrement保证整型变量自增的原子性</STRONG><BR>所以代码5是安全的<BR><BR><STRONG>6.function static object的初始化是多线程安全的么</STRONG><BR>不是。<BR>著名的Meyer Singleton其实不是线程安全的<BR>Object &amp; getInstance()<BR>{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp; static Object o;<BR>&nbsp;&nbsp;&nbsp;&nbsp; return o;<BR>}<BR>可能会造成多次初始化对象<BR>所以代码6处是不安全的<BR><BR><STRONG>7.在32机器上，4字节整形一次assign是原子的</STRONG><BR>比如<BR>i =10; //thread1<BR>i=4; //thread2<BR>不会导致i的值处于未知状态,要么是10要么是4<BR><BR>其它的大家自己去体会。<BR><BR>写好多线程安全的法宝就是封装，使数据有保护的被访问到<BR>安全性：<BR>局部变量&gt;成员变量&gt;全局变量<img src ="http://www.cppblog.com/sandy/aggbug/2690.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-01-12 17:45 <a href="http://www.cppblog.com/sandy/archive/2006/01/12/2690.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用PostThreadMessage在Win32线程间传递消息</title><link>http://www.cppblog.com/sandy/archive/2005/12/31/2320.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Sat, 31 Dec 2005 07:31:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2005/12/31/2320.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/2320.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2005/12/31/2320.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/2320.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/2320.html</trackback:ping><description><![CDATA[<P><STRONG>PostThreadMessage</STRONG>的原型是这样的</P>
<P>BOOL PostThreadMessage(&nbsp;DWORD idThread,<BR>&nbsp;&nbsp;&nbsp; UINT Msg,<BR>&nbsp;&nbsp;&nbsp; WPARAM wParam,<BR>&nbsp;&nbsp;&nbsp; LPARAM lParam<BR>);<BR><BR><STRONG>PostThreadMessage</STRONG>可以用于线程之间的异步通讯，因为它不用等待调用者返回，<BR>这也许是线程通讯中最简单的一种方法了。<BR><BR>但是要注意以下问题<BR>1 .PostThreadMessage有时会失败，报1444错误(Invalid thread identifier. )<BR>其实这不一定是线程不存在的原因，也有可能是线程不存在消息队列(message queue)造成的。<BR>事实上，并不是每个thread都有message queue,那如何让thread具有呢？<BR>答案是，至少调用message相关的function一次，比如GetMessage,PeekMessage。<BR><BR>2.如果是post动态分配的memory给另外一个thread,要注意内存的正确释放。<BR><BR>3.PostThreadMessage不能够post WM_COPYDATE之类的同步消息，否则会报错<BR><BR>4.最好不要使用PostThreadMessage post message给一个窗口,使用PostMessage替代。<BR><BR>下面是我写的一个比较严整的例子，仅供参考。<BR></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">windows.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">cstdio</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">process.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><BR></SPAN><SPAN style="COLOR: #0000ff">#define</SPAN><SPAN style="COLOR: #000000">&nbsp;MY_MSG&nbsp;WM_USER+100</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;MAX_INFO_SIZE&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">20</SPAN><SPAN style="COLOR: #000000">;<BR><BR>HANDLE&nbsp;hStartEvent;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;thread&nbsp;start&nbsp;event<BR><BR></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;thread&nbsp;function</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">unsigned&nbsp;__stdcall&nbsp;fun(</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">param)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">thread&nbsp;fun&nbsp;start<IMG src="http://www.cppblog.com/images/dot.gif">\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;MSG&nbsp;msg;<BR>&nbsp;&nbsp;&nbsp;&nbsp;PeekMessage(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">msg,&nbsp;NULL,&nbsp;WM_USER,&nbsp;WM_USER,&nbsp;PM_NOREMOVE);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">!</SPAN><SPAN style="COLOR: #000000">SetEvent(hStartEvent))&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">set&nbsp;thread&nbsp;start&nbsp;event&nbsp;</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">set&nbsp;start&nbsp;event&nbsp;failed,errno:%d\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,::GetLastError());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(GetMessage(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">msg,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">))&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">get&nbsp;msg&nbsp;from&nbsp;message&nbsp;queue</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">switch</SPAN><SPAN style="COLOR: #000000">(msg.message)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">case</SPAN><SPAN style="COLOR: #000000">&nbsp;MY_MSG:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;pInfo&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)msg.wParam;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">recv&nbsp;%s\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,pInfo);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete[]&nbsp;pInfo;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">break</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;};<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;HANDLE&nbsp;hThread;<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;nThreadID;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;hStartEvent&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;::CreateEvent(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,FALSE,FALSE,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">create&nbsp;thread&nbsp;start&nbsp;event</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(hStartEvent&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">create&nbsp;start&nbsp;event&nbsp;failed,errno:%d\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,::GetLastError());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">start&nbsp;thread</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;hThread&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(HANDLE)_beginthreadex(&nbsp;NULL,&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">fun,&nbsp;NULL,&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">nThreadID&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(hThread&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">start&nbsp;thread&nbsp;failed,errno:%d\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,::GetLastError());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(hStartEvent);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">wait&nbsp;thread&nbsp;start&nbsp;event&nbsp;to&nbsp;avoid&nbsp;PostThreadMessage&nbsp;return&nbsp;errno:1444</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;::WaitForSingleObject(hStartEvent,INFINITE);<BR>&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(hStartEvent);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;count&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;pInfo&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">[MAX_INFO_SIZE];&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">create&nbsp;dynamic&nbsp;msg</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf(pInfo,</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">msg_%d</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">count);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">!</SPAN><SPAN style="COLOR: #000000">PostThreadMessage(nThreadID,MY_MSG,(WPARAM)pInfo,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">))</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">post&nbsp;thread&nbsp;msg</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">post&nbsp;message&nbsp;failed,errno:%d\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,::GetLastError());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete[]&nbsp;pInfo;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;::Sleep(</SPAN><SPAN style="COLOR: #000000">1000</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(hThread);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>}</SPAN></DIV>
<P><STRONG><FONT style="BACKGROUND-COLOR: #ffffff" color=#ff0000>===========Happy New Year==============</FONT></STRONG><BR><BR>&nbsp;</P><img src ="http://www.cppblog.com/sandy/aggbug/2320.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2005-12-31 15:31 <a href="http://www.cppblog.com/sandy/archive/2005/12/31/2320.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>为英雄无敌3写个游戏修改器</title><link>http://www.cppblog.com/sandy/archive/2005/12/19/1872.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Mon, 19 Dec 2005 03:37:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2005/12/19/1872.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/1872.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2005/12/19/1872.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/1872.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/1872.html</trackback:ping><description><![CDATA[我是比较铁杆的英雄无敌3的fans,在网上看到这样的文章：<A href="http://game.china.com/zh_cn/play/10002765/20021113/11362720.html">http://game.china.com/zh_cn/play/10002765/20021113/11362720.html</A><BR><BR>就是让我方英雄学会所有技能，真的蛮爽的<BR>学会28项技能修改法<BR><IMG height=644 alt=heroes3.jpg src="http://www.cppblog.com/images/cppblog_com/sandy/heroes3.jpg" width=806 border=0><BR><BR>　　<FONT size=2>首先，把你的英雄的士兵调到前面几格来，接着用FPE来搜索。例如你的士兵数：第一格3，第二格14，第三格4，那么就用 3，0，14，0，4，0 来搜索，就可以找到地址。显示为： <BR>　　士兵种类－ FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF<BR>　　 FF FF FF FF FF FF FF FF FF FF FF FF 03 00 00 00－士兵数量<BR>　　 0E 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 <BR>　　 00 00 00 00 00 00 00 00 01 02 03 04 05 06 07 08<BR>　　 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <BR>　　 25 26 27 28 01 02 03 04 05 06 07 08 00 00 00 00<BR>　　 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <BR>　　 04 00 00 00 00 00 00 00 FF FF FF FF FF FF FF FF<BR>　　 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF<BR>　　 FF或00作为一格，每4格是代表一种属性，物品， 状态等等。前面28格是士兵的种类，接下来28格是士兵的数量。例如: 英雄可带7种士兵，你要每种都是天使，那么把<BR>　　 03 00 00 00 前面的FF FF FF FF 改为0D 00 00 00。 如果你要数量100， 那么把<BR>　　 03 00 00 00 改为64 00 00 00。 士兵的种类是这样分的：00 00 00 00 是枪兵，<BR>　　 01 00 00 00 是 进化一级的枪兵，02 00 00 00 是弓箭手，03 00 00 00 是神箭手。<BR>　　 如此类推。<BR>　　 <BR>　　 从03那一格开始数，28格后就是英雄的技能等级，接着的28格是英雄的技能，<BR>　　 也就是从01到28代表的是英雄的技能等级，技能等级最多只能是3。<BR>　　 01 箭术等级 02 寻路术等级 03 后勤学等级<BR>　　 04 侦察术等级 05 外交术等级 06 航海术等级<BR>　　 07 领导术等级 08 智慧术等级 09 神秘术等级<BR>　　 10 幸运术等级 11 弹道术等级 12 鹰眼术等级<BR>　　 13 招魂术等级 14 理财术等级 15 火系魔法等级<BR>　　 16 气系魔法等级 17 水系魔法等级 18 土系魔法等级<BR>　　 19 学术等级 20 战术等级 21 炮术等级<BR>　　 22 学习能力等级 23 进攻术等级 24 防御术等级 <BR></FONT><BR>但是每次使用FPE真的很麻烦，我又是很懒的人。FPE可以做到的事情，我也可以做到。<BR>其实游戏修改不过是使用<STRONG><FONT color=#0000ff>ReadProcessMemory</FONT></STRONG>和<STRONG><FONT color=#0000ff>WriteProcessMemory</FONT></STRONG><BR><BR>花了半天不断的试验，终于写出一个可以用于英雄无敌3.1版的修改器，没有写界面（I'm lazy-:)），运行之后就是让我方英雄学会所有技能。<BR><BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">windows.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">tlhelp32.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">cstdio</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><BR></SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;MODULE_NAME[]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Heroes3.exe</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><BR></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;printError(&nbsp;TCHAR</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;msg&nbsp;)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;eNum;<BR>&nbsp;&nbsp;&nbsp;&nbsp;TCHAR&nbsp;sysMsg[</SPAN><SPAN style="COLOR: #000000">256</SPAN><SPAN style="COLOR: #000000">];<BR>&nbsp;&nbsp;&nbsp;&nbsp;TCHAR</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;p;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;eNum&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;GetLastError(&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;FormatMessage(&nbsp;FORMAT_MESSAGE_FROM_SYSTEM&nbsp;</SPAN><SPAN style="COLOR: #000000">|</SPAN><SPAN style="COLOR: #000000">&nbsp;FORMAT_MESSAGE_IGNORE_INSERTS,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL,&nbsp;eNum,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MAKELANGID(LANG_NEUTRAL,&nbsp;SUBLANG_DEFAULT),&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Default&nbsp;language</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sysMsg,&nbsp;</SPAN><SPAN style="COLOR: #000000">256</SPAN><SPAN style="COLOR: #000000">,&nbsp;NULL&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Trim&nbsp;the&nbsp;end&nbsp;of&nbsp;the&nbsp;line&nbsp;and&nbsp;terminate&nbsp;it&nbsp;with&nbsp;a&nbsp;null</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;sysMsg;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000">(&nbsp;(&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">p&nbsp;</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">31</SPAN><SPAN style="COLOR: #000000">&nbsp;)&nbsp;</SPAN><SPAN style="COLOR: #000000">||</SPAN><SPAN style="COLOR: #000000">&nbsp;(&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">p&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">9</SPAN><SPAN style="COLOR: #000000">&nbsp;)&nbsp;)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">p;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">do</SPAN><SPAN style="COLOR: #000000">&nbsp;{&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">p</SPAN><SPAN style="COLOR: #000000">--</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;&nbsp;}&nbsp;</SPAN><SPAN style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000">(&nbsp;(&nbsp;p&nbsp;</SPAN><SPAN style="COLOR: #000000">&gt;=</SPAN><SPAN style="COLOR: #000000">&nbsp;sysMsg&nbsp;)&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;&amp;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&nbsp;(&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">p&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">.</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">&nbsp;)&nbsp;</SPAN><SPAN style="COLOR: #000000">||</SPAN><SPAN style="COLOR: #000000">&nbsp;(&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">p&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">33</SPAN><SPAN style="COLOR: #000000">&nbsp;)&nbsp;)&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Display&nbsp;the&nbsp;message</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">WARNING:&nbsp;%s&nbsp;failed&nbsp;with&nbsp;error&nbsp;%d&nbsp;(%s)\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;msg,&nbsp;eNum,&nbsp;sysMsg&nbsp;);<BR>}<BR><BR><BR>DWORD&nbsp;findProcessId(</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">module)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;result&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;HANDLE&nbsp;hProcessSnap;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Take&nbsp;a&nbsp;snapshot&nbsp;of&nbsp;all&nbsp;processes&nbsp;in&nbsp;the&nbsp;system.</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;hProcessSnap&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;CreateToolhelp32Snapshot(&nbsp;TH32CS_SNAPPROCESS,&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(&nbsp;hProcessSnap&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;INVALID_HANDLE_VALUE&nbsp;)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printError(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">CreateToolhelp32Snapshot&nbsp;(of&nbsp;processes)</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;result;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;PROCESSENTRY32&nbsp;pe32;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Set&nbsp;the&nbsp;size&nbsp;of&nbsp;the&nbsp;structure&nbsp;before&nbsp;using&nbsp;it.</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;pe32.dwSize&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(&nbsp;PROCESSENTRY32&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Retrieve&nbsp;information&nbsp;about&nbsp;the&nbsp;first&nbsp;process,<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;and&nbsp;exit&nbsp;if&nbsp;unsuccessful</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(&nbsp;</SPAN><SPAN style="COLOR: #000000">!</SPAN><SPAN style="COLOR: #000000">Process32First(&nbsp;hProcessSnap,&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">pe32&nbsp;)&nbsp;)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printError(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Process32First</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;);&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Show&nbsp;cause&nbsp;of&nbsp;failure</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(&nbsp;hProcessSnap&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Must&nbsp;clean&nbsp;up&nbsp;the&nbsp;snapshot&nbsp;object!</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;result;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Now&nbsp;walk&nbsp;the&nbsp;snapshot&nbsp;of&nbsp;processes,&nbsp;and<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;display&nbsp;information&nbsp;about&nbsp;each&nbsp;process&nbsp;in&nbsp;turn</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">do</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(stricmp(pe32.szExeFile,module)</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">find&nbsp;process:&nbsp;&nbsp;%s\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;module&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;pe32.th32ProcessID;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">break</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</SPAN><SPAN style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000">(&nbsp;Process32Next(&nbsp;hProcessSnap,&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">pe32&nbsp;)&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(&nbsp;hProcessSnap&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;result;<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;argc,</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">argv[])<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;h3pid&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;h3pid&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;findProcessId(MODULE_NAME);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(h3pid&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">can't&nbsp;find&nbsp;%s&nbsp;in&nbsp;memory,please&nbsp;make&nbsp;sure&nbsp;the&nbsp;program&nbsp;started!\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,MODULE_NAME);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;HANDLE&nbsp;h3&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;OpenProcess(&nbsp;PROCESS_ALL_ACCESS,&nbsp;FALSE,&nbsp;h3pid&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(&nbsp;h3&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;NULL&nbsp;)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printError(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">OpenProcess</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</SPAN><SPAN style="COLOR: #0000ff">long</SPAN><SPAN style="COLOR: #000000">&nbsp;sideOffset</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">0x824994</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;side&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0xff</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">!</SPAN><SPAN style="COLOR: #000000">ReadProcessMemory(h3,(LPCVOID)sideOffset,</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">side,</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)) //查找我方的颜色<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printError(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">ReadProcessMemory</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(side</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">0xff</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">find&nbsp;current&nbsp;side:%d\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">)side);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">can't&nbsp;find&nbsp;current&nbsp;side\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;side&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</SPAN><SPAN style="COLOR: #0000ff">long</SPAN><SPAN style="COLOR: #000000">&nbsp;heroBaseAddress&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0x15216ab</SPAN><SPAN style="COLOR: #000000">;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">hero&nbsp;name&nbsp;start</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;name[</SPAN><SPAN style="COLOR: #000000">20</SPAN><SPAN style="COLOR: #000000">]</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">{</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">};<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</SPAN><SPAN style="COLOR: #0000ff">long</SPAN><SPAN style="COLOR: #000000">&nbsp;temp&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;heroBaseAddress</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;b[</SPAN><SPAN style="COLOR: #000000">28</SPAN><SPAN style="COLOR: #000000">]; //28种技能<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;size&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(b);<BR>&nbsp;&nbsp;&nbsp;&nbsp;memset(b,</SPAN><SPAN style="COLOR: #000000">3</SPAN><SPAN style="COLOR: #000000">,size);<BR>&nbsp;&nbsp;&nbsp;&nbsp;b[</SPAN><SPAN style="COLOR: #000000">12</SPAN><SPAN style="COLOR: #000000">]</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;//不学招魂术<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;i</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;i</SPAN><SPAN style="COLOR: #000000">&lt;=</SPAN><SPAN style="COLOR: #000000">155</SPAN><SPAN style="COLOR: #000000">;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">i) //一共156个Hero<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">!</SPAN><SPAN style="COLOR: #000000">ReadProcessMemory(h3,(LPCVOID)temp,name,</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(name),</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printError(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">ReadProcessMemory</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(name[</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">]</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">side)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">find:%s\t</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,name</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">!</SPAN><SPAN style="COLOR: #000000">WriteProcessMemory(h3,(LPVOID)(temp</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">0xA7</SPAN><SPAN style="COLOR: #000000">),b,size,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printError(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">WriteProcessMemory</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">update&nbsp;skill&nbsp;sucess!\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp&nbsp;</SPAN><SPAN style="COLOR: #000000">+=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0x492</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(h3);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>}</SPAN></DIV>
<P><BR>附加:<BR>让我方英雄所有英雄学会除了招魂术的以外的27种技能的小程序</P>
<P>以前英雄世界也有一个这样的程序，但是版本太老，不能用了。</P>
<P>使用版本:中文版3.1,其他版本没有测试</P>
<P>使用方法:进入游戏后，运行程序</P>
<P>Download: http://www.cppblog.com/Files/sandy/h3c.zip</P><img src ="http://www.cppblog.com/sandy/aggbug/1872.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2005-12-19 11:37 <a href="http://www.cppblog.com/sandy/archive/2005/12/19/1872.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Nothing is impossible:自删除的程序</title><link>http://www.cppblog.com/sandy/archive/2005/12/07/1580.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Wed, 07 Dec 2005 02:21:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2005/12/07/1580.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/1580.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2005/12/07/1580.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/1580.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/1580.html</trackback:ping><description><![CDATA[记得以前在程序员杂志上面，看见有人提到这个问题，试了很多种方法，结果是没办法将程序删除。<BR><BR>真的没办法删除自身么？<BR>请运行下面的代码：<BR><BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">windows.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">shlobj.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><BR>BOOL&nbsp;SelfDelete()<BR>{<BR>&nbsp;&nbsp;SHELLEXECUTEINFO&nbsp;sei;<BR>&nbsp;&nbsp;TCHAR&nbsp;szModule&nbsp;[MAX_PATH],&nbsp;szComspec[MAX_PATH],&nbsp;szParams&nbsp;[MAX_PATH];<BR><BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;get&nbsp;file&nbsp;path&nbsp;names:</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">((GetModuleFileName(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,szModule,MAX_PATH)</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;&amp;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(GetShortPathName(szModule,szModule,MAX_PATH)</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;&amp;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(GetEnvironmentVariable(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">COMSPEC</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,szComspec,MAX_PATH)</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">))<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;set&nbsp;command&nbsp;shell&nbsp;parameters</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;lstrcpy(szParams,</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">/c&nbsp;del&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;lstrcat(szParams,&nbsp;szModule);<BR>&nbsp;&nbsp;&nbsp;&nbsp;lstrcat(szParams,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;&gt;&nbsp;nul</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;set&nbsp;struct&nbsp;members</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;sei.cbSize&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(sei);<BR>&nbsp;&nbsp;&nbsp;&nbsp;sei.hwnd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;sei.lpVerb&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Open</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;sei.lpFile&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;szComspec;<BR>&nbsp;&nbsp;&nbsp;&nbsp;sei.lpParameters&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;szParams;<BR>&nbsp;&nbsp;&nbsp;&nbsp;sei.lpDirectory&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;sei.nShow&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;SW_HIDE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;sei.fMask&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;SEE_MASK_NOCLOSEPROCESS;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;increase&nbsp;resource&nbsp;allocation&nbsp;to&nbsp;program</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;SetPriorityClass(GetCurrentProcess(),<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;REALTIME_PRIORITY_CLASS);<BR>&nbsp;&nbsp;&nbsp;&nbsp;SetThreadPriority(GetCurrentThread(),<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;THREAD_PRIORITY_TIME_CRITICAL);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;invoke&nbsp;command&nbsp;shell</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(ShellExecuteEx(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">sei))<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;suppress&nbsp;command&nbsp;shell&nbsp;process&nbsp;until&nbsp;program&nbsp;exits</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetPriorityClass(sei.hProcess,IDLE_PRIORITY_CLASS);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetProcessPriorityBoost(sei.hProcess,TRUE);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;notify&nbsp;explorer&nbsp;shell&nbsp;of&nbsp;deletion</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SHChangeNotify(SHCNE_DELETE,SHCNF_PATH,szModule,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;TRUE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;if&nbsp;error,&nbsp;normalize&nbsp;allocation</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetPriorityClass(GetCurrentProcess(),<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NORMAL_PRIORITY_CLASS);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetThreadPriority(GetCurrentThread(),<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;THREAD_PRIORITY_NORMAL);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;FALSE;<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;APIENTRY&nbsp;WinMain(HINSTANCE&nbsp;hInstance,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HINSTANCE&nbsp;hPrevInstance,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LPSTR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpCmdLine,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nCmdShow)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;on&nbsp;program&nbsp;exit<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;close&nbsp;all&nbsp;handles&nbsp;etc.</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">!</SPAN><SPAN style="COLOR: #000000">SelfDelete())<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;add&nbsp;error&nbsp;messaging</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;WinMain&nbsp;exit</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;}<BR><BR></SPAN></DIV><BR><STRONG><FONT color=#0000ff size=2>程序的思想是通过创建一个另外的进程(ShellExecuteEx)，再赋予本进程比较高的权限(SetPriorityClass),<BR>等这个程序退出以后，那个杀进程的进程就可以删除程序了，另外程序通过</FONT></STRONG><FONT size=2><STRONG><FONT color=#0000ff>SHChangeNotify通知Explorer:程序被删除。<BR></FONT></STRONG><BR>具体API的使用方法请看MSDN.<BR><BR>ps:这个程序是我在老外的网站上找到的，不是我写的。我在VC6,Win2000 Professional上面调试通过<BR><BR></FONT><img src ="http://www.cppblog.com/sandy/aggbug/1580.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2005-12-07 10:21 <a href="http://www.cppblog.com/sandy/archive/2005/12/07/1580.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Colorful life:让控制台五颜六色</title><link>http://www.cppblog.com/sandy/archive/2005/12/05/1537.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Mon, 05 Dec 2005 02:49:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2005/12/05/1537.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/1537.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2005/12/05/1537.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/1537.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/1537.html</trackback:ping><description><![CDATA[当我回想起来我刚刚学习C语言，Turbo C2.0提供的丰富的函数，可以让枯燥的文本界面，显示出花花绿绿的文字界面。在windows时代，这些函数都不在标准库中。不过WINAPI可以帮你实现。<BR><BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">windows.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">ctime</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><BR></SPAN><SPAN style="COLOR: #0000ff">enum</SPAN><SPAN style="COLOR: #000000">&nbsp;Colors<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;BLACK&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;BLUE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;DARK_GREEN&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;LIGHT_BLUE&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">3</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;RED&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">4</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;PURPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">5</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;ORANGE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">6</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;GREY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">7</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;DARKER_GREY&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">8</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;MEDIUM_BLUE&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">9</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;LIGHT_GREEN&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;TEAL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">11</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;RED_ORANGE&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">12</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;LIGHT_PURPLE&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">13</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;YELLOW&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">14</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;WHITE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">15</SPAN><SPAN style="COLOR: #000000"><BR>};<BR><BR></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;set_cursor(</SPAN><SPAN style="COLOR: #0000ff">short</SPAN><SPAN style="COLOR: #000000">&nbsp;x,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">short</SPAN><SPAN style="COLOR: #000000">&nbsp;y)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;COORD&nbsp;point&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;{x,&nbsp;y};<BR>&nbsp;&nbsp;&nbsp;&nbsp;::SetConsoleCursorPosition(::GetStdHandle(STD_OUTPUT_HANDLE),&nbsp;point);<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;set_color(unsigned&nbsp;</SPAN><SPAN style="COLOR: #0000ff">short</SPAN><SPAN style="COLOR: #000000">&nbsp;color)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),&nbsp;color);<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;delay(unsigned&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;delay)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;::Sleep(delay);<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;set_title(std::</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;title)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;::SetConsoleTitle(title.c_str());<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;show_cursor(</SPAN><SPAN style="COLOR: #0000ff">bool</SPAN><SPAN style="COLOR: #000000">&nbsp;show,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;size&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">25</SPAN><SPAN style="COLOR: #000000">)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;CONSOLE_CURSOR_INFO&nbsp;cci;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(size&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)&nbsp;size&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(size&nbsp;</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">100</SPAN><SPAN style="COLOR: #000000">)&nbsp;size&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">100</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;cci.dwSize&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;size;<BR>&nbsp;&nbsp;&nbsp;&nbsp;cci.bVisible&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;show;<BR>&nbsp;&nbsp;&nbsp;&nbsp;::SetConsoleCursorInfo(::GetStdHandle(STD_OUTPUT_HANDLE),&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">cci);<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;clear_screen()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;system(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">cls</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>}</SPAN></DIV><BR>代码很简单，不用多作解释了<img src ="http://www.cppblog.com/sandy/aggbug/1537.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2005-12-05 10:49 <a href="http://www.cppblog.com/sandy/archive/2005/12/05/1537.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>