1

i downloaded this class http://www.codeproject.com/file/VersionInfo.asp

i use it to get the file information from a few programs i currently run.

It works fine when i want to information from the program i wrote, but i can't get the file informations of "chrome" or my "visual studio" for example.

when i query the productName of my application i get "Todo:ProductName" because it is not set yet ;)

but when i query the productName of chrome.exe i just get "" nothing.

Does anynone know why?

Edit: i debugged a little and the probleme is here.

BOOL CFileVersionInfo::Open( IN LPCTSTR lpszFileName )
{  
if( lpszFileName == NULL )
    ASSERT_RETURN( FALSE );

Close();
if( !GetVersionInfo( lpszFileName ) || !QueryVersionTrans() )
    Close();

return m_bValid;
};

The GetVersionInfo( lpsz... ) does return 0 when there is "chrome.exe" entered

Edit 2: Yes chrome.exe has an product name field.

example: chrome.exe - File description: Google Chrome - Type: Application - File version: 23.0.1271.97 - Product NAme: Google Chrome - Product Version: 23.0.1271.97 - Copyright: ..blabla... - Size: 1.18MB - Date modified: some_date - Language: English - Original Filename: chrome.exe

Edit 3: How can I get the full path to the exe? I just have the process ID. At the moment iam looping over all processes and search for my processId.

Thanks so far for you answers :-)

hmjd
  • 120,187
  • 20
  • 207
  • 252
Laokoon
  • 1,241
  • 4
  • 24
  • 47

2 Answers2

2

How can I get the full path to the exe?

Use OpenProcess() to get a HANDLE to the process and then use QueryFullProcessImageName() to obtain the full path of the exe.

Remember to check the result of GetLastError() to determine the reason for failure.

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • That's the solution. Thanks ;) – Laokoon Jan 10 '13 at 10:00
  • It should be noted that `QueryFullProcessImageName()` only works with Windows Vista and later. If you're using Windows XP you have to use [GetModuleFileNameEx()](https://msdn.microsoft.com/en-us/library/ms683198(VS.85).aspx) in combination with [EnumProcessModules()](https://msdn.microsoft.com/en-gb/library/windows/desktop/ms682631(v=vs.85).aspx) to retrieve the full file path. – Vinz Oct 11 '16 at 07:27
1

This works fine for me

#include <windows.h>
#include <vector>
#include <string>
#pragma comment( lib, "Version.lib" )

std::string processId_2_version( int processId )
{
    HANDLE h = OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, processId );
    if ( h == 0 )
    {
        return "";
    }
    char exe[ 1024 ];
    DWORD exe_size = 1024;
    QueryFullProcessImageNameA( h, 0, exe, & exe_size );
    CloseHandle( h );
    DWORD dwHandle, sz = GetFileVersionInfoSizeA( exe, & dwHandle );
    if ( 0 == sz )
    {
        return "";
    }
    std::vector< unsigned char > buf( sz );
    if ( !GetFileVersionInfoA( exe, dwHandle, sz, & buf[ 0 ] ) )
    {
        return "";
    }
    VS_FIXEDFILEINFO * pvi;
    sz = sizeof( VS_FIXEDFILEINFO );
    if ( !VerQueryValueA( & buf[ 0 ], "\\", (LPVOID*)&pvi, (unsigned int*)&sz ) )
    {
        return "";
    }
    char ver[ 142 ];
    sprintf( ver, "%d.%d.%d.%d"
            , pvi->dwProductVersionMS >> 16
            , pvi->dwFileVersionMS & 0xFFFF
            , pvi->dwFileVersionLS >> 16
            , pvi->dwFileVersionLS & 0xFFFF
            );
    return ver;
}
borisbn
  • 4,988
  • 25
  • 42