Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Product Version Comparision

Status
Not open for further replies.

Iceman2005

Programmer
May 6, 2005
22
0
0
US
Guys, is there an already good product version comparsion function i can use? Or do i need to write one? if so, how should i do it?

I would like to determine if a product version is <, >, ==, >=, <= to another version...

Like how would i write a function that compares and determine if ver1 >= ver2 where i need to consider cases where version could be.....

2.3.0
2.0.0.5
8.0034.234.4
0.56
2007.0046.234.12345.76
7

and i need to compare them?


 
I don't know of any, but you might be able to find something on Google.
Otherwise, you'll have to do some string parsing, and it would probably be a good idea to create a ProductVersion class that you can use to overload the comparison operators.
 
Try this improvisation:
Code:
/// 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;
  }
}
Add version.lib system library in your project settings Link tab.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top