﻿<?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++博客-zoj</title><link>http://www.cppblog.com/zoj/</link><description>海纳百川，有容乃大；壁立千仞，无欲则刚</description><language>zh-cn</language><lastBuildDate>Sun, 05 Apr 2026 22:48:13 GMT</lastBuildDate><pubDate>Sun, 05 Apr 2026 22:48:13 GMT</pubDate><ttl>60</ttl><item><title>漫卷诗书喜欲狂</title><link>http://www.cppblog.com/zoj/archive/2006/02/27/3547.html</link><dc:creator>zoj</dc:creator><author>zoj</author><pubDate>Mon, 27 Feb 2006 06:52:00 GMT</pubDate><guid>http://www.cppblog.com/zoj/archive/2006/02/27/3547.html</guid><wfw:comment>http://www.cppblog.com/zoj/comments/3547.html</wfw:comment><comments>http://www.cppblog.com/zoj/archive/2006/02/27/3547.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/zoj/comments/commentRss/3547.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zoj/services/trackbacks/3547.html</trackback:ping><description><![CDATA[<FONT style="BACKGROUND-COLOR: #d3d3d3">考研分数终于在焦急而又忐忑的等待中下来了<BR>中午手机铃声大作，打开短信，只有六个字——分数可以查了，顿时心狂跳不止。午饭匆匆扒了几口，赶忙做到电脑前，熟练的打开网页。突然间犹豫了，三年前高考查分的一幕又在眼前恍现，三年前的失败使我对查分产生了一种不可名状的恐惧。每次查分必将作一次激烈的思想斗争，检讨自己最近有没有作什么不对劲的事，害怕由于冒犯冥冥之中的神灵而使自己的分数诡异的变的不堪回首。<BR>终究还是打开了网页，终究还是输入了准考证，网速以超乎想象的速度将一份雅丽的成绩单呈现在我的面前<BR>令人惊喜的分数，410分，我做梦也没想到会是这么高的分数。狂喜！！！无言以表自己的心情<BR>我一年的心血没有白费阿……<BR>下面就该考虑一下自己的事了，争取在这一年的时间内，自己能在黑客技术有所长进，自己还是个门外汉阿，还没入道啊。不急不急，这事不是急得，关键是潜心研究，宝剑锋从磨砺出，我终可以铸成自己的一天宝剑！！<BR><IMG height=1 src="/zoj/admin/FtbWebResource.axd?a=FreeTextBox%2c+Version%3d3.1.1.34886%2c+Culture%3dneutral%2c+PublicKeyToken%3d5962a4e684a48b87&amp;r=FreeTextBoxControls.Resources.Images.Utility.spacer.gif&amp;t=632699064217187500" width=1><IMG height=1 src="/zoj/admin/FtbWebResource.axd?a=FreeTextBox%2c+Version%3d3.1.1.34886%2c+Culture%3dneutral%2c+PublicKeyToken%3d5962a4e684a48b87&amp;r=FreeTextBoxControls.Resources.Images.Utility.spacer.gif&amp;t=632699064217187500" width=1><BR></FONT><img src ="http://www.cppblog.com/zoj/aggbug/3547.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zoj/" target="_blank">zoj</a> 2006-02-27 14:52 <a href="http://www.cppblog.com/zoj/archive/2006/02/27/3547.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原来MFC程序读取写放注册表就这么简单 </title><link>http://www.cppblog.com/zoj/archive/2006/02/16/3289.html</link><dc:creator>zoj</dc:creator><author>zoj</author><pubDate>Thu, 16 Feb 2006 08:38:00 GMT</pubDate><guid>http://www.cppblog.com/zoj/archive/2006/02/16/3289.html</guid><wfw:comment>http://www.cppblog.com/zoj/comments/3289.html</wfw:comment><comments>http://www.cppblog.com/zoj/archive/2006/02/16/3289.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zoj/comments/commentRss/3289.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zoj/services/trackbacks/3289.html</trackback:ping><description><![CDATA[<TABLE class="fixedTable blogpost" cellSpacing=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=ellipse><SPAN class=bvTitle id=subjcns!7CEECED6FB4F9318!158><STRONG></STRONG></SPAN></TD></TR>
<TR>
<TD class=bvh8><STRONG></STRONG></TD></TR>
<TR>
<TD id=msgcns!7CEECED6FB4F9318!158>
<DIV>
<DIV><A href="http://blog.vckbase.com/zaboli/archive/2005/03/03/3355.html"><U><FONT color=#800080>原来MFC程序读取写放注册表就这么简单~</FONT></U></A> </DIV>
<DIV>MFC的<BR>CWinApp 类提供了很容易的注册表访问函数~~以前从来没注意过~~还到处找读写注册表的办法~~ -_-! 看下面几个成员函数~ <BR><BR>
<TABLE cellSpacing=0>
<TBODY>
<TR vAlign=top>
<TD width="41%"><A><U><FONT color=#0000ff>SetRegistryKey</FONT></U></A></TD>
<TD width="59%">Causes application settings to be stored in the registry instead of .INI files.</TD></TR></TBODY></TABLE>
<P>SetRegistryKey 这个函数功能是设置MFC程序的注册表访问键，并把读写 ini 文件的成员函数映射到读写注册表。只要调用一下 SetRegistryKey 并指定注册表键值，那么下面6个成员函数，就被映射到进行注册表读取了~<BR><BR>
<TABLE cellSpacing=0>
<TBODY>
<TR vAlign=top>
<TD width="41%"><A><U><FONT color=#0000ff>WriteProfileBinary</FONT></U></A></TD>
<TD width="59%">Writes binary data to an entry in the application's .INI file.</TD></TR>
<TR vAlign=top>
<TD width="41%"><A><U><FONT color=#0000ff>WriteProfileInt</FONT></U></A></TD>
<TD width="59%">Writes an integer to an entry in the application's .INI file.</TD></TR>
<TR vAlign=top>
<TD width="41%"><A><U><FONT color=#0000ff>WriteProfileString</FONT></U></A></TD>
<TD width="59%">Writes a string to an entry in the application's .INI&nbsp;file.</TD></TR></TBODY></TABLE><BR>
<TABLE cellSpacing=0>
<TBODY>
<TR vAlign=top>
<TD width="41%"><A><U><FONT color=#0000ff>GetProfileBinary</FONT></U></A></TD>
<TD width="59%">Retrieves binary data from an entry in the application's .INI file.</TD></TR>
<TR vAlign=top>
<TD width="41%"><A><U><FONT color=#0000ff>GetProfileInt</FONT></U></A></TD>
<TD width="59%">Retrieves an integer from an entry in the application's .INI file.</TD></TR>
<TR vAlign=top>
<TD width="41%"><A><U><FONT color=#0000ff>GetProfileString</FONT></U></A></TD>
<TD width="59%">Retrieves a string from an entry in the application's .INI file.</TD></TR></TBODY></TABLE><BR>MSDN上面写上面6个函数是写到INI文件的。所以俺就忽略了其访问注册表的功能。无意中看了其MFC实现才有所了解。<BR><BR>例子如下：<BR>SetRegistryKey(_T("boli's app")); //这里是准备在注册表HKEY_CURRENT_USER\\software 下面生成一个boli's app 分支~为什么说是准备呢？因为如果不调用相关函数，如上面提到的6个函数，它是不会真正读写注册表的。具体本文最最下面的MFC实现摘录。<BR>CString strUserName,strPassword;<BR>WriteProfileString("LogInfo","UserName",strUserName); //向注册表HKEY_CURRENT_USER\\software\\boli's app\\LogInfo\\分支下写入 UserName 字符串行键值~<BR>WriteProfileString("LogInfo","Password",strPassword);//同上~<BR><BR>&nbsp;strUserName = GetProfileString("LogInfo","UserName");// 这里是读取HKEY_CURRENT_USER\\software\\boli's app\\LogInfo\\分支下的 UserName 字符串键值到 strUserName~<BR>&nbsp;strPassword =&nbsp; GetProfileString("LogInfo","Password"); <BR><BR><FONT color=#ff3333>如果不是在CWinApp 派生的类中读写注册表，可以直接用：<BR>&nbsp;strUserName = theApp.GetProfileString("LogInfo","UserName");<BR>&nbsp;strPassword = theApp.GetProfileString("LogInfo","Password");<BR>或 <BR>&nbsp;strUserName = AfxGetApp()-&gt;GetProfileString("LogInfo","UserName");<BR>条条大路通罗马。</FONT><BR><BR><BR>看看MFC的代码吧~~SDK高手们不屑MFC，但有时候看看MFC的代码会到学习SDK，windows api有很大的帮助~ ：P<BR>////////////////////////////////////////////////////////////////////////////<BR>// CWinApp Settings Helpers</P>
<P>#ifdef AFX_INIT_SEG<BR>#pragma code_seg(AFX_INIT_SEG)<BR>#endif</P>
<P>void CWinApp::SetRegistryKey(LPCTSTR lpszRegistryKey)<BR>{<BR>&nbsp;ASSERT(m_pszRegistryKey == NULL);<BR>&nbsp;ASSERT(lpszRegistryKey != NULL);<BR>&nbsp;ASSERT(m_pszAppName != NULL);</P>
<P>&nbsp;BOOL bEnable = AfxEnableMemoryTracking(FALSE);<BR>&nbsp;free((void*)m_pszRegistryKey);<BR>&nbsp;m_pszRegistryKey = _tcsdup(lpszRegistryKey);<BR>&nbsp;free((void*)m_pszProfileName);<BR>&nbsp;m_pszProfileName = _tcsdup(m_pszAppName);<BR>&nbsp;AfxEnableMemoryTracking(bEnable);<BR>}</P>
<P>void CWinApp::SetRegistryKey(UINT nIDRegistryKey)<BR>{<BR>&nbsp;ASSERT(m_pszRegistryKey == NULL);</P>
<P>&nbsp;TCHAR szRegistryKey[256];<BR>&nbsp;VERIFY(AfxLoadString(nIDRegistryKey, szRegistryKey));<BR>&nbsp;SetRegistryKey(szRegistryKey);<BR>}</P>
<P>// returns key for HKEY_CURRENT_USER\"Software"\RegistryKey\ProfileName<BR>// creating it if it doesn't exist<BR>// responsibility of the caller to call RegCloseKey() on the returned HKEY<BR>HKEY CWinApp::GetAppRegistryKey()<BR>{<BR>&nbsp;ASSERT(m_pszRegistryKey != NULL);<BR>&nbsp;ASSERT(m_pszProfileName != NULL);</P>
<P>&nbsp;HKEY hAppKey = NULL;<BR>&nbsp;HKEY hSoftKey = NULL;<BR>&nbsp;HKEY hCompanyKey = NULL;<BR>&nbsp;if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("software"), 0, KEY_WRITE|KEY_READ,<BR>&nbsp;&nbsp;&amp;hSoftKey) == ERROR_SUCCESS)<BR>&nbsp;{<BR>&nbsp;&nbsp;DWORD dw;<BR>&nbsp;&nbsp;if (RegCreateKeyEx(hSoftKey, m_pszRegistryKey, 0, REG_NONE,<BR>&nbsp;&nbsp;&nbsp;REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,<BR>&nbsp;&nbsp;&nbsp;&amp;hCompanyKey, &amp;dw) == ERROR_SUCCESS)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;RegCreateKeyEx(hCompanyKey, m_pszProfileName, 0, REG_NONE,<BR>&nbsp;&nbsp;&nbsp;&nbsp;REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&amp;hAppKey, &amp;dw);<BR>&nbsp;&nbsp;}<BR>&nbsp;}<BR>&nbsp;if (hSoftKey != NULL)<BR>&nbsp;&nbsp;RegCloseKey(hSoftKey);<BR>&nbsp;if (hCompanyKey != NULL)<BR>&nbsp;&nbsp;RegCloseKey(hCompanyKey);</P>
<P>&nbsp;return hAppKey;<BR>}</P>
<P>// returns key for:<BR>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HKEY_CURRENT_USER\"Software"\RegistryKey\AppName\lpszSection<BR>// creating it if it doesn't exist.<BR>// responsibility of the caller to call RegCloseKey() on the returned HKEY<BR>HKEY CWinApp::GetSectionKey(LPCTSTR lpszSection)<BR>{<BR>&nbsp;ASSERT(lpszSection != NULL);</P>
<P>&nbsp;HKEY hSectionKey = NULL;<BR>&nbsp;HKEY hAppKey = GetAppRegistryKey();<BR>&nbsp;if (hAppKey == NULL)<BR>&nbsp;&nbsp;return NULL;</P>
<P>&nbsp;DWORD dw;<BR>&nbsp;RegCreateKeyEx(hAppKey, lpszSection, 0, REG_NONE,<BR>&nbsp;&nbsp;REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,<BR>&nbsp;&nbsp;&amp;hSectionKey, &amp;dw);<BR>&nbsp;RegCloseKey(hAppKey);<BR>&nbsp;return hSectionKey;<BR>}</P>
<P>UINT CWinApp::GetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry,<BR>&nbsp;int nDefault)<BR>{<BR>&nbsp;ASSERT(lpszSection != NULL);<BR>&nbsp;ASSERT(lpszEntry != NULL);<BR>&nbsp;if (m_pszRegistryKey != NULL) // use registry<BR>&nbsp;{<BR>&nbsp;&nbsp;HKEY hSecKey = GetSectionKey(lpszSection);<BR>&nbsp;&nbsp;if (hSecKey == NULL)<BR>&nbsp;&nbsp;&nbsp;return nDefault;<BR>&nbsp;&nbsp;DWORD dwValue;<BR>&nbsp;&nbsp;DWORD dwType;<BR>&nbsp;&nbsp;DWORD dwCount = sizeof(DWORD);<BR>&nbsp;&nbsp;LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &amp;dwType,<BR>&nbsp;&nbsp;&nbsp;(LPBYTE)&amp;dwValue, &amp;dwCount);<BR>&nbsp;&nbsp;RegCloseKey(hSecKey);<BR>&nbsp;&nbsp;if (lResult == ERROR_SUCCESS)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;ASSERT(dwType == REG_DWORD);<BR>&nbsp;&nbsp;&nbsp;ASSERT(dwCount == sizeof(dwValue));<BR>&nbsp;&nbsp;&nbsp;return (UINT)dwValue;<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;return nDefault;<BR>&nbsp;}<BR>&nbsp;else<BR>&nbsp;{<BR>&nbsp;&nbsp;ASSERT(m_pszProfileName != NULL);<BR>&nbsp;&nbsp;return ::GetPrivateProfileInt(lpszSection, lpszEntry, nDefault,<BR>&nbsp;&nbsp;&nbsp;m_pszProfileName);<BR>&nbsp;}<BR>}</P>
<P>CString CWinApp::GetProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,<BR>&nbsp;LPCTSTR lpszDefault)<BR>{<BR>&nbsp;ASSERT(lpszSection != NULL);<BR>&nbsp;ASSERT(lpszEntry != NULL);<BR>&nbsp;if (m_pszRegistryKey != NULL)<BR>&nbsp;{<BR>&nbsp;&nbsp;HKEY hSecKey = GetSectionKey(lpszSection);<BR>&nbsp;&nbsp;if (hSecKey == NULL)<BR>&nbsp;&nbsp;&nbsp;return lpszDefault;<BR>&nbsp;&nbsp;CString strValue;<BR>&nbsp;&nbsp;DWORD dwType, dwCount;<BR>&nbsp;&nbsp;LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &amp;dwType,<BR>&nbsp;&nbsp;&nbsp;NULL, &amp;dwCount);<BR>&nbsp;&nbsp;if (lResult == ERROR_SUCCESS)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;ASSERT(dwType == REG_SZ);<BR>&nbsp;&nbsp;&nbsp;lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &amp;dwType,<BR>&nbsp;&nbsp;&nbsp;&nbsp;(LPBYTE)strValue.GetBuffer(dwCount/sizeof(TCHAR)), &amp;dwCount);<BR>&nbsp;&nbsp;&nbsp;strValue.ReleaseBuffer();<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;RegCloseKey(hSecKey);<BR>&nbsp;&nbsp;if (lResult == ERROR_SUCCESS)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;ASSERT(dwType == REG_SZ);<BR>&nbsp;&nbsp;&nbsp;return strValue;<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;return lpszDefault;<BR>&nbsp;}<BR>&nbsp;else<BR>&nbsp;{<BR>&nbsp;&nbsp;ASSERT(m_pszProfileName != NULL);</P>
<P>&nbsp;&nbsp;if (lpszDefault == NULL)<BR>&nbsp;&nbsp;&nbsp;lpszDefault = _T("");&nbsp;// don't pass in NULL<BR>&nbsp;&nbsp;TCHAR szT[4096];<BR>&nbsp;&nbsp;DWORD dw = ::GetPrivateProfileString(lpszSection, lpszEntry,<BR>&nbsp;&nbsp;&nbsp;lpszDefault, szT, _countof(szT), m_pszProfileName);<BR>&nbsp;&nbsp;ASSERT(dw &lt; 4095);<BR>&nbsp;&nbsp;return szT;<BR>&nbsp;}<BR>}</P>
<P>BOOL CWinApp::GetProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry,<BR>&nbsp;BYTE** ppData, UINT* pBytes)<BR>{<BR>&nbsp;ASSERT(lpszSection != NULL);<BR>&nbsp;ASSERT(lpszEntry != NULL);<BR>&nbsp;ASSERT(ppData != NULL);<BR>&nbsp;ASSERT(pBytes != NULL);<BR>&nbsp;*ppData = NULL;<BR>&nbsp;*pBytes = 0;<BR>&nbsp;if (m_pszRegistryKey != NULL)<BR>&nbsp;{<BR>&nbsp;&nbsp;HKEY hSecKey = GetSectionKey(lpszSection);<BR>&nbsp;&nbsp;if (hSecKey == NULL)<BR>&nbsp;&nbsp;&nbsp;return FALSE;</P>
<P>&nbsp;&nbsp;DWORD dwType, dwCount;<BR>&nbsp;&nbsp;LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &amp;dwType,<BR>&nbsp;&nbsp;&nbsp;NULL, &amp;dwCount);<BR>&nbsp;&nbsp;*pBytes = dwCount;<BR>&nbsp;&nbsp;if (lResult == ERROR_SUCCESS)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;ASSERT(dwType == REG_BINARY);<BR>&nbsp;&nbsp;&nbsp;*ppData = new BYTE[*pBytes];<BR>&nbsp;&nbsp;&nbsp;lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &amp;dwType,<BR>&nbsp;&nbsp;&nbsp;&nbsp;*ppData, &amp;dwCount);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;RegCloseKey(hSecKey);<BR>&nbsp;&nbsp;if (lResult == ERROR_SUCCESS)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;ASSERT(dwType == REG_BINARY);<BR>&nbsp;&nbsp;&nbsp;return TRUE;<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;else<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;delete [] *ppData;<BR>&nbsp;&nbsp;&nbsp;*ppData = NULL;<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;return FALSE;<BR>&nbsp;}<BR>&nbsp;else<BR>&nbsp;{<BR>&nbsp;&nbsp;ASSERT(m_pszProfileName != NULL);</P>
<P>&nbsp;&nbsp;CString str = GetProfileString(lpszSection, lpszEntry, NULL);<BR>&nbsp;&nbsp;if (str.IsEmpty())<BR>&nbsp;&nbsp;&nbsp;return FALSE;<BR>&nbsp;&nbsp;ASSERT(str.GetLength()%2 == 0);<BR>&nbsp;&nbsp;INT_PTR nLen = str.GetLength();<BR>&nbsp;&nbsp;*pBytes = UINT(nLen)/2;<BR>&nbsp;&nbsp;*ppData = new BYTE[*pBytes];<BR>&nbsp;&nbsp;for (int i=0;i&lt;nLen;i+=2)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;(*ppData)[i/2] = (BYTE)<BR>&nbsp;&nbsp;&nbsp;&nbsp;(((str[i+1] - 'A') &lt;&lt; 4) + (str[i] - 'A'));<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;return TRUE;<BR>&nbsp;}<BR>}</P>
<P>#ifdef AFX_CORE3_SEG<BR>#pragma code_seg(AFX_CORE3_SEG)<BR>#endif</P>
<P>BOOL CWinApp::WriteProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry,<BR>&nbsp;int nValue)<BR>{<BR>&nbsp;ASSERT(lpszSection != NULL);<BR>&nbsp;ASSERT(lpszEntry != NULL);<BR>&nbsp;if (m_pszRegistryKey != NULL)<BR>&nbsp;{<BR>&nbsp;&nbsp;HKEY hSecKey = GetSectionKey(lpszSection);<BR>&nbsp;&nbsp;if (hSecKey == NULL)<BR>&nbsp;&nbsp;&nbsp;return FALSE;<BR>&nbsp;&nbsp;LONG lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_DWORD,<BR>&nbsp;&nbsp;&nbsp;(LPBYTE)&amp;nValue, sizeof(nValue));<BR>&nbsp;&nbsp;RegCloseKey(hSecKey);<BR>&nbsp;&nbsp;return lResult == ERROR_SUCCESS;<BR>&nbsp;}<BR>&nbsp;else<BR>&nbsp;{<BR>&nbsp;&nbsp;ASSERT(m_pszProfileName != NULL);</P>
<P>&nbsp;&nbsp;TCHAR szT[16];<BR>&nbsp;&nbsp;wsprintf(szT, _T("%d"), nValue);<BR>&nbsp;&nbsp;return ::WritePrivateProfileString(lpszSection, lpszEntry, szT,<BR>&nbsp;&nbsp;&nbsp;m_pszProfileName);<BR>&nbsp;}<BR>}</P>
<P>BOOL CWinApp::WriteProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,<BR>&nbsp;&nbsp;&nbsp;LPCTSTR lpszValue)<BR>{<BR>&nbsp;ASSERT(lpszSection != NULL);<BR>&nbsp;if (m_pszRegistryKey != NULL)<BR>&nbsp;{<BR>&nbsp;&nbsp;LONG lResult;<BR>&nbsp;&nbsp;if (lpszEntry == NULL) //delete whole section<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;HKEY hAppKey = GetAppRegistryKey();<BR>&nbsp;&nbsp;&nbsp;if (hAppKey == NULL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;return FALSE;<BR>&nbsp;&nbsp;&nbsp;lResult = ::RegDeleteKey(hAppKey, lpszSection);<BR>&nbsp;&nbsp;&nbsp;RegCloseKey(hAppKey);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;else if (lpszValue == NULL)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;HKEY hSecKey = GetSectionKey(lpszSection);<BR>&nbsp;&nbsp;&nbsp;if (hSecKey == NULL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;return FALSE;<BR>&nbsp;&nbsp;&nbsp;// necessary to cast away const below<BR>&nbsp;&nbsp;&nbsp;lResult = ::RegDeleteValue(hSecKey, (LPTSTR)lpszEntry);<BR>&nbsp;&nbsp;&nbsp;RegCloseKey(hSecKey);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;else<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;HKEY hSecKey = GetSectionKey(lpszSection);<BR>&nbsp;&nbsp;&nbsp;if (hSecKey == NULL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;return FALSE;<BR>&nbsp;&nbsp;&nbsp;lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_SZ,<BR>&nbsp;&nbsp;&nbsp;&nbsp;(LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));<BR>&nbsp;&nbsp;&nbsp;RegCloseKey(hSecKey);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;return lResult == ERROR_SUCCESS;<BR>&nbsp;}<BR>&nbsp;else<BR>&nbsp;{<BR>&nbsp;&nbsp;ASSERT(m_pszProfileName != NULL);<BR>&nbsp;&nbsp;ASSERT(lstrlen(m_pszProfileName) &lt; 4095); // can't read in bigger<BR>&nbsp;&nbsp;return ::WritePrivateProfileString(lpszSection, lpszEntry, lpszValue,<BR>&nbsp;&nbsp;&nbsp;m_pszProfileName);<BR>&nbsp;}<BR>}</P>
<P>BOOL CWinApp::WriteProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry,<BR>&nbsp;LPBYTE pData, UINT nBytes)<BR>{<BR>&nbsp;ASSERT(lpszSection != NULL);<BR>&nbsp;if (m_pszRegistryKey != NULL)<BR>&nbsp;{<BR>&nbsp;&nbsp;LONG lResult;<BR>&nbsp;&nbsp;HKEY hSecKey = GetSectionKey(lpszSection);<BR>&nbsp;&nbsp;if (hSecKey == NULL)<BR>&nbsp;&nbsp;&nbsp;return FALSE;<BR>&nbsp;&nbsp;lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_BINARY,<BR>&nbsp;&nbsp;&nbsp;pData, nBytes);<BR>&nbsp;&nbsp;RegCloseKey(hSecKey);<BR>&nbsp;&nbsp;return lResult == ERROR_SUCCESS;<BR>&nbsp;}</P>
<P>&nbsp;// convert to string and write out<BR>&nbsp;LPTSTR lpsz = new TCHAR[nBytes*2+1];<BR>&nbsp;UINT i;<BR>&nbsp;for (i = 0; i &lt; nBytes; i++)<BR>&nbsp;{<BR>&nbsp;&nbsp;lpsz[i*2] = (TCHAR)((pData[i] &amp; 0x0F) + 'A'); //low nibble<BR>&nbsp;&nbsp;lpsz[i*2+1] = (TCHAR)(((pData[i] &gt;&gt; 4) &amp; 0x0F) + 'A'); //high nibble<BR>&nbsp;}<BR>&nbsp;lpsz[i*2] = 0;</P>
<P>&nbsp;ASSERT(m_pszProfileName != NULL);</P>
<P>&nbsp;BOOL bResult = WriteProfileString(lpszSection, lpszEntry, lpsz);<BR>&nbsp;delete[] lpsz;<BR>&nbsp;return bResult;<BR>}</P></DIV></DIV></TD></TR></TBODY></TABLE><img src ="http://www.cppblog.com/zoj/aggbug/3289.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zoj/" target="_blank">zoj</a> 2006-02-16 16:38 <a href="http://www.cppblog.com/zoj/archive/2006/02/16/3289.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使CWinApp::GetProfileString读取自己的INI文件</title><link>http://www.cppblog.com/zoj/archive/2006/02/16/3288.html</link><dc:creator>zoj</dc:creator><author>zoj</author><pubDate>Thu, 16 Feb 2006 08:31:00 GMT</pubDate><guid>http://www.cppblog.com/zoj/archive/2006/02/16/3288.html</guid><wfw:comment>http://www.cppblog.com/zoj/comments/3288.html</wfw:comment><comments>http://www.cppblog.com/zoj/archive/2006/02/16/3288.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zoj/comments/commentRss/3288.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zoj/services/trackbacks/3288.html</trackback:ping><description><![CDATA[<P><FONT color=#4db34d>//以下代码放到InitInstance中<BR>//首先清除注册表键值变量所分配的内存空间&nbsp;.</FONT><BR><FONT color=#1111ee>free</FONT>((<FONT color=#2222dd>void</FONT>*)m_pszRegistryKey);<BR><FONT color=#3dee11><FONT color=#61b34d>//再清除Ini变量</FONT><BR></FONT><FONT color=#0000ff>free</FONT>((<FONT color=#0000ff>void</FONT>*)m_pszProfileName);<BR>&nbsp;<FONT color=#57c43c>//改变Ini文件名.</FONT><BR>&nbsp;m_pszProfileName=_<FONT color=#0033ff>tcsdup</FONT>("d:\\myPrj\\iniName.ini");</P>
<P>进行完以上设置后我就可以直接使用 <STRONG>GetProfileString WriteProfileString</STRONG> 等函数进行操作了</P><img src ="http://www.cppblog.com/zoj/aggbug/3288.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zoj/" target="_blank">zoj</a> 2006-02-16 16:31 <a href="http://www.cppblog.com/zoj/archive/2006/02/16/3288.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>CINT的简单实例</title><link>http://www.cppblog.com/zoj/archive/2006/02/16/3287.html</link><dc:creator>zoj</dc:creator><author>zoj</author><pubDate>Thu, 16 Feb 2006 08:12:00 GMT</pubDate><guid>http://www.cppblog.com/zoj/archive/2006/02/16/3287.html</guid><wfw:comment>http://www.cppblog.com/zoj/comments/3287.html</wfw:comment><comments>http://www.cppblog.com/zoj/archive/2006/02/16/3287.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zoj/comments/commentRss/3287.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zoj/services/trackbacks/3287.html</trackback:ping><description><![CDATA[<P>下列代码摘录示范了如何创建一个简单的浏览器、下载一张网页、通过FTP操作一个文件和查找一个Gopher文件。它们并不是一个完整的例子，其中不包含异常处理,只是本人最近写程序的一点总结而已。</P>
<P>1 创建一个非常简单的浏览器<BR>#include &lt;afxinet.h&gt;<BR>//假设URL的名字已经被初始化了<BR>CInternetSession session("My Session");<BR>CStdioFile* pFile=NULL;<BR>//使用一个URL，并显示一张网页<BR>while(lpszURL=DisplayPage(...))<BR>{<BR>&nbsp;&nbsp; pFile=session.OpenURL(lpszURL);<BR>&nbsp;&nbsp; while(pFile-&gt;Read(szBuff,1024)&gt;0)<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //读取文件...<BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp; delete pFile;<BR>}<BR>session.Close();</P>
<P>2 下载一张网页<BR>#include &lt;afxinet.h&gt;<BR>//假设服务器、端口号和URL名字已经初始化了<BR>CInternetSession session("My Session");<BR>CHttpConnection* pSever=NULL;<BR>CHttpFile* pFile=NULL;<BR>try<BR>{<BR>&nbsp; CString strServerName;<BR>&nbsp; INTERNET_PORT nPort;</P>
<P>&nbsp; pServer=session.GetHttpConnection(strServerName,nPort);<BR>&nbsp; pFile=pServer-&gt;OpenRequest(CHttpConnection::HTTP_VERB_GET,strObject);<BR>&nbsp; pFile-&gt;AddRequestHeaders(szHeaders);<BR>&nbsp; pFile-&gt;SendRequest();<BR>&nbsp; pFile-&gt;QueryInfoStatusCode(dwRet);</P>
<P>&nbsp; if(dwRet==HTTP_STATUS_OK)<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; UINT nRead=pFile-&gt;Read(szBuff,1023);<BR>&nbsp;while(nRead&gt;0)<BR>&nbsp;{<BR>&nbsp;&nbsp;&nbsp; //读取文件<BR>&nbsp;}<BR>&nbsp; }<BR>&nbsp; delete pFile;<BR>&nbsp; delete pServer;<BR>}<BR>catch(CInternetException* pEx)<BR>{<BR>&nbsp; //捕捉WinInet的错误<BR>}<BR>session.Close();</P>
<P>3 通过FTP操作一个文件<BR>#include &lt;afxinet.h&gt;<BR>//假设服务器名和文件名已经初始化了<BR>CInternetSession session("My FTP Session");<BR>CFtpConnection* pConn=NULL;</P>
<P>pConn=session.GetFtpConnect(lpszServerName);<BR>//取得文件<BR>if(!pConn-&gt;GetFile(pstrRemoteFile,pstrLocalFile))<BR>&nbsp;&nbsp; //显示一个错误<BR>delete pConn;<BR>session.Close();</P>
<P>4重新获得一个Gopher目录<BR>#include &lt;afxinet.h&gt;<BR>//假设文件名已经初始化了<BR>CInternetSession session("My Gopher Session");<BR>CGopherConnection* pConn=NULL;<BR>CGopherFileFind* pFile;</P>
<P>pConn=session.GetGopherConnection("<A href="gopher://gopher.yousite.com/">gopher.yousite.com</A>");<BR>pFile=new CGopherFileFind(pConn);<BR>BOOL bFound=pFile-&gt;FindFile(lpszFileToFind);<BR>while(bFound)<BR>{<BR>&nbsp; bFound=pFile-&gt;FindNextFile();<BR>&nbsp; //重新获得找到的文件属性<BR>}<BR>delete pFile;<BR>delete pConn;<BR>session.Close();</P>
<P>当在使用WinInet类库时，用户可以使用应用程序的CInternetSession对象的成员函数<BR>OnStatusCallback来获取状态信息。如果用户继承了其自己的CInternetSession对象，<BR>编写了超越函数OnStatusCallback并允许状态的回调，MFC将把此次国际互连网对话的<BR>所有活动的进度信息作为参数，调用用户的OnStatusCallback函数。</P><img src ="http://www.cppblog.com/zoj/aggbug/3287.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zoj/" target="_blank">zoj</a> 2006-02-16 16:12 <a href="http://www.cppblog.com/zoj/archive/2006/02/16/3287.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>新的一页，新的人生</title><link>http://www.cppblog.com/zoj/archive/2006/02/15/3276.html</link><dc:creator>zoj</dc:creator><author>zoj</author><pubDate>Wed, 15 Feb 2006 14:42:00 GMT</pubDate><guid>http://www.cppblog.com/zoj/archive/2006/02/15/3276.html</guid><wfw:comment>http://www.cppblog.com/zoj/comments/3276.html</wfw:comment><comments>http://www.cppblog.com/zoj/archive/2006/02/15/3276.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zoj/comments/commentRss/3276.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zoj/services/trackbacks/3276.html</trackback:ping><description><![CDATA[<FONT face="Courier New">研究生考试还是一把悬在我头上的德谟克利斯之剑，随着时间的流逝，我脑海中的印象愈来愈淡。有时，我几乎要怀疑我是否真的考过，但是那些触目惊心的答案总是在无情的提醒着我，等吧，熬吧，分数还是这场考试最后的悬念。这是令我懊恼得事，我的人生究竟如何继续？这两个月将看出端倪，我还在等……</FONT><img src ="http://www.cppblog.com/zoj/aggbug/3276.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zoj/" target="_blank">zoj</a> 2006-02-15 22:42 <a href="http://www.cppblog.com/zoj/archive/2006/02/15/3276.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>