123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #include "stdafx.h"
- #include <memory>
- #ifndef _T
- #ifndef _INC_TCHAR
- #include < TChar.h >
- #endif
- #endif
- #ifndef ASSERT
- #ifndef _INC_CRTDBG
- #include < CrtDbg.h >
- #endif
- #define ASSERT(x) _ASSERTE (x)
- #endif
- #include "FileVersion.hpp"
- #ifndef ASSERT_RETURN
- #define ASSERT_RETURN(x) { ASSERT (0); return x; }
- #endif
- //-----------------------------------------------------------------------------
- // FileVersion
- //-----------------------------------------------------------------------------
- FileVersion::FileVersion (void)
- {
- m_bValid = false;
- ::ZeroMemory (&m_vsffi, sizeof (m_vsffi));
- }
- FileVersion::FileVersion (XPCTSTR lpszFileName)
- {
- m_bValid = false;
- ::ZeroMemory (&m_vsffi, sizeof (m_vsffi));
- Open (lpszFileName);
- }
- FileVersion::~FileVersion (void)
- {
- Close ();
- }
- void FileVersion::Close (void)
- {
- m_bValid = false;
- ::ZeroMemory (&m_vsffi, sizeof (m_vsffi));
- }
- bool FileVersion::Open (IN XPCTSTR lpszFileName)
- {
- if (lpszFileName == nullptr)
- ASSERT_RETURN (FALSE);
- Close ();
- if (! GetVersionInfo (lpszFileName))
- Close ();
- return m_bValid;
- }
- bool FileVersion::GetVersionInfo (IN XPCTSTR FileName)
- {
- XPCTSTR lpszFileName = const_cast <XPCTSTR> (FileName);
- DWORD dwDummy = 0;
- int dwSize = ::GetFileVersionInfoSizeA (lpszFileName, &dwDummy);
- if (dwSize <= 0)
- return false;
- auto tmpBuf = std::unique_ptr <BYTE> (new BYTE [dwSize]);
- auto tmpVIB = tmpBuf.get ();
- if (tmpVIB == nullptr) return false;
- if (::GetFileVersionInfoA (lpszFileName, 0, dwSize, tmpVIB))
- {
- UINT uLen = 0;
- LPVOID lpVSFFI = nullptr;
-
- if (::VerQueryValue (tmpVIB, _T ("\\"), (LPVOID *) &lpVSFFI, &uLen)) // root
- {
- ::CopyMemory (&m_vsffi, lpVSFFI, sizeof (VS_FIXEDFILEINFO));
- m_bValid = (m_vsffi.dwSignature == VS_FFI_SIGNATURE);
- }
- }
- return m_bValid;
- }
- std::string FileVersion::GetVersionString () const
- {
- if (! m_bValid) return std::string ();
- return
- std::to_string (GetVersionMajor ()) + "." +
- std::to_string (GetVersionMinor ()) + "." +
- std::to_string (GetVersionBuild ()) + "." +
- std::to_string (GetVersionRevision ());
- }
|