Click here to Skip to main content
15,891,880 members
Articles / Desktop Programming / MFC
Article

How to retrieve file information

Rate me:
Please Sign up or sign in to vote.
1.38/5 (9 votes)
27 Jun 2005 38.7K   12   2
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);

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhat are the admission parameters of the function? Pin
Merinat16-Jul-07 3:16
Merinat16-Jul-07 3:16 
GeneralCouple of issues Pin
FlySolo12-Dec-06 13:25
FlySolo12-Dec-06 13:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.