65.9K
CodeProject is changing. Read more.
Home

How to retrieve file information

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.38/5 (9 votes)

Jun 28, 2005

viewsIcon

38958

A simple function to retrieve file information as file version, product info etc.

Introduction

This article reports a short function (Borland C++) useful to retrieve all the information stored in a file. Information like product info, file version, and so on can be retrieved.

The body of the function is:

int GetFileInfo(AnsiString filepath, 
    AnsiString fileinfo, AnsiString *fileinfovalue)
{
  DWORD whandle;
  DWORD rsize;
  int retval;

  rsize=GetFileVersionInfoSize(filepath.c_str(), &whandle);
  if (rsize>0)
  {
    unsigned char *buffer = new unsigned char[rsize];
    if (GetFileVersionInfo(Application->ExeName.c_str(), 
                                   whandle, rsize, buffer))
    {
      unsigned short *subBlock;
      unsigned int len = 0;
      if (VerQueryValue(buffer, 
          "\\VarFileInfo\\Translation", (void **)&subBlock, &len))
      {
        AnsiString spv;
        char *versionInfo;
        len=0;

        spv.sprintf("\\StringFileInfo\\%04x%04x\\", 
                         subBlock[0], subBlock[1]);
        spv+=fileinfo;
        if (VerQueryValue(buffer, spv.c_str(), (void *
                                *)&versionInfo, &len))
        {
          *fileinfovalue=versionInfo;
          retval=0;
        }
        else
          retval=1;
      }
      else
        retval=2;
    }
    else
      retval=3;
  }
  else
    retval=4;

  return retval;
}

And it's used as:

AnsiString asv;
GetFileInfo(Application->ExeName, "ProductVersion", &asv);
ShowMessage(asv);