环境:Windows 98SE/2000,VC++ 6.0,Foxmail 4.1 简体中文版 摘要 本文旨在介绍 Foxmail 的账号存储机制,并基于此编写一个辅助工具 SmartFox ,实现闪存“随身邮”功能。 问题的出现与分析 最近我买了一支爱国者的经典型“迷你王”闪存,自己安装了 Foxmail ,并在闪存上创建了两个账号以收发邮件。但当我在另一台 PC 上使用 Foxmail 时,它却提示没有可用的账号。这是怎么回事呢?原来, Foxmail 在创建账号时,会将这个账号所在目录完整的路径名记录下来。由于闪存在不同的 PC 上获得的盘符不一定相同,这样在闪存的盘符改变后, Foxmail 便因无法定位该目录而发生错误。 开始时,我编辑了一个批处理文件 Foxmail.BAT 用以启动 Foxmail 。其思路是将闪存的根目录重定向为 Z: 盘,每次将闪存的当前盘符作为参数传递给这个批处理文件即可。为此我不得不将之前创建的账号删除,在命令行提示符下启动批处理命令,然后在 Z: 盘下创建账号。这样不是太麻烦了吗?是的!我也曾尝试过利用中间文件自动进行参数传递,但是由于 DOS 的不可重入性而不能保证每次都成功运行。因此我开始剖析 Foxmail 的账号存储机制。 Foxmail 的账号存储机制 Foxmail 将每个账号的最基本信息(账号名称、存储目录)存放在安装目录下的文件Accounts.CFG 中。在每个账号的目录中则利用文件 Account.STG 存储该账号的其他信息。 Accounts.CFG 是一个复合文档,它包括一个 Ver30 存储( Storage )。在 Ver30 存储下有一个accounts.cfg 流( Stream )。你可以使用 Visual Studio 6.0 提供的 DocFile 阅读器打开它。关于存储和流的更多知识,请参阅 MSDN: [Platform SDK] Structured Storage 。 经过分析,我得到了 Foxmail 的账号存储机制。如下即为 accounts.cfg 流的 C 语言描述结构(表中均为十六进制):
编程实现 现在我们就可以开始编写 Foxmail 的辅助工具 SmartFox 了。 重要约定: 1.所有账号的目录均在闪存上! 2.SmartFox 在 Foxmail 安装目录下工作! 程序的流程:获得闪存的当前盘符,打开 accounts.cfg 流,修改所有账号的目录盘符为闪存当前盘符,启动 Foxmail 。我认为没有必要在 Foxmail 退出后恢复 accounts.cfg 的内容,你认为呢? SmartFox 是一个利用 MFC 实现的基于对话框的应用程序,静态链接 MFC 的动态链接库。 运行界面如下,单击左键启动 Foxmail ,单击右键退出。
(一)对程序中使用的 API 的介绍,更详细的内容请参见 MSDN : 1. 获得当前目录的全路径名 (Win API) 1. DWORD GetCurrentDirectory( 2. DWORD nBufferLength, // 保存目录名的缓冲区大小 3. LPTSTR lpBuffer // 指向缓冲区的指针 4. ); 2. 打开一个复合文档 (Win API) 1. HRESULT StgOpenStorage( 2. const WCHAR *pwcsName, //复合文档的文件名 3. IStorage *pstgPriority, //先前已打开的根存储的指针 4. DWORD grfMode, //访问模式 5. SNB snbExclude, //指向一个SNB结构的指针,以确定哪些元素将被排除访问 6. DWORD reserved, //保留 7. IStorage **ppstgOpen //接受返回的IStorage接口指针 8. ); 3. 打开一个子存储或流 (IStorage API) 1. HRESULT OpenStorage( 2. const WCHAR *pwcsName, //要打开的存储的名字 3. IStorage *pstgPriority, //该参数一定为NULL值 4. DWORD grfMode, //访问模式 5. SNB snbExclude, //该参数一定为NULL值 6. DWORD reserved, //保留 7. IStorage **ppstg //接受返回的IStorage接口指针 8. ); OpenStream 的参数与 OpenStorage 的差不多,只是返回的是一个 IStream 指针。 4. 取得流的大小,移动流的读写指针,从流读写数据 (IStream API) 01. HRESULT Stat( 02. STATSTG *pstatstg, //指向一个STATSTG结构的指针,STATSTG的cb域即为流的大小。 03. DWORD grfStatFlag //决定是否要在STATSTG中返回某些值的标志 04. ); 05. 06. 07. HRESULT Seek( 08. LARGE_INTEGER dlibMove, //相对于dwOrigin的偏址 09. DWORD dwOrigin, //起始位置 10. ULARGE_INTEGER *plibNewPosition //接受指向新位置的指针 11. ); 12. 13. 14. HRESULT Read( 15. void *pv, //指向数据缓冲区的指针 16. ULONG cb, //要读的字节数 17. ULONG *pcbRead //实际读出的字节数 18. );Write 的参数与 Read 的一样。 5. 加载外部程序 (Win API) 01. HINSTANCE ShellExecute( 02. HWND hwnd, //父窗口句柄 03. LPCTSTR lpVerb, //要执行的动作:edit,explore,find,open,print,properties 04. LPCTSTR lpFile, //文件名 05. LPCTSTR lpParameters, //传递的命令行参数 06. LPCTSTR lpDirectory, //缺省工作目录 07. INT nShowCmd //窗口的显示模式 08. ); 09. 10. UINT WinExec( 11. LPCSTR lpCmdLine, // 命令的字符串 12. UINT uCmdShow //窗口的显示模式 13. ); 6. 此外,我在程序中使用了 COM 库的缺省 IMalloc 接口管理缓冲区 1. HRESULT CoGetMalloc( //这是一个Win API 2. DWORD dwMemContext, //决定该内存块是否被共享的标志 3. LPMALLOC * ppMalloc //接受返回的内存分配器的IMalloc接口指针 4. ); IMalloc::Alloc() ,IMalloc::Free() 的使用与 C 语言中的 alloc() 和 free() 类似,在此不再赘述。 (二)实现步骤: 在 StdAfx.h 中加入以下的头文件:objidl.h ,afxole.h ,afxpriv.h ,afxtempl.h 1.在 CSmartFoxApp::InitInstance() 中加入: ::CoInitialize(NULL); 2.为 CSmartFoxDlg 添加如下数据成员: 1. IStorage * m_pRootStg; // 根存储的接口指针 2. IStorage * m_pVer30Stg; //Ver30 存储的接口指针 3. IStream * m_pStream; //accounts.cfg 流的接口指针 4. char * m_pBuffer; // 用以读写 accounts.cfg 流的缓冲区指针 5. char m_Driver; // 闪存的当前盘符 6. CArray m_aryPosition; // 保存流中账号目录所在偏移地址的数组 3.利用 Class Wizard 为 CSmartFoxDlg 添加或修改下列函数: 001. BOOL CSmartFoxDlg::OnInitDialog() 002. { 003. CDialog::OnInitDialog(); 004. SetIcon(m_hIcon, TRUE); // Set big icon 005. SetIcon(m_hIcon, FALSE); // Set small icon 006. try 007. { 008. m_Driver = GetDriver(); 009. m_pStream = GetIStream(); 010. m_pBuffer = GetBuffer(m_pStream); 011. GetAccountInfo(m_pStream, m_pBuffer); 012. } 013. catch ( char * sMsg) 014. { 015. AfxMessageBox(sMsg,MB_OK,NULL); 016. ClearUp(); 017. } 018. return TRUE; // return TRUE unless you set the focus to a control 019. } 020. 021. void CSmartFoxDlg::OnClickEmail() 022. { 023. ShellExecute( this ->m_hWnd, 024. "open" , 025. "mailto: korby@sohu.com?subject=Re: 关于SmartFox的意见" , 026. NULL, 027. NULL, 028. SW_SHOWNORMAL); 029. ClearUp(); 030. } 031. 032. void CSmartFoxDlg::OnLButtonDown( UINT nFlags, CPoint point) 033. { 034. // I will modify the driver letter of accounts here, and startup FoxMail. 035. try 036. { 037. ModifyAccountDriver(m_pStream, m_pBuffer); 038. ClearUp(); 039. if (WinExec( "FoxMail.EXE" , SW_SHOWNORMAL) < 31) throw "加载FoxMail.EXE失败" ; 040. } 041. catch ( char * sMsg) 042. { 043. AfxMessageBox(sMsg,MB_OK,NULL); 044. } 045. } 046. 047. void CSmartFoxDlg::OnRButtonDown( UINT nFlags, CPoint point) 048. { 049. // If I don''''t do this, the message will be transferred to window behind. 050. SetCapture(); 051. } 052. 053. void CSmartFoxDlg::OnRButtonUp( UINT nFlags, CPoint point) 054. { 055. ::ReleaseCapture(); 056. ClearUp(); 057. } 058. 059. void CSmartFoxDlg::ClearUp() 060. { 061. if (m_pRootStg != NULL) m_pRootStg->Release(); 062. if (m_pVer30Stg != NULL) m_pVer30Stg->Release(); 063. if (m_pStream != NULL) m_pStream->Release(); 064. if (m_pBuffer != NULL) 065. { 066. IMalloc * pMalloc; 067. ::CoGetMalloc(MEMCTX_TASK, &pMalloc); 068. pMalloc->Free(m_pBuffer); 069. pMalloc->Release(); 070. } 071. ::CoUninitialize(); 072. OnCancel(); 073. } 074. 075. // All codes below find out driver letter and directories of each account. 076. char CSmartFoxDlg::GetDriver() 077. { 078. char sCurDir[256]; 079. int ret = ::GetCurrentDirectory(256, sCurDir); 080. if (ret == NULL) throw "取当前驱动器盘符时失败" ; 081. else return sCurDir[0]; 082. } 083. 084. IStream * CSmartFoxDlg::GetIStream() 085. { 086. USES_CONVERSION; 087. // Get interface Storage pointer of Accounts.CFG 088. IStream * pStream; 089. HRESULT hr; 090. hr = ::StgOpenStorage(T2COLE( "Accounts.CFG" ), 091. NULL, 092. STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 093. NULL, 094. 0, 095. &m_pRootStg); 096. if (hr != S_OK) throw "打开Accounts.CFG文件时失败" ; 097. 098. hr = m_pRootStg->OpenStorage(T2COLE( "Ver30" ), 099. NULL, 100. STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 101. NULL, 102. 0, 103. &m_pVer30Stg); 104. if (hr != S_OK) throw "打开Ver30存储时失败" ; 105. 106. hr = m_pVer30Stg->OpenStream(T2COLE( "accounts.cfg" ), 107. NULL, 108. STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 109. NULL, 110. &pStream); 111. if (hr != S_OK) throw "打开accounts.cfg流时失败" ; 112. 113. return pStream; 114. } 115. 116. char * CSmartFoxDlg::GetBuffer(IStream * pStream) 117. { 118. STATSTG StatStg; 119. IMalloc * pMalloc; 120. char * pBuffer; 121. HRESULT hr; 122. 123. hr = pStream->Stat(&StatStg, NULL); 124. if (hr != S_OK) throw "读取accounts.cfg流的大小时失败" ; 125. 126. hr = ::CoGetMalloc(MEMCTX_TASK, &pMalloc); 127. if (hr != S_OK) throw "获取COM库的IMalloc接口指针时失败" ; 128. 129. pBuffer = ( char *)pMalloc->Alloc( ULONG (StatStg.cbSize.QuadPart)); 130. if (pBuffer == NULL) throw "申请缓冲区时失败" ; 131. 132. pMalloc->Release(); 133. return pBuffer; 134. } 135. 136. void CSmartFoxDlg::GetAccountInfo(IStream * pStream, char * pBuffer) 137. { 138. // I will find out names and directories of each account. 139. STATSTG StatStg; 140. ULONG cbReaded; 141. HRESULT hr; 142. 143. char * p = pBuffer; 144. DWORD cAccount; 145. DWORD len; 146. CString name; // Gets the name of account. 147. CString path; // Gets the path of account. 148. 149. CString strEdit; //Displays text in edit control. 150. CEdit * pEdit = (CEdit *) GetDlgItem(IDC_EDIT); 151. strEdit.Format( "闪存当前为%c:盘, 现存账号及其目录: \r\n" , m_Driver); 152. 153. hr = pStream->Stat(&StatStg, NULL); 154. if (hr != S_OK) throw "读取accounts.cfg流的大小时失败" ; 155. 156. hr = pStream->Read(pBuffer, ULONG (StatStg.cbSize.QuadPart), &cbReaded); 157. if (hr != S_OK) throw "读取accounts.cfg流的内容时失败" ; 158. 159. p += 0x40; 160. cAccount = (*p); //Count of accounts. 161. p += 0x4; 162. 163. for ( DWORD i = 1; i <= cAccount; i++) 164. { 165. // Value of (*p) is index of account. 166. if ( DWORD (*p) != i) throw "accounts.cfg流损坏" ; 167. p += 0x4; //Skips the index number. 168. len = DWORD (*p); //Gets length of name. 169. p += 0x4; //Skips the length number. The string does not include NULL. 170. 171. name.Empty(); 172. path.Empty(); 173. 174. for ( DWORD n = 0; n < len; n++, p++) name += char (*p); // Gets account name. 175. 176. len = DWORD (*p); // Gets length of directory. 177. p += 4; // Skips the length number. 178. 179. m_aryPosition.Add(p-pBuffer); //Stores offset into array. 180. 181. for (n = 0; n< len; n++, p++) path += char (*p); //Gets account path. 182. 183. strEdit += name+ "\t" +path+ "\r\n" ; 184. 185. p += 0x18; //Skips 0x18 Nulls. 186. } 187. pEdit->SetWindowText(strEdit); 188. } 189. 190. void CSmartFoxDlg::ModifyAccountDriver(IStream *pStream, char * pBuffer) 191. { 192. STATSTG StatStg; 193. ULONG cbWrited; 194. HRESULT hr; 195. 196. LARGE_INTEGER MovOffset; 197. ULARGE_INTEGER NewPosition; 198. 199. char * p; 200. 201. for ( int i = 0; i< m_aryPosition.GetSize(); i++) 202. { 203. p = pBuffer + m_aryPosition.GetAt(i); 204. (*p) = m_Driver; 205. } 206. 207. hr = pStream->Stat(&StatStg, NULL); 208. if (hr != S_OK) throw "读取accounts.cfg流的大小时失败" ; 209. 210. MovOffset.QuadPart = 0; 211. hr = pStream->Seek(MovOffset, 0, &NewPosition); 212. if (hr != S_OK) throw "移动accounts.cfg流的读写指针时失败" ; 213. 214. pStream->Write(pBuffer, ULONG (StatStg.cbSize.QuadPart), &cbWrited); 215. if (hr != S_OK) throw "向accounts.cfg流写入数据时失败" ; 216. } 编译链接生成 SmartFox.EXE 后,将其拷贝到 Foxmail 在闪存上的安装目录。利用 SmartFox.EXE 运行 Foxmail 就可以实现“随身邮”的功能了! |
|