#include "version.h"

//---------------------------------------------------------------------------------
char * getVersion(const char *lpFileName)
{
	VS_FIXEDFILEINFO *pInfo;
	DWORD handle;
	BYTE *data;
	char *version=0;

	DWORD size=GetFileVersionInfoSize(lpFileName, &handle);
	if(size > 0){
		data=new BYTE[size];
		if(GetFileVersionInfo(lpFileName, handle, size, data)){

			UINT len;
			VerQueryValue(data, "\\", (LPVOID*)&pInfo, &len);

			int numbers[4];
			numbers[0]=HIWORD(pInfo->dwFileVersionMS);
			numbers[1]=LOWORD(pInfo->dwFileVersionMS);
			numbers[2]=HIWORD(pInfo->dwFileVersionLS);
			numbers[3]=LOWORD(pInfo->dwFileVersionLS);

			char tmp[6];
			version=new char[5 * 4 + 3 + 1]; // max 5 characters for 1 number, 3 dots, null terminator
			char *p=version;
			for(int i=0; i < 4; i++){
				itoa(numbers[i], tmp, 10);
				size_t len=strlen(tmp);
				memcpy(p, tmp, len);
				p+=len;
				if(i!=3)
					*p++='.';
			}
			*p=0;
		}
		delete[] data;
	}
	if(version==NULL){
		version=new char[1];
		version[0]=0;
	}
	return version;
}
//---------------------------------------------------------------------------------
char * getMyVersion(HMODULE hModule)
{
	char name[MAX_PATH];
	GetModuleFileName(hModule, name, sizeof(name));
	return getVersion(name);
}
//---------------------------------------------------------------------------------