修改文件版本号方法

  鼠标右键选择文件属性可以显示详细信息, 里面可以显示版本号 : 

  修改这个版本号也简单, 可以直接用 VS 的打开文件的方式打开它(只有这样打开才是查看详细信息的) : 

  然后双击版本信息就能编辑了, 编辑后保存就能修改了 : 

  手动的也挺方便的, 不过如果要使用代码来修改, 就有点麻烦了, 我找到一份C++的代码, 修改了可以正常使用 : 

-- 使用的是 dll 导出工程, 它提供了很多库的自动引用和宏

// pch.h: 预编译标头

#ifndef PCH_H
#define PCH_H

#include "framework.h"
#include <string>

#define LIBEXPORT_API extern "C" __declspec(dllexport)
LIBEXPORT_API BOOL SetFileVersion(LPCTSTR filePath, int major, int minor, int build, int revision = 0);

#endif //PCH_H
// pch.cpp: 与预编译标头对应的源文件

#include "pch.h"
#include <string>
#include <windows.h> 
#include <shlwapi.h> 
#include <tchar.h>


#pragma region Write File Version Info
#pragma comment(lib, "version.lib")
#pragma comment(lib, "shlwapi")

struct
{
    WORD wLanguage;
    WORD wCodePage;
} *lpTranslate;

BOOL SetFileVersion(LPCTSTR filePath, int major, int minor, int build, int revision)
{
    if (NULL == filePath || !PathFileExists(filePath))
        return FALSE;

    BOOL bRet = FALSE;
    DWORD dwHandle = 0;
    DWORD dwSize = 0;

    dwSize = GetFileVersionInfoSize(filePath, &dwHandle);
    if (0 >= dwSize)
        return bRet;

    LPBYTE lpBuffer = new BYTE[dwSize];
    memset(lpBuffer, 0, dwSize);

    if (GetFileVersionInfo(filePath, dwHandle, dwSize, lpBuffer) != FALSE)
    {
        HANDLE hResource = BeginUpdateResource(filePath, FALSE);
        if (NULL != hResource)
        {
            UINT uTemp;
            DWORD dwVer[4] = { 0 };

            if (VerQueryValue(lpBuffer, TEXT("\\VarFileInfo\\Translation"), (LPVOID*)&lpTranslate, &uTemp) != FALSE)
            {
                LPVOID lpFixedBuf = NULL;
                DWORD dwFixedLen = 0;
                if (FALSE != VerQueryValue(lpBuffer, TEXT("\\"), &lpFixedBuf, (PUINT)&dwFixedLen))
                {
                    VS_FIXEDFILEINFO* pFixedInfo = (VS_FIXEDFILEINFO*)lpFixedBuf;

                    long MS = (((long)major) << 16) + minor;
                    long LS = (((long)build) << 16) + revision;

                    pFixedInfo->dwFileVersionMS = MS;
                    pFixedInfo->dwFileVersionLS = LS;

                    dwVer[0] = HIWORD(pFixedInfo->dwFileVersionMS);
                    dwVer[1] = LOWORD(pFixedInfo->dwFileVersionMS);
                    dwVer[2] = HIWORD(pFixedInfo->dwFileVersionLS);
                    dwVer[3] = LOWORD(pFixedInfo->dwFileVersionLS);
                }

                // 修改版本的文本信息
                LPVOID lpStringBuf = NULL;
                DWORD dwStringLen = 0;
                TCHAR szTemp[MAX_PATH] = { 0 };
                TCHAR szVersion[MAX_PATH] = { 0 };

                _stprintf_s(szTemp, MAX_PATH - 1, TEXT("\\StringFileInfo\\%04x%04x\\FileVersion"), lpTranslate->wLanguage, lpTranslate->wCodePage);
                _stprintf_s(szVersion, MAX_PATH - 1, TEXT("%d.%d.%d.%d"), dwVer[0], dwVer[1], dwVer[2], dwVer[3]);

                if (FALSE != VerQueryValue(lpBuffer, szTemp, &lpStringBuf, (PUINT)&dwStringLen))
                    memcpy(lpStringBuf, szVersion, (_tcslen(szVersion) + 1) * sizeof(TCHAR));

                memset(szTemp, 0, sizeof(szTemp));
                _stprintf_s(szTemp, MAX_PATH - 1, TEXT("\\StringFileInfo\\%04x%04x\\ProductVersion"), lpTranslate->wLanguage, lpTranslate->wCodePage);

                if (FALSE != VerQueryValue(lpBuffer, szTemp, &lpStringBuf, (PUINT)&dwStringLen))
                    memcpy(lpStringBuf, szVersion, (_tcslen(szVersion) + 1) * sizeof(TCHAR));

                // 更新  
                if (UpdateResource(hResource, RT_VERSION, MAKEINTRESOURCE(VS_VERSION_INFO), lpTranslate->wLanguage, lpBuffer, dwSize) != FALSE)
                {
                    if (EndUpdateResource(hResource, FALSE) != FALSE)
                        bRet = TRUE;
                }
            }
        }
    }

    if (lpBuffer)
        delete[] lpBuffer;

    return bRet;
}

#pragma endregion Write File Version Info

  这样导出个 dll 就能用了 : 

    [System.Runtime.InteropServices.DllImport("XXXX.dll")]
    public static extern bool SetFileVersion([MarshalAsAttribute(UnmanagedType.LPWStr)] string filePath, int major, int minor, int build, int revision = 0);

 

posted @ 2021-08-31 10:59  tiancaiKG  阅读(2358)  评论(0编辑  收藏  举报