随笔-5  评论-1  文章-0  trackbacks-0
  2006年2月27日
考研分数终于在焦急而又忐忑的等待中下来了
中午手机铃声大作,打开短信,只有六个字——分数可以查了,顿时心狂跳不止。午饭匆匆扒了几口,赶忙做到电脑前,熟练的打开网页。突然间犹豫了,三年前高考查分的一幕又在眼前恍现,三年前的失败使我对查分产生了一种不可名状的恐惧。每次查分必将作一次激烈的思想斗争,检讨自己最近有没有作什么不对劲的事,害怕由于冒犯冥冥之中的神灵而使自己的分数诡异的变的不堪回首。
终究还是打开了网页,终究还是输入了准考证,网速以超乎想象的速度将一份雅丽的成绩单呈现在我的面前
令人惊喜的分数,410分,我做梦也没想到会是这么高的分数。狂喜!!!无言以表自己的心情
我一年的心血没有白费阿……
下面就该考虑一下自己的事了,争取在这一年的时间内,自己能在黑客技术有所长进,自己还是个门外汉阿,还没入道啊。不急不急,这事不是急得,关键是潜心研究,宝剑锋从磨砺出,我终可以铸成自己的一天宝剑!!

posted @ 2006-02-27 14:52 zoj 阅读(391) | 评论 (1)编辑 收藏
  2006年2月16日
MFC的
CWinApp 类提供了很容易的注册表访问函数~~以前从来没注意过~~还到处找读写注册表的办法~~ -_-! 看下面几个成员函数~

SetRegistryKey Causes application settings to be stored in the registry instead of .INI files.

SetRegistryKey 这个函数功能是设置MFC程序的注册表访问键,并把读写 ini 文件的成员函数映射到读写注册表。只要调用一下 SetRegistryKey 并指定注册表键值,那么下面6个成员函数,就被映射到进行注册表读取了~

WriteProfileBinary Writes binary data to an entry in the application's .INI file.
WriteProfileInt Writes an integer to an entry in the application's .INI file.
WriteProfileString Writes a string to an entry in the application's .INI file.

GetProfileBinary Retrieves binary data from an entry in the application's .INI file.
GetProfileInt Retrieves an integer from an entry in the application's .INI file.
GetProfileString Retrieves a string from an entry in the application's .INI file.

MSDN上面写上面6个函数是写到INI文件的。所以俺就忽略了其访问注册表的功能。无意中看了其MFC实现才有所了解。

例子如下:
SetRegistryKey(_T("boli's app")); //这里是准备在注册表HKEY_CURRENT_USER\\software 下面生成一个boli's app 分支~为什么说是准备呢?因为如果不调用相关函数,如上面提到的6个函数,它是不会真正读写注册表的。具体本文最最下面的MFC实现摘录。
CString strUserName,strPassword;
WriteProfileString("LogInfo","UserName",strUserName); //向注册表HKEY_CURRENT_USER\\software\\boli's app\\LogInfo\\分支下写入 UserName 字符串行键值~
WriteProfileString("LogInfo","Password",strPassword);//同上~

 strUserName = GetProfileString("LogInfo","UserName");// 这里是读取HKEY_CURRENT_USER\\software\\boli's app\\LogInfo\\分支下的 UserName 字符串键值到 strUserName~
 strPassword =  GetProfileString("LogInfo","Password");

如果不是在CWinApp 派生的类中读写注册表,可以直接用:
 strUserName = theApp.GetProfileString("LogInfo","UserName");
 strPassword = theApp.GetProfileString("LogInfo","Password");

 strUserName = AfxGetApp()->GetProfileString("LogInfo","UserName");
条条大路通罗马。



看看MFC的代码吧~~SDK高手们不屑MFC,但有时候看看MFC的代码会到学习SDK,windows api有很大的帮助~ :P
////////////////////////////////////////////////////////////////////////////
// CWinApp Settings Helpers

#ifdef AFX_INIT_SEG
#pragma code_seg(AFX_INIT_SEG)
#endif

void CWinApp::SetRegistryKey(LPCTSTR lpszRegistryKey)
{
 ASSERT(m_pszRegistryKey == NULL);
 ASSERT(lpszRegistryKey != NULL);
 ASSERT(m_pszAppName != NULL);

 BOOL bEnable = AfxEnableMemoryTracking(FALSE);
 free((void*)m_pszRegistryKey);
 m_pszRegistryKey = _tcsdup(lpszRegistryKey);
 free((void*)m_pszProfileName);
 m_pszProfileName = _tcsdup(m_pszAppName);
 AfxEnableMemoryTracking(bEnable);
}

