问题
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 :-)
回答1:
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.
回答2:
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;
}
来源:https://stackoverflow.com/questions/14254359/c-visualstudio-getfileversioninfo