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

<channel>
	<title>Kernel2Heart &#187; C++</title>
	<atom:link href="http://www.kernel2heart.com/labels/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kernel2heart.com</link>
	<description>一个人的内心里,究竟隐藏了什么呢?</description>
	<lastBuildDate>Fri, 20 May 2011 05:10:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>C#获取本机CPU、内存信息类</title>
		<link>http://www.kernel2heart.com/2010/08/c-sharp-get-pc-cpu-memory-class/</link>
		<comments>http://www.kernel2heart.com/2010/08/c-sharp-get-pc-cpu-memory-class/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 13:27:53 +0000</pubDate>
		<dc:creator>janxin</dc:creator>
				<category><![CDATA[技术交流]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.kernel2heart.com/2010/08/c-sharp-get-pc-cpu-memory-class/</guid>
		<description><![CDATA[代码写的很烂，多多包涵就是了，用起来感觉还可以，需要几个using： using System.Runtime.InteropServices; using System.Management; 其中System.Management需要手工添加，发布时，需要附带System.Management.dll。 public class SystemInfo &#123; &#160; &#91;StructLayout&#40;LayoutKind.Sequential&#41;&#93; private struct MEMORY_INFO &#123; public uint dwLength; public uint dwMemoryLoad; public uint dwTotalPhys; public uint dwAvailPhys; public uint dwTotalPageFile; public uint dwAvailPageFile; public uint dwTotalVirtual; public uint dwAvailVirtual; &#8230; <a href="http://www.kernel2heart.com/2010/08/c-sharp-get-pc-cpu-memory-class/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>代码写的很烂，多多包涵就是了，用起来感觉还可以，需要几个using：</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Runtime.InteropServices</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Management</span><span style="color: #008000;">;</span></pre></div></div>

<p>其中System.Management需要手工添加，发布时，需要附带System.Management.dll。</p>
<p><span id="more-461"></span></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> SystemInfo
    <span style="color: #008000;">&#123;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>StructLayout<span style="color: #008000;">&#40;</span>LayoutKind<span style="color: #008000;">.</span><span style="color: #0000FF;">Sequential</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">struct</span> MEMORY_INFO
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">uint</span> dwLength<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">uint</span> dwMemoryLoad<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">uint</span> dwTotalPhys<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">uint</span> dwAvailPhys<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">uint</span> dwTotalPageFile<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">uint</span> dwAvailPageFile<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">uint</span> dwTotalVirtual<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">uint</span> dwAvailVirtual<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;kernel32&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">void</span> GlobalMemoryStatus<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span>  MEMORY_INFO meminfo<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> m_ProcessorCount <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>   <span style="color: #008080; font-style: italic;">//CPU个数</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> PerformanceCounter pcCpuLoad<span style="color: #008000;">;</span>   <span style="color: #008080; font-style: italic;">//CPU计数器</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">long</span> m_PhysicalMemory <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>   <span style="color: #008080; font-style: italic;">//物理内存</span>
&nbsp;
        <span style="color: #008080;">#region 构造函数</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> SystemInfo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">//初始化CPU计数器</span>
            pcCpuLoad <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PerformanceCounter<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Processor&quot;</span>, <span style="color: #666666;">&quot;% Processor Time&quot;</span>, <span style="color: #666666;">&quot;_Total&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            pcCpuLoad<span style="color: #008000;">.</span><span style="color: #0000FF;">MachineName</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;.&quot;</span><span style="color: #008000;">;</span>
            pcCpuLoad<span style="color: #008000;">.</span><span style="color: #0000FF;">NextValue</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//CPU个数</span>
            m_ProcessorCount <span style="color: #008000;">=</span> Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">ProcessorCount</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//获得物理内存</span>
            ManagementClass mc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ManagementClass<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Win32_ComputerSystem&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            ManagementObjectCollection moc <span style="color: #008000;">=</span> mc<span style="color: #008000;">.</span><span style="color: #0000FF;">GetInstances</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>ManagementObject mo <span style="color: #0600FF; font-weight: bold;">in</span> moc<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>mo<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;TotalPhysicalMemory&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    m_PhysicalMemory <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">long</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>mo<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;TotalPhysicalMemory&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region CPU个数</span>
        <span style="color: #008080; font-style: italic;">///</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// 获取CPU个数</span>
        <span style="color: #008080; font-style: italic;">/// </span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> ProcessorCount
        <span style="color: #008000;">&#123;</span>
            get
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> m_ProcessorCount<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region CPU占用率</span>
        <span style="color: #008080; font-style: italic;">///</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// 获取CPU占用率</span>
        <span style="color: #008080; font-style: italic;">/// </span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">float</span> CpuLoad
        <span style="color: #008000;">&#123;</span>
            get
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> pcCpuLoad<span style="color: #008000;">.</span><span style="color: #0000FF;">NextValue</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region 可用内存</span>
        <span style="color: #008080; font-style: italic;">///</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// 获取可用内存</span>
        <span style="color: #008080; font-style: italic;">/// </span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">long</span> MemoryAvailable
        <span style="color: #008000;">&#123;</span>
            get
            <span style="color: #008000;">&#123;</span>
                <span style="color: #6666cc; font-weight: bold;">long</span> availablebytes <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                ManagementClass mos <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ManagementClass<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Win32_OperatingSystem&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>ManagementObject mo <span style="color: #0600FF; font-weight: bold;">in</span> mos<span style="color: #008000;">.</span><span style="color: #0000FF;">GetInstances</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>mo<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;FreePhysicalMemory&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #008000;">&#123;</span>
                        availablebytes <span style="color: #008000;">=</span> <span style="color: #FF0000;">1024</span> <span style="color: #008000;">*</span> <span style="color: #6666cc; font-weight: bold;">long</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>mo<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;FreePhysicalMemory&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #008000;">&#125;</span>
                <span style="color: #008000;">&#125;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>availablebytes <span style="color: #008000;">/</span> <span style="color: #FF0000;">1024</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">1024</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region 物理内存</span>
        <span style="color: #008080; font-style: italic;">///</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// 获取物理内存</span>
        <span style="color: #008080; font-style: italic;">/// </span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> PhysicalMemory
        <span style="color: #008000;">&#123;</span>
            get
            <span style="color: #008000;">&#123;</span>
                <span style="color: #6666cc; font-weight: bold;">double</span> temp <span style="color: #008000;">=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Round</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>m_PhysicalMemory <span style="color: #008000;">/</span> <span style="color: #FF0000;">1024</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">1024</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">1024</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>temp<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region 物理内存占用率</span>
        <span style="color: #008080; font-style: italic;">///</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// 获取物理内存占用率</span>
        <span style="color: #008080; font-style: italic;">/// </span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">uint</span> PhysicalMemoryLoad
        <span style="color: #008000;">&#123;</span>
            get
            <span style="color: #008000;">&#123;</span>
                MEMORY_INFO MemInfo <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MEMORY_INFO<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                GlobalMemoryStatus<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span>  MemInfo<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> MemInfo<span style="color: #008000;">.</span><span style="color: #0000FF;">dwMemoryLoad</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p><script type='text/javascript'>
alimama_pid='mm_14356979_2041853_8949568';
alimama_type='g';
alimama_tks={};
alimama_tks.style_i=2;
alimama_tks.lg_i=1;
alimama_tks.w_i=572;
alimama_tks.h_i=69;
alimama_tks.btn_i=1;
alimama_tks.txt_s='';
alimama_tks.hot_i=1;
alimama_tks.hc_c='#0065FF';
alimama_tks.cid_i=0;
alimama_tks.t_i=1
</script><br />
<script type='text/javascript' src='http://a.alimama.cn/inf.js'></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kernel2heart.com/2010/08/c-sharp-get-pc-cpu-memory-class/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>一个小细节：Security Enhancements in the CRT</title>
		<link>http://www.kernel2heart.com/2009/07/security-enhancements-in-the-crt/</link>
		<comments>http://www.kernel2heart.com/2009/07/security-enhancements-in-the-crt/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 13:41:52 +0000</pubDate>
		<dc:creator>janxin</dc:creator>
				<category><![CDATA[技术交流]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.kernel2heart.com/2009/07/security-enhancements-in-the-crt/</guid>
		<description><![CDATA[最近写点小程序，为了安全性，打算改写部分CRT函数成secure versions of run-time library routines。最初的想法是，对于这类函数，一般就是加上_s，有的需要加个参数就可以了。没什么其他的需要修改。前段时间改了一个程序，因为写的不是很标准，就出了一点小问题。拿一个来说说： 1 2 3 4 5 &#40;&#40;_waccess&#40;strVirName,0&#41;&#41; != -1&#41; &#123; //ShowMessage(TEXT(&#34;There is a virus in here!&#34;),TEXT(&#34;IsExist&#34;)); return true; &#125; 这样是一定会在改写成_waccess_s时出现问题的。最初我以为是_waccess_s的定义问题，查了下msdn，是下面这个样子： 1 2 3 4 5 6 7 8 int _waccess&#40; const wchar_t *path, int mode &#8230; <a href="http://www.kernel2heart.com/2009/07/security-enhancements-in-the-crt/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>最近写点小程序，为了安全性，打算改写部分CRT函数成secure versions of run-time library routines。最初的想法是，对于这类函数，一般就是加上_s，有的需要加个参数就可以了。没什么其他的需要修改。前段时间改了一个程序，因为写的不是很标准，就出了一点小问题。拿一个来说说：</p>
<p><span id="more-82"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>_waccess<span style="color: #008000;">&#40;</span>strVirName,<span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">//ShowMessage(TEXT(&quot;There is a virus in here!&quot;),TEXT(&quot;IsExist&quot;));</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>这样是一定会在改写成_waccess_s时出现问题的。最初我以为是_waccess_s的定义问题，查了下msdn，是下面这个样子：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> _waccess<span style="color: #008000;">&#40;</span>
   <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">wchar_t</span> <span style="color: #000040;">*</span>path,
   <span style="color: #0000ff;">int</span> mode
<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
errno_t _waccess_s<span style="color: #008000;">&#40;</span>
   <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">wchar_t</span> <span style="color: #000040;">*</span>path,
   <span style="color: #0000ff;">int</span> mode
<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>这两个函数在参数上没什么差别，貌似问题并不出在这里。看编译器并不在这里提示错误，但是最后逻辑一定会有错误。不过仔细看下类型，一个是int，一个是errno_t，这个不太一样。但是实际上你看下errno_t的定义就知道，其实这个是tpyedef int errno_t来的。好像程序分析到这里陷入到了僵局。不过注意到_waccess定义中有一句话：</p>
<blockquote><p>Each function returns 0 if the file has the given mode. The function returns –1 if the named file does not exist or does not have the given mode; in this case, <strong>errno</strong> is set as shown in the following table.</p></blockquote>
<p>从这句话看，我这原来的程序在这里没有任何问题，修改后应该也不会出现什么问题，但是实际上却不然。在_waccess_t的返回值中是这么提到的：</p>
<blockquote><p>Each function returns 0 if the file has the given mode. The function returns an error code if the named file does not exist or is not accessible in the given mode. In this case, the function returns an error code from the set as follows and also sets <strong>errno</strong> to the same value.</p></blockquote>
<p>这就说明，实际上在_wacess_s中-1并不能在这里判定文件不存在，因为在_waccess_s中不会出现这个返回值，仅会出现EACCES、ENOENT、EINVAL三者之中的一个。因此实际上将最初语句中的-1替换为ENOENT就可以解决这个问题了。</p>
<p>实际上，这个问题也是很有提示作用的，这说明在写程序的时候，还是需要用些定义的了东西，不仅在移植的时候方便，而且更新的时候不需要具体的调整参数了，避免一些莫名其妙的错误。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kernel2heart.com/2009/07/security-enhancements-in-the-crt/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

