GetFileVersionSize / GetFileVersionInfo -> Confusion

Posted on 1998-02-25
Last Modified: 2013-12-03
I have stumbled my way through getting this code to work..  Mostly guess work and looking at someone else's code on this subject.  I am confused about how things are working.  I have several questions on this code marked throughout..  Hopefully someone can explain what I am confused about..


   DWORD FileVersionSize;
   LPTSTR Filename;
   DWORD dwHnd;
   LPVOID pBuffer;
   UINT uVersionLen;

1.)  Is this a good technique for converting an object of CString to LPTSTR?  It seems to work?

     Filename = new char[strlen(TmpStr)+1];

      FileVersionSize = GetFileVersionInfoSize( Filename,&dwHnd);

#2.)  Why does this function require a DWORD Handle if it is never used?

    pBuffer = new LPVOID[FileVersionSize];  //Create Buffer

#3.)  when I allocated the memory for pBuffer did I do it correctly for that type (LPVOID)?

    //Query Value
    VerQueryValue(pBuffer,"\\",(void **)&pFixedInfo,&uVersionLen);

#4.)  Now this is where I am completly lost.  Looking at the documentation for this function under Visual C++ 5.0.  How could one determine that the third parameter had to be of type VSFIXEDFILE_INFO?  The doc says this parameter "Points to a buffer that receives a pointer to the version-information value."  Only after looking at this over and over and looking at someone else's code I came to the conclusion that it had to be an Object of type VSFIXEDFILE_INFO.  Why does this parameter have to be a (void **) pointer?

    delete[] Filename;
    delete[] pBuffer;

Any help on any of these questions would greatly be appreciated..  Thanks In Advance,
Question by:larockd
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Accepted Solution

galkin earned 10 total points
ID: 1411567
1.Yes you can convert CString to LPCTSTR since CString class has such an operator.
2.This function require this parameter since it sets it to zero. Ispite of the fact this parameter is never used I guess when this function was designed Windows developers intended to use it but funally then they didn't. But you still must supply valid DWORD variable.
3. When you allocate memory with operator new you must specify type since new allocates specific number of bytes. So specifying type new allocates sizeof(type) * number bytes. void doesn't have meaning of byte so this allocation is incorrect and you must be given compilation error.
4.About how to get version information of specific file I refer you to "codeguru" site at

Author Comment

ID: 1411568
#1.  I am confused on your number one answer.  CString has an operator to convert CString into LPTSTR?  How do I do this?  Right now you can see hwo I am doing it above.  Probably not the most efficient way.

#3.  On the new operation for number three, I do not get a compilation error.  In fact, when all is said and done and I check pFixedInfo it appears to contain all thwe correct values.  
This all might be luck of the draw.  However, since pBuffer needs to be of type LPVOID how should I allocated the buffer based on FileVersionSize which is a DWORD?


Expert Comment

ID: 1411569
1. when you pass CString object where LPCTSTR is expected this operator is called. you can check it by stepping into code.
3.Where in 2 you allocate memory with new LPVOID you should be given a compilation error. If you don't get it it is due to rather sophisticated compiler which treats void as byte. I suggest you new BYTE and then convert result to LPVOID.

Author Comment

ID: 1411570
#1.) This is not happening on my compiler (Visual C++ v5.0 pro).

TmpStr is a CString that contains a Valid Path To A File Name.
I changed parameter one of the GetFileVersionSize call and GetFileVersionInfo call directly to the CString Object.  As above it was previously Filename which I manually converted to LPTSTR.  When I looked at your answer again you are referring to LPCTSTR, However I need LPTSTR for that parameter.

#3.)  My compiler is Visual C++.  So the way I should do it is

    pBuffer = new BYTE[FileVersionSize];

How would I convert the result to LPVOID?  In fact wouldn't the result already be in LPVOID form since I declared  pBuffer is of type LPVOID?

Thanks For your time.

When I compile this code with the CString Objects instead of the LPTSTR I get the following Errors.
--------------------Configuration: LORDSED - Win32 Debug--------------------
F:\Program Files\DevStudio\MyProjects\LordsBKup\LORDSEDDlg.cpp(185) : error C2664: 'GetFileVersionInfoSizeA' : cannot convert parameter 1 from 'class CString' to 'char *'
F:\Program Files\DevStudio\MyProjects\LordsBKup\LORDSEDDlg.cpp(189) : error C2664: 'GetFileVersionInfoA' : cannot convert parameter 1 from 'class CString' to 'char *'
Error executing cl.exe.

LORDSED.exe - 2 error(s), 0 warning(s)

Now if I change the parameter to the LPTSTR Filename it compiles.
Why does this happen.  Above you said it should automatically convert it?  Even if I type cast it, it will error (LPTSTR)TmpStr.

Filename = new char[strlen(TmpStr)+1];

FileVersionSize = GetFileVersionInfoSize( TmpStr,&dwHnd);

pBuffer = new LPVOID[FileVersionSize];  //Create Buffer


Expert Comment

ID: 1411571
1.You get en error since char* is ecpected in GetFileVersionInfoSize. CString has LPCTSTR operator which converts to const char in ANSI. You should explicity convert it to char. (LPTSTR)(LPCTSTR)strParam.
2.When you allocate memory you are given pointer to the same type you allocate. Since in C++ pointer of any type can be converted to pointer of another type( in some cases compiler does the job in other cases explicit conversion is required) you can later conver LPBYTE to LPVOID when you nedd to pass this parameter and LPVOID is expected.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question