由于在工作中经常要用到多国语言的语言库资源的修改和更新,于是做了一个小的DLL资源提取工具,快速提取出当前封装在动态语言库中的串表资源。生成的Excel有三列,分别对应的是DLL中串表资源中的ID,Value,Caption
现在把主要的代码写在下面,由于本人刚刚参加工作,水平有限,所以要是有什么写的不好的地方,还请多多指教。
- /*************************************************
- Description: // 从整体上实现了DLL字符串资源的析取输出
- *************************************************/
- void CDLLToolDlg::OnOK()
- {
- hExe = LoadLibrary(m_csLoadPath); //导入选择的动态链接库
- if (hExe == NULL)
- {
- return;
- }
- CreatExcel(); //创建一张Excel表格,用于资源的存储
- ReadHeadFile(); //读取头文件中的ID号和对应的Value值,依Value在DLL中查找出对应的字符串,所有数据写入到Excel表格中
- FreeLibrary(hExe); //释放DLL资源
- CDialog::OnOK();
- }
- /*************************************************
- Function: // CreatExcel
- Description: // 创建一张Excel表格,表的名称是:Resource,表项分别为:TheID, NumValues, TableString
- *************************************************/
- void CDLLToolDlg::CreatExcel()
- {
- CString csDriver;
- CString csExcelFile;
- csDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; // Excel安装驱动
- csExcelFile = m_csOutPath; // 要建立的Excel文件 "c://ClientResCHS.xls" m_csOutPath
- // 创建进行存取的字符串
- csSql.Format("DRIVER={%s};DSN='''';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=/"%s/";DBQ=%s", csDriver, csExcelFile, csExcelFile);
- // 创建数据库 (既Excel表格文件)
- if( cDatabase.OpenEx(csSql, CDatabase::noOdbcDialog) )
- {
- // 创建表结构(TheID, NumValues, TableString)
- csSql = "CREATE TABLE Resource (TheID TEXT, NumValues NUMBER, TableString TEXT)";
- cDatabase.ExecuteSQL(csSql);
- }
- }
- /*************************************************
- Function: // ReadHeadFile
- Description: // 读取头文件中的ID和Value的函数,此方法可以借鉴到其他读取文本文件的操作中
- *************************************************/
- void CDLLToolDlg::ReadHeadFile()
- {
- int iPos;
- int iPosError;
- int iValue;
- int iResult;
- int iNumLength;
- CString csID;
- CString csValue;
- CString csStr2="";
- CString csSubStr;
- CString csSubstring;
- CString csSubstringError;
- CStdioFile cRead;
- char szBuffernew[1000];
- ZeroMemory(szBuffernew,1000);
- if(!cRead.Open(m_csAddHFile, CFile::modeRead)) //打开已经选定的头文件
- {
- AfxMessageBox("Open file error!");
- return;
- }
- while(cRead.ReadString(csStr2)) //CStdioFile是逐行读取数据的,并存入str2中
- {
- csSubstring ="#define " ;
- iPos = csStr2.Find(csSubstring); //查找含有字符子串"#define "的字符串(这里每一行存为一个字符串)
- csSubstringError = "_APS_NEXT";
- iPosError = csStr2.Find(csSubstringError); //查找含有字符子串"_APS_NEXT"的字符串
- if(iPos >= 0 && iPosError < 0) //当含有满足限制要求的字符子串时,进行读写操作
- {
- AfxExtractSubString(csSubStr, csStr2, 1, ' '); // 用空格符来分割字符串,并取出第2个子串,既是ID号
- //得到ID号最后一个字符开始,后面剩下的字符串长度,包括一串空格符和Value(以数字形式)
- iNumLength = csStr2.GetLength() - csSubStr.GetLength() - csSubstring.GetLength();
- //输出ID号最后一个字符开始,后面剩下的字符串(了解Right,left这两个函数的用法)
- csValue = csStr2.Right(iNumLength);
- csValue.TrimLeft(); //去掉字符串左边的空白
- iValue = atoi(csValue); //把Value由字符串类型转化为数字类型
- iResult = LoadString(hExe, iValue, szBuffernew, 1000); //根据iValue的值,从DLL资源中找出对应的字符串
- // CreatResourceLink(iValue, csSubStr, szBuffernew);
- if (iResult != 0)
- {
- WriteToExcel(iValue, csSubStr, szBuffernew); //把所有资源写入Excel表格中
- }
- }
- }
- cDatabase.Close(); // 关闭数据库
- }
- /*************************************************
- Function: // WriteToExcel
- Description: // 把从DLL中导出的字符串资源写入Excel表格中
- Input: // 输入的三个参数分别对应DLL串表中的ID,Value和Caption;
- Output: // 对输出参数的说明
- Return: // 函数返回值的说明
- *************************************************/
- void WriteToExcel(int iValue, CString csID, CString csCaption)
- {
- int iPos;
- int iReplace;
- CString csSubstring ="'" ;
- iPos = csCaption.Find(csSubstring); //判断当前csCaption字符串中是否含有单引号
- //注:在使用SQL语句的时候字符串中的单引号会和语法使用的引号产生匹配冲突
- if(iPos >= 0)
- {
- iReplace = csCaption.Replace("/'", "/''"); //把单引号由“操作符”替换为“普通字符”
- }
- //各项数据插入Excel表格中
- csSql.Format(_T("INSERT INTO Resource (TheID, NumValues, TableString) VALUES ('%s','%u','%s')"), csID, iValue, csCaption);
- cDatabase.ExecuteSQL(csSql);
- }