2012. 4. 30. 08:39

PE 포맷 바이너리 파일의 버전을 구하는 함수


와 같이 fileversion을 구하는 함수를 공유합니다.
fileversion은 숫자가 4개로 구성되는데, 각 숫자값을 전달해 줍니다.

// version.lib import
#include <assert.h>
#include <winver.h>

DWORD GetVersion(IN LPCTSTR lpszFilePath, OUT PWORD pwVer1, OUT PWORD pwVer2, OUT PWORD pwVer3, OUT PWORD pwVer4)
{
	DWORD				dwRtnValue				= ERROR_SUCCESS;
	DWORD				dwHandle				= 0;
	DWORD				dwDataSize				= 0;
	LPBYTE				bufVersionData			= NULL;
	TCHAR				szValue[MAX_PATH]		= {0,};
	LPVOID				pData					= NULL;
	UINT				nQuerySize				= 0;
	VS_FIXEDFILEINFO*	pVsffi					= NULL;

	if ((NULL == lpszFilePath) || (NULL == pwVer1) || (NULL == pwVer2) || (NULL == pwVer3) || (NULL == pwVer4))
	{
		dwRtnValue = ERROR_INVALID_PARAMETER;
		assert(FALSE);
		goto FINAL;
	}

	// Clear output
	*pwVer1 = 0;
	*pwVer2 = 0;
	*pwVer3 = 0;
	*pwVer4 = 0;

	dwDataSize = ::GetFileVersionInfoSize(lpszFilePath, &dwHandle);
	if (0 == dwDataSize)
	{
		dwRtnValue = ::GetLastError();
		assert(FALSE);
		goto FINAL;
	}

	bufVersionData = new BYTE[dwDataSize];
	if (NULL == bufVersionData)
	{
		dwRtnValue = ERROR_NOT_ENOUGH_MEMORY;
		assert(FALSE);
		goto FINAL;
	}
	ZeroMemory(bufVersionData, sizeof(BYTE)*dwDataSize);

	if (FALSE == ::GetFileVersionInfo(lpszFilePath, 
									  dwHandle, 
									  dwDataSize, 
									  bufVersionData))
	{
		dwRtnValue = ::GetLastError();
		assert(FALSE);
		goto FINAL;
	}

	if (FALSE == ::VerQueryValue(bufVersionData, 
								 TEXT("\\"), 
								 (LPVOID*)&pVsffi, 
								 &nQuerySize))
	{
		dwRtnValue = ::GetLastError();
		assert(FALSE);
		goto FINAL;
	}

	*pwVer1 = HIWORD(pVsffi->dwFileVersionMS);
	*pwVer2 = LOWORD(pVsffi->dwFileVersionMS);
	*pwVer3 = HIWORD(pVsffi->dwFileVersionLS);
	*pwVer4 = LOWORD(pVsffi->dwFileVersionLS);

FINAL:

	if (NULL != bufVersionData)
	{
		delete [] bufVersionData;
		bufVersionData = NULL;
	}

	return dwRtnValue;
}