|
如下如所示,*.dll 或者*.exe文件的版本号分两部分.
第一部分是数值形式的版本号,而第二部分是资源中的文字字符串。
下面的代码做为修改版本号的参考
typedef struct _tagLanguage
{ WORD wLanguage; WORD wCodePage; }
tagLanguage,
*
LPLanguage;
struct VS_VERSIONINFO
{ WORD wLength; WORD wValueLength; WORD wType; WCHAR szKey[
1
]; WORD wPadding1[
1
]; VS_FIXEDFILEINFO Value; WORD wPadding2[
1
]; WORD wChildren[
1
]; }
;
//
these macros help to align on r-byte boundaries (thanks Ted Peck)
#define roundoffs(a,b,r) (((BYTE
*
) (b)
-
(BYTE
*
) (a)
+
((r)
-
1
))
&
~
((r)
-
1
)) #define roundpos(a,b,r) (((BYTE
*
) (a))
+
roundoffs(a,b,r))
{ DWORD dwVerHnd
=
0
; DWORD dwVerInfoSize
=
GetFileVersionInfoSize( m_strLastSignedArchivesDir
+
strShortFilePath,
&
dwVerHnd);
if
( dwVerInfoSize
>
0
)
{ LPWSTR lpstrVffInfo
=
(LPWSTR)
new
BYTE[dwVerInfoSize]; ZeroMemory( lpstrVffInfo, dwVerInfoSize);
if
(GetFileVersionInfo( m_strLastSignedArchivesDir
+
strShortFilePath, dwVerHnd, dwVerInfoSize, lpstrVffInfo))
{ UINT uTemp
=
0
; VS_VERSIONINFO
*
pVerInfo; LPBYTE pOffsetBytes; VS_FIXEDFILEINFO
*
pFixedInfo;
pVerInfo
=
(VS_VERSIONINFO
*
) lpstrVffInfo; pOffsetBytes
=
(BYTE
*
)
&
pVerInfo
->
szKey[_tcslen(pVerInfo
->
szKey)
+
1
]; pFixedInfo
=
(VS_FIXEDFILEINFO
*
) roundpos(pVerInfo, pOffsetBytes,
4
);
pFixedInfo
->
dwFileVersionLS
=
pFixedInfo
->
dwFileVersionLS
+
0x00000001
; pFixedInfo
->
dwProductVersionLS
=
pFixedInfo
->
dwProductVersionLS
+
0x00000001
;
LPLanguage lpTranslate
=
NULL; VerQueryValue( lpstrVffInfo, _T(
"
\\VarFileInfo\\Translation
"
), (LPVOID
*
)
&
lpTranslate,
&
uTemp);
CString strPath; LPTSTR pValueBuffer;
strPath.Format(_T(
"
\\StringFileInfo\\%04x%04x\\FileVersion
"
) , lpTranslate
->
wLanguage , lpTranslate
->
wCodePage );
VerQueryValue( lpstrVffInfo , strPath.GetBuffer() , (LPVOID
*
)
&
pValueBuffer ,
&
uTemp ); strPath.ReleaseBuffer(); ZeroMemory( pValueBuffer, _tcslen(pValueBuffer)
*
sizeof(TCHAR));
strPath.Format(_T(
"
\\StringFileInfo\\%04x%04x\\ProductVersion
"
) , lpTranslate
->
wLanguage , lpTranslate
->
wCodePage );
VerQueryValue( lpstrVffInfo , strPath.GetBuffer() , (LPVOID
*
)
&
pValueBuffer ,
&
uTemp ); strPath.ReleaseBuffer(); ZeroMemory( pValueBuffer, _tcslen(pValueBuffer)
*
sizeof(TCHAR));
HANDLE hResource
=
BeginUpdateResource(strDest, FALSE);
if
(NULL
!=
hResource)
{
if
(UpdateResource(hResource, RT_VERSION, MAKEINTRESOURCE(VS_VERSION_INFO), lpTranslate
->
wLanguage, lpstrVffInfo, dwVerInfoSize)
!=
FALSE) EndUpdateResource(hResource, FALSE); }
}
delete [] lpstrVffInfo; }
}
对于资源中的文本类型的版本号,如FileVersion, ProductVersion, 修改的字符串长度不能超过原长度。如果不足的话末尾用'\0‘填不满。 所以,在编译的时候,尽量在资源中设置一个长一点的字符串来占位,免得到时候修改的时候空间不足。
|