• Advertisement

Archived

This topic is now archived and is closed to further replies.

help using VerQueryValue

This topic is 5026 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I been trying for a while to figureout the correct way to use this function. I want to get the productversion (or any other version for tht matter) of a file. I never had this much problem trying to find out how to use a function before, especially since I looked in the docs. Anyway here is the code.
std::string GetFileName( HMODULE hModule )
{
   char lpFilename[ MAX_PATH ];
   ::ZeroMemory( lpFilename, MAX_PATH );
   ::GetModuleFileName( hModule, (LPTSTR) lpFilename, MAX_PATH );
   return std::string( lpFilename );
}

std::string GetFileVersion( HMODULE hModule )
{
   DWORD           zero = 0;
   VS_FIXEDFILEINFO version;

   ::ZeroMemory( &version, sizeof(VS_FIXEDFILEINFO) );

   std::string filename = GetFileName( hModule );

   if( !filename.size() )
   {
      // handle error

   }

   if( ::GetFileVersionInfo( (LPTSTR) filename.c_str(), 0, ::GetFileVersionInfoSize(  (LPTSTR) filename.c_str(), &zero ), &version  ) )
   {

   }
   else
   {
      // Handle error

   }

   TCHAR buffer[MAX_PATH];
   UINT   Len = MAX_PATH;
   ::ZeroMemory( buffer, MAX_PATH );

   if( ::VerQueryValue( &version, (LPTSTR) TEXT("\\VarFileInfo\\ProductVersion"),
                        &buffer, &Len ) )
   {

   }
   else
   {
      // Handle error

   }

   return std::string( buffer );
}
I was hoping that anyone that has tried to use this function before could post the way they did it. Right now this wont compile because buffer is not a void **. thanks
gdipong oglpong [edited by - try_catch_this on April 15, 2004 7:30:43 PM]

Share this post


Link to post
Share on other sites
Advertisement
If I recall correctly, (and I may not, it''s been a while since I''ve done this), lplpBuffer shouldn''t be initialized with an existing buffer. The VerQueryValue() function will allocate the buffer itself (and that buffer will get freed when the pBlock is freed). So what the lplpBuffer function wants is the address of a pointer where it can stick the address of the buffer that it allocates, much in the same way it wants the address of a integer to stick the size into.

Share this post


Link to post
Share on other sites
Thanks for the explanation.

But what I rwally need is code that works. The example on msdn sux. It leaves too much unexplained.

Share this post


Link to post
Share on other sites
Well, specifically for the VerQueryValue() part this should get it to compile:

LPVOID buffer = 0;
UINT Len = 0;

if( ::VerQueryValue( &version, (LPTSTR) TEXT("\\VarFileInfo\\ProductVersion"),
&buffer, &Len )

You''ll need to do some casts to actually use the buffer pointer.

Share this post


Link to post
Share on other sites

std::string GetFileName( HMODULE hModule )
{
char lpFilename[ MAX_PATH ];
::ZeroMemory( lpFilename, MAX_PATH );
::GetModuleFileName( hModule, (LPTSTR) lpFilename, MAX_PATH );
return std::string( lpFilename );
}

std::string GetFileVersionValue( HMODULE hModule, std::string info )
{
DWORD zero = 0;
DWORD dwLen = 0;
C_Errors ErrorHandlers;

std::string filename = GetFileName( hModule );

if( !filename.size() )
{
// handle error

return std::string( ErrorHandlers.GetLastSystemError() );
}

if( !( dwLen = ::GetFileVersionInfoSize( (LPTSTR)filename.c_str(), &zero ) ) )
{
// handle error

return std::string( ErrorHandlers.GetLastSystemError() );
}

LPVOID version[ dwLen ];

if( ::GetFileVersionInfo( (LPTSTR) filename.c_str(), 0, dwLen, version ) )
{

}
else
{
// Handle error

return std::string( ErrorHandlers.GetLastSystemError() );
}

LPVOID buffer = 0;
UINT Len = 0;

std::string SubBlock( "\\StringFileInfo\\040904E4\\" );
SubBlock += info;

if( ::VerQueryValue( version, (LPTSTR)SubBlock.c_str(), &buffer, &Len ) )
{

}
else
{
// Handle error

return std::string( ErrorHandlers.GetLastSystemError() );
}

return std::string( (char*)buffer );
}


This works.

Share this post


Link to post
Share on other sites

  • Advertisement