void CWinApp::SetRegistryKey(UINT nIDRegistryKey)
{
 ASSERT(m_pszRegistryKey == NULL);

 TCHAR szRegistryKey[256];
 VERIFY(AfxLoadString(nIDRegistryKey, szRegistryKey));
 SetRegistryKey(szRegistryKey);
}

// returns key for HKEY_CURRENT_USER\"Software"\RegistryKey\ProfileName
// creating it if it doesn't exist
// responsibility of the caller to call RegCloseKey() on the returned HKEY
HKEY CWinApp::GetAppRegistryKey()
{
 ASSERT(m_pszRegistryKey != NULL);
 ASSERT(m_pszProfileName != NULL);

 HKEY hAppKey = NULL;
 HKEY hSoftKey = NULL;
 HKEY hCompanyKey = NULL;
 if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("software"), 0, KEY_WRITE|KEY_READ,
  &hSoftKey) == ERROR_SUCCESS)
 {
  DWORD dw;
  if (RegCreateKeyEx(hSoftKey, m_pszRegistryKey, 0, REG_NONE,
   REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
   &hCompanyKey, &dw) == ERROR_SUCCESS)
  {
   RegCreateKeyEx(hCompanyKey, m_pszProfileName, 0, REG_NONE,
    REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
    &hAppKey, &dw);
  }
 }
 if (hSoftKey != NULL)
  RegCloseKey(hSoftKey);
 if (hCompanyKey != NULL)
  RegCloseKey(hCompanyKey);

 return hAppKey;
}

// returns key for:
//      HKEY_CURRENT_USER\"Software"\RegistryKey\AppName\lpszSection
// creating it if it doesn't exist.
// responsibility of the caller to call RegCloseKey() on the returned HKEY
HKEY CWinApp::GetSectionKey(LPCTSTR lpszSection)
{
 ASSERT(lpszSection != NULL);

 HKEY hSectionKey = NULL;
 HKEY hAppKey = GetAppRegistryKey();
 if (hAppKey == NULL)
  return NULL;

 DWORD dw;
 RegCreateKeyEx(hAppKey, lpszSection, 0, REG_NONE,
  REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
  &hSectionKey, &dw);
 RegCloseKey(hAppKey);
 return hSectionKey;
}

UINT CWinApp::GetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry,
 int nDefault)
{
 ASSERT(lpszSection != NULL);
 ASSERT(lpszEntry != NULL);
 if (m_pszRegistryKey != NULL) // use registry
 {
  HKEY hSecKey = GetSectionKey(lpszSection);
  if (hSecKey == NULL)
   return nDefault;
  DWORD dwValue;
  DWORD dwType;
  DWORD dwCount = sizeof(DWORD);
  LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
   (LPBYTE)&dwValue, &dwCount);
  RegCloseKey(hSecKey);
  if (lResult == ERROR_SUCCESS)
  {
   ASSERT(dwType == REG_DWORD);
   ASSERT(dwCount == sizeof(dwValue));
   return (UINT)dwValue;
  }
  return nDefault;
 }
 else
 {
  ASSERT(m_pszProfileName != NULL);
  return ::GetPrivateProfileInt(lpszSection, lpszEntry, nDefault,
   m_pszProfileName);
 }
}

CString CWinApp::GetProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,
 LPCTSTR lpszDefault)
{
 ASSERT(lpszSection != NULL);
 ASSERT(lpszEntry != NULL);
 if (m_pszRegistryKey != NULL)
 {
  HKEY hSecKey = GetSectionKey(lpszSection);
  if (hSecKey == NULL)
   return lpszDefault;
  CString strValue;
  DWORD dwType, dwCount;
  LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
   NULL, &dwCount);
  if (lResult == ERROR_SUCCESS)
  {
   ASSERT(dwType == REG_SZ);
   lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
    (LPBYTE)strValue.GetBuffer(dwCount/sizeof(TCHAR)), &dwCount);
   strValue.ReleaseBuffer();
  }
  RegCloseKey(hSecKey);
  if (lResult == ERROR_SUCCESS)
  {
   ASSERT(dwType == REG_SZ);
   return strValue;
  }
  return lpszDefault;
 }
 else
 {
  ASSERT(m_pszProfileName != NULL);

  if (lpszDefault == NULL)
   lpszDefault = _T(""); // don't pass in NULL
  TCHAR szT[4096];
  DWORD dw = ::GetPrivateProfileString(lpszSection, lpszEntry,
   lpszDefault, szT, _countof(szT), m_pszProfileName);
  ASSERT(dw < 4095);
  return szT;
 }
}

