Link to home
Start Free TrialLog in
Avatar of larockd
larockd

asked on

GetFileVersionSize / GetFileVersionInfo -> Confusion

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..



////////GET PRODUCT VERSION INFORMATION

   DWORD FileVersionSize;
   LPTSTR Filename;
   DWORD dwHnd;
   LPVOID pBuffer;
   VS_FIXEDFILEINFO *pFixedInfo;
   UINT uVersionLen;

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

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

    //GET FILE VERSION SIZE INFO
      FileVersionSize = GetFileVersionInfoSize( Filename,&dwHnd);

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

    //GET FILE VERSION INFORMATION
    pBuffer = new LPVOID[FileVersionSize];  //Create Buffer
    GetFileVersionInfo(Filename,dwHnd,FileVersionSize,pBuffer);

#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,
Darrell
ASKER CERTIFIED SOLUTION
Avatar of galkin
galkin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of larockd
larockd

ASKER

#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?

Thanks
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.
Avatar of larockd

ASKER

#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--------------------
Compiling...
LORDSEDDlg.cpp
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.


//CONVERT CString To LPTSTR
Filename = new char[strlen(TmpStr)+1];
strcpy(Filename,TmpStr);

    //GET FILE VERSION SIZE INFO
FileVersionSize = GetFileVersionInfoSize( TmpStr,&dwHnd);

//GET FILE VERSION INFORMATION
pBuffer = new LPVOID[FileVersionSize];  //Create Buffer
GetFileVersionInfo(TmpStr,dwHnd,FileVersionSize,pBuffer);

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.