当前位置:编程学习 > VC++ >>

VC资源文件版本解析类: (2) 源文件

1#include "rcversion.h"
  2#include "..\mysdk\common\c_ex_api.h"
  3#include "..\mysdk\common\macro.h"
  4#include <algorithm>
  5#include <cstring>
  6#include <cassert>
  7using namespace std;
  8
  9CRCVersion::CRCVersion():
 10m_hFile(INVALID_HANDLE_VALUE)
 11,m_ullFileVer(0)
 12,m_ullProductVer(0)
 13,m_ullOtherFileVer(0)
 14,m_ullOtherProductVer(0)
 15{
 16}
 17
 18/// 打开资源文件,后缀名为.rc
 19bool CRCVersion::Open(LPCTSTR lpRCFile)
 20{
 21  const TCHAR* pExt = _tcsstr(lpRCFile,_T(".rc"));
 22    if (NULL == pExt) return false;
 23
 24    m_hFile = CreateFile(lpRCFile,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,
 25                        FILE_ATTRIBUTE_NORMAL,NULL);
 26    if (INVALID_HANDLE_VALUE==m_hFile)
 27        return false;
 28
 29  m_tstrFileName = lpRCFile;    ParseRC();
 30    return true;
 31}
 32/// 保存文件,先保存到一个临时文件,再拷贝覆盖到原文件
 33bool CRCVersion::Save()
 34{
 35    tstring tstrTempFile = m_tstrFileName + _T(".tmp");
 36    HANDLE hTempFile = CreateFile(tstrTempFile.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,
 37                       FILE_ATTRIBUTE_NORMAL,NULL);
 38  if (INVALID_HANDLE_VALUE == hTempFile)
 39        return false;
 40   
 41    SaveContent(hTempFile);
 42    Close();
 43
 44    CloseHandle(hTempFile);
 45    DWORD dwFlag = GetFileAttributes(m_tstrFileName.c_str());
 46    dwFlag &= ~FILE_ATTRIBUTE_READONLY;
 47    SetFileAttributes(m_tstrFileName.c_str(),dwFlag);
 48    DeleteFile(m_tstrFileName.c_str());
 49    MoveFile(tstrTempFile.c_str(),m_tstrFileName.c_str());
 50    DeleteFile(tstrTempFile.c_str());
 51}
 52// 关闭文件
 53void CRCVersion::Close()
 54{
 55  if (INVALID_HANDLE_VALUE != m_hFile)
 56    {
 57        CloseHandle(m_hFile);
 58        m_hFile = INVALID_HANDLE_VALUE;
 59    }
 60}
 61
 62void CRCVersion::SaveContent(HANDLE hFile)
 63{
 64    assert(INVALID_HANDLE_VALUE != hFile);
 65   
 66  DWORD dwWrite, dwWrited;
 67    char  buf[2];
 68    if (UNICODE_BIG == m_nCharEncodeType)
 69    {
 70        memcpy(buf,"\xfe\xff",2);
 71        dwWrite = 2;
 72    }
 73    else if (UNICODE_LITTLE == m_nCharEncodeType)
 74    {
 75        memcpy(buf,"\xff\xfe",2);
 76        dwWrite = 2;
 77    }
 78    else
 79    {
 80        dwWrite = 0;
 81    }
 82    WriteFile(hFile,buf,dwWrite,&dwWrited,NULL);
 83
 84    for (string strLine;!eof();strLine.clear())
 85    {
 86        getline(strLine);
 87        ChangeLine(strLine);
 88        if (ANSI != m_nCharEncodeType)
 89        {
 90            wstring wstrLine;
 91            astr2wstr(strLine.c_str(),wstrLine);
 92            if ((is_big_endian() ^ (UNICODE_BIG==m_nCharEncodeType)))
 93            {
 94                for (const wchar_t* pwc = wstrLine.c_str();*pwc != L'\0';++pwc)
 95                {
 96                    char* pH = (char*)pwc;
 97                    char* pL = (char*)(pH+1);
 98                    swap(*pH,*pL);
 99                }
100            }
101            WriteFile(hFile,wstrLine.c_str(),wstrLine.length()*sizeof(wchar_t),&dwWrited,NULL);
102        }
103        else
104        {
105        WriteFile(hFile,strLine.c_str(),strLine.length()*sizeof(char),&dwWrited,NULL);
106        }
107    }
108}
109
110void CRCVersion::ChangeLine(string& strLine)
111{
112    string strVal;  size_t pos;
113  string strReplace;
114  ULONGLONG ullVer;
115
116    if ((pos = ParseVersionInfo(strLine,"FILEVERSION",strVal)) != string::npos)
117    {
118        GetVersion(&ullVer,NULL);
119        strReplace = VerToStrA(ullVer,',');
120    }
121    else if ((pos = ParseVersionInfo(strLine,"PRODUCTVERSION",strVal)) != string::npos)
122    {
123        GetVersion(NULL,&ullVer);
124        strReplace = VerToStrA(ullVer,',');
125    }
126    else if ((pos = ParseBlockInfo(strLine,"VALUE \"CompanyName\"",strVal)) != string::npos)
127    {
128        tstr2astr(m_tstrCom

补充:软件开发 , Vc ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,