BOOL CWinApp::GetProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry,
 BYTE** ppData, UINT* pBytes)
{
 ASSERT(lpszSection != NULL);
 ASSERT(lpszEntry != NULL);
 ASSERT(ppData != NULL);
 ASSERT(pBytes != NULL);
 *ppData = NULL;
 *pBytes = 0;
 if (m_pszRegistryKey != NULL)
 {
  HKEY hSecKey = GetSectionKey(lpszSection);
  if (hSecKey == NULL)
   return FALSE;

  DWORD dwType, dwCount;
  LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
   NULL, &dwCount);
  *pBytes = dwCount;
  if (lResult == ERROR_SUCCESS)
  {
   ASSERT(dwType == REG_BINARY);
   *ppData = new BYTE[*pBytes];
   lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
    *ppData, &dwCount);
  }
  RegCloseKey(hSecKey);
  if (lResult == ERROR_SUCCESS)
  {
   ASSERT(dwType == REG_BINARY);
   return TRUE;
  }
  else
  {
   delete [] *ppData;
   *ppData = NULL;
  }
  return FALSE;
 }
 else
 {
  ASSERT(m_pszProfileName != NULL);

  CString str = GetProfileString(lpszSection, lpszEntry, NULL);
  if (str.IsEmpty())
   return FALSE;
  ASSERT(str.GetLength()%2 == 0);
  INT_PTR nLen = str.GetLength();
  *pBytes = UINT(nLen)/2;
  *ppData = new BYTE[*pBytes];
  for (int i=0;i<nLen;i+=2)
  {
   (*ppData)[i/2] = (BYTE)
    (((str[i+1] - 'A') << 4) + (str[i] - 'A'));
  }
  return TRUE;
 }
}

#ifdef AFX_CORE3_SEG
#pragma code_seg(AFX_CORE3_SEG)
#endif

BOOL CWinApp::WriteProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry,
 int nValue)
{
 ASSERT(lpszSection != NULL);
 ASSERT(lpszEntry != NULL);
 if (m_pszRegistryKey != NULL)
 {
  HKEY hSecKey = GetSectionKey(lpszSection);
  if (hSecKey == NULL)
   return FALSE;
  LONG lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_DWORD,
   (LPBYTE)&nValue, sizeof(nValue));
  RegCloseKey(hSecKey);
  return lResult == ERROR_SUCCESS;
 }
 else
 {
  ASSERT(m_pszProfileName != NULL);

  TCHAR szT[16];
  wsprintf(szT, _T("%d"), nValue);
  return ::WritePrivateProfileString(lpszSection, lpszEntry, szT,
   m_pszProfileName);
 }
}

BOOL CWinApp::WriteProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,
   LPCTSTR lpszValue)
{
 ASSERT(lpszSection != NULL);
 if (m_pszRegistryKey != NULL)
 {
  LONG lResult;
  if (lpszEntry == NULL) //delete whole section
  {
   HKEY hAppKey = GetAppRegistryKey();
   if (hAppKey == NULL)
    return FALSE;
   lResult = ::RegDeleteKey(hAppKey, lpszSection);
   RegCloseKey(hAppKey);
  }
  else if (lpszValue == NULL)
  {
   HKEY hSecKey = GetSectionKey(lpszSection);
   if (hSecKey == NULL)
    return FALSE;
   // necessary to cast away const below
   lResult = ::RegDeleteValue(hSecKey, (LPTSTR)lpszEntry);
   RegCloseKey(hSecKey);
  }
  else
  {
   HKEY hSecKey = GetSectionKey(lpszSection);
   if (hSecKey == NULL)
    return FALSE;
   lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_SZ,
    (LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));
   RegCloseKey(hSecKey);
  }
  return lResult == ERROR_SUCCESS;
 }
 else
 {
  ASSERT(m_pszProfileName != NULL);
  ASSERT(lstrlen(m_pszProfileName) < 4095); // can't read in bigger
  return ::WritePrivateProfileString(lpszSection, lpszEntry, lpszValue,
   m_pszProfileName);
 }
}

BOOL CWinApp::WriteProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry,
 LPBYTE pData, UINT nBytes)
{
 ASSERT(lpszSection != NULL);
 if (m_pszRegistryKey != NULL)
 {
  LONG lResult;
  HKEY hSecKey = GetSectionKey(lpszSection);
  if (hSecKey == NULL)
   return FALSE;
  lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_BINARY,
   pData, nBytes);
  RegCloseKey(hSecKey);
  return lResult == ERROR_SUCCESS;
 }

 // convert to string and write out
 LPTSTR lpsz = new TCHAR[nBytes*2+1];
 UINT i;
 for (i = 0; i < nBytes; i++)
 {
  lpsz[i*2] = (TCHAR)((pData[i] & 0x0F) + 'A'); //low nibble
  lpsz[i*2+1] = (TCHAR)(((pData[i] >> 4) & 0x0F) + 'A'); //high nibble
 }
 lpsz[i*2] = 0;

 ASSERT(m_pszProfileName != NULL);

 BOOL bResult = WriteProfileString(lpszSection, lpszEntry, lpsz);
 delete[] lpsz;
 return bResult;
}

