본문 바로가기
프로그래밍/C, C++

MFC - 리소스에서 FileVersion 얻어오기

by 꾸션 2015. 2. 2.
#pragma comment(lib, "version.lib")

void main()
{
  // 버전정보를 담을 버퍼
  char *buffer = NULL;
  // 버전을 확인할 파일
  char *name = "c:\\TestFolder\\test.exe";

  DWORD infoSize = 0;

  // 파일로부터 버전정보데이터의 크기가 얼마인지를 구합니다.
  infoSize = GetFileVersionInfoSize(name, 0);
  if (infoSize == 0)
    return;

  // 버퍼할당
  buffer = new char[infoSize];
  if (buffer)
  {
    // 버전정보데이터를 가져옵니다.
    if (GetFileVersionInfo(name, 0, infoSize, buffer) != 0)
    {
      VS_FIXEDFILEINFO *pFineInfo = NULL;
      UINT bufLen = 0;
      // buffer로 부터 VS_FIXEDFILEINFO 정보를 가져옵니다.
      if (VerQueryValue(buffer, "\\", (LPVOID *)&pFineInfo, &bufLen) != 0)
      {
        WORD majorVer, minorVer, buildNum, revisionNum;
        majorVer = HIWORD(pFineInfo->dwFileVersionMS);
        minorVer = LOWORD(pFineInfo->dwFileVersionMS);
        buildNum = HIWORD(pFineInfo->dwFileVersionLS);
        revisionNum = LOWORD(pFineInfo->dwFileVersionLS);

        // 파일버전 출력
        printf("version : %d,%d,%d,%d\n", majorVer, minorVer, buildNum, revisionNum);
      }
    }
    delete[] buffer;
  }
}

 

자료출처: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646985(v=vs.85).aspx

 

// Get a file information...
// KSA 2010/08/23 add function for get file info (version number & e.t.c)

/*
GetInfo("Comments");
GetInfo("CompanyName");
GetInfo("FileDescription");
GetInfo("FileVersion");
GetInfo("InternalName");
GetInfo("LegalCopyright");
GetInfo("LegalTrademarks");
GetInfo("OriginalFilename");
GetInfo("PrivateBuild");
GetInfo("ProductName");
GetInfo("ProductVersion");
*/

char *GetInfo(char *InfoItem)
{
  static char szResult[256] = {0};
  char szFullPath[256];
  char szGetName[256];
  LPSTR lpVersion;     // String pointer to Item text
  DWORD dwVerInfoSize; // Size of version information block
  DWORD dwVerHnd = 0;  // An 'ignored' parameter, always '0'
  UINT uVersionLen;
  BOOL bRetCode;

  GetModuleFileName(NULL, szFullPath, sizeof(szFullPath));
  dwVerInfoSize = GetFileVersionInfoSize(szFullPath, &dwVerHnd);
  if (dwVerInfoSize)
  {
    LPSTR lpstrVffInfo;
    HANDLE hMem;
    hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
    lpstrVffInfo = (LPSTR)GlobalLock(hMem);
    GetFileVersionInfo(szFullPath, dwVerHnd, dwVerInfoSize, lpstrVffInfo);

    // Get a codepage from base_file_info_sctructure
    lstrcpy(szGetName, "\\VarFileInfo\\Translation");

    uVersionLen = 0;
    lpVersion = NULL;
    bRetCode = VerQueryValue((LPVOID)lpstrVffInfo,
                             (LPSTR)szGetName,
                             (void **)&lpVersion,
                             (UINT *)&uVersionLen);
    if (bRetCode && uVersionLen && lpVersion)
    {
      sprintf(szResult, "%04x%04x", (WORD)(*((DWORD *)lpVersion)),
              (WORD)(*((DWORD *)lpVersion) >> 16));
      // lstrcpy(szResult, lpVersion);
    }
    else
    {
      // 041904b0 is a very common one, because it means:
      // US English/Russia, Windows MultiLingual characterset
      // Or to pull it all apart:
      // 04------        = SUBLANG_ENGLISH_USA
      // --09----        = LANG_ENGLISH
      // --19----        = LANG_RUSSIA
      // ----04b0 = 1200 = Codepage for Windows:Multilingual
      lstrcpy(szResult, "041904b0");
    }

    // Add a codepage to base_file_info_sctructure
    sprintf(szGetName, "\\StringFileInfo\\%s\\", szResult);
    // Get a specific item
    lstrcat(szGetName, InfoItem);

    uVersionLen = 0;
    lpVersion = NULL;
    bRetCode = VerQueryValue((LPVOID)lpstrVffInfo,
                             (LPSTR)szGetName,
                             (void **)&lpVersion,
                             (UINT *)&uVersionLen);
    if (bRetCode && uVersionLen && lpVersion)
    {
      lstrcpy(szResult, lpVersion);
    }
    else
    {
      lstrcpy(szResult, "");
    }
  }
  return szResult;
}

 

반응형

댓글