/// Place this declaration in header file (filever.h)
class FileVer
{
public:
enum Info { Product, File };
FileVer():dfd(false) {}
FileVer(unsigned x, unsigned y = 0, unsigned z = 0, unsigned t = 0);
bool isOK() const { return dfd; }
operator bool() const { return dfd; }
bool operator < (const FileVer& v) const
{
return (isOK() && v.isOK()) && (ms < v.ms || ms == v.ms && ls < v.ls);
}
bool operator >=(const FileVer& v) const
{
return (isOK() && v.isOK()) && (ms > v.ms || ms == v.ms && ls >= v.ls);
}
bool operator ==(const FileVer& v) const
{
return (isOK() && v.isOK()) && ms == v.ms && ls == v.ls;
}
bool operator !=(const FileVer& v) const { return !(*this == v); }
bool operator > (const FileVer& v) const
{
return (isOK() && v.isOK()) && (ms > v.ms || ms == v.ms && ls > v.ls);
}
bool operator <=(const FileVer& v) const
{
return (isOK() && v.isOK()) && (ms < v.ms || ms == v.ms && ls <= v.ls);
}
bool getVer(const char* fname, Info t = File);
bool getVer(const std::string& fname, Info t)
{
return getVer(fname.c_str(),t);
}
std::string toStr() const;
private:
unsigned ms;
unsigned ls;
bool dfd;
};
inline std::ostream& operator << (std::ostream& os, const FileVer& v)
{
os << v.toStr();
return os;
}
// Place the text below in the implementation cpp file.
// Don't forget to compile with
// #include <windows.h>
// #include <string>
// #include <ostream>
// #include "filever.h"
FileVer::FileVer(unsigned x, unsigned y, unsigned z, unsigned t):
ms((x<<16)|(y&0xFFFFu)), ls((z<<16)|(t&0xFFFFu)),dfd(true)
{
}
bool FileVer::getVer(const char* fname, FileVer::Info t)
{
dfd = false;
DWORD junk, insz;
insz = GetFileVersionInfoSize((char*)fname,&junk);
if (insz)
{
char* p = new char[insz];
if (GetFileVersionInfo((char*)fname,0,insz,p))
{
void* ptr = 0;
unsigned len = 0;
if (VerQueryValue(p,"\\",&ptr,&len) && ptr && len)
{
VS_FIXEDFILEINFO* q = (VS_FIXEDFILEINFO*)ptr;
if (t == FileVer::Info::File)
{
ms = q->dwFileVersionMS;
ls = q->dwFileVersionLS;
}
else
{
ms = q->dwProductVersionMS;
ls = q->dwProductVersionLS;
}
dfd = true;
}
}
delete [] p;
}
return isOK();
}
std::string FileVer::toStr() const
{
if (!isOK())
return "?.?.?.?";
else
{
char v[32];
sprintf(v,"%u.%u.%u.%u",
ms>>16,ms&0xFFFFu,ls>>16,ls&0xFFFFu);
return v;
}
}