posted @ 2006-02-16 16:38 zoj 阅读(5948) | 评论 (0)编辑 收藏

//以下代码放到InitInstance中
//首先清除注册表键值变量所分配的内存空间 .

free((void*)m_pszRegistryKey);
//再清除Ini变量
free((void*)m_pszProfileName);
 //改变Ini文件名.
 m_pszProfileName=_tcsdup("d:\\myPrj\\iniName.ini");

进行完以上设置后我就可以直接使用 GetProfileString WriteProfileString 等函数进行操作了

posted @ 2006-02-16 16:31 zoj 阅读(1740) | 评论 (0)编辑 收藏

下列代码摘录示范了如何创建一个简单的浏览器、下载一张网页、通过FTP操作一个文件和查找一个Gopher文件。它们并不是一个完整的例子,其中不包含异常处理,只是本人最近写程序的一点总结而已。

1 创建一个非常简单的浏览器
#include <afxinet.h>
//假设URL的名字已经被初始化了
CInternetSession session("My Session");
CStdioFile* pFile=NULL;
//使用一个URL,并显示一张网页
while(lpszURL=DisplayPage(...))
{
   pFile=session.OpenURL(lpszURL);
   while(pFile->Read(szBuff,1024)>0)
   {
          //读取文件...
   }
   delete pFile;
}
session.Close();

2 下载一张网页
#include <afxinet.h>
//假设服务器、端口号和URL名字已经初始化了
CInternetSession session("My Session");
CHttpConnection* pSever=NULL;
CHttpFile* pFile=NULL;
try
{
  CString strServerName;
  INTERNET_PORT nPort;

  pServer=session.GetHttpConnection(strServerName,nPort);
  pFile=pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,strObject);
  pFile->AddRequestHeaders(szHeaders);
  pFile->SendRequest();
  pFile->QueryInfoStatusCode(dwRet);

  if(dwRet==HTTP_STATUS_OK)
  {
    UINT nRead=pFile->Read(szBuff,1023);
 while(nRead>0)
 {
    //读取文件
 }
  }
  delete pFile;
  delete pServer;
}
catch(CInternetException* pEx)
{
  //捕捉WinInet的错误
}
session.Close();

3 通过FTP操作一个文件
#include <afxinet.h>
//假设服务器名和文件名已经初始化了
CInternetSession session("My FTP Session");
CFtpConnection* pConn=NULL;

pConn=session.GetFtpConnect(lpszServerName);
//取得文件
if(!pConn->GetFile(pstrRemoteFile,pstrLocalFile))
   //显示一个错误
delete pConn;
session.Close();

4重新获得一个Gopher目录
#include <afxinet.h>
//假设文件名已经初始化了
CInternetSession session("My Gopher Session");
CGopherConnection* pConn=NULL;
CGopherFileFind* pFile;

pConn=session.GetGopherConnection("gopher.yousite.com");
pFile=new CGopherFileFind(pConn);
BOOL bFound=pFile->FindFile(lpszFileToFind);
while(bFound)
{
  bFound=pFile->FindNextFile();
  //重新获得找到的文件属性
}
delete pFile;
delete pConn;
session.Close();

当在使用WinInet类库时,用户可以使用应用程序的CInternetSession对象的成员函数
OnStatusCallback来获取状态信息。如果用户继承了其自己的CInternetSession对象,
编写了超越函数OnStatusCallback并允许状态的回调,MFC将把此次国际互连网对话的
所有活动的进度信息作为参数,调用用户的OnStatusCallback函数。

posted @ 2006-02-16 16:12 zoj 阅读(989) | 评论 (0)编辑 收藏
  2006年2月15日
研究生考试还是一把悬在我头上的德谟克利斯之剑,随着时间的流逝,我脑海中的印象愈来愈淡。有时,我几乎要怀疑我是否真的考过,但是那些触目惊心的答案总是在无情的提醒着我,等吧,熬吧,分数还是这场考试最后的悬念。这是令我懊恼得事,我的人生究竟如何继续?这两个月将看出端倪,我还在等……
posted @ 2006-02-15 22:42 zoj 阅读(251) | 评论 (0)编辑 收藏
仅列出标题