Visual Studio dll obtain information themselves Version

  Now compiled dll, exe version most stress management, can be added by adding Version "resource" in the resource file for the project in Visual Studio, select "Version" entry, and then modify "VS_VERSION_INFO" in "FILEVERSION", "PRODUCTVERSION "and add the version number and other information.

  But after you create, and sometimes you need to obtain the version number in the code, such as used in logging and check. Internet to find where relevant method, find the macro definition, the method "GetFileVersionInfo", "FindResource" three implementations of.

Macro definition

  How to get the version number VS2013 RC file
  through four macro definition digital version number in the public file, use the rc, cpp file, but it can not use the IDE vs open resource files (rc is automatically recorded documents), here, use the environment reference.

GetFileVersionInfo

  GetFileVersionInfo get the version number
  vc get version information resource file software in the ... / Version / VS_VERSION_INFO / value FILEVERSION is
  defined here GetVersionInfo need to enter to be detected dll or exe file name, and then processed to obtain version information, should be noted that, If the problem can not be linked GetFileVersionInfoSize like tips, it may not be introduced "version.lib", plus you can, as detailed in the linker:
  resolve not link GetFileVersionInfoSize, GetFileVersionInfo or VerQueryValue

FindResource

  The FindResource () function
  VC resource file acquired FileVersion method
  by FindResource, LoadResource dll or exe obtain the version information of the current module to enter the handle.

Examples

  Above GetFileVersionInfo, FindResource can obtain the version information of dll or exe, but they need to get the name of dll itself or module handle, if so dll used internally, can be acquired version information itself is now commonly used wrapper function GetSelfModuleHandle.

// 获取自身句柄
HMODULE GetSelfModuleHandle()
{
	MEMORY_BASIC_INFORMATION mbi;
	return ((::VirtualQuery(GetSelfModuleHandle, &mbi, sizeof(mbi)) != 0) ? (HMODULE)mbi.AllocationBase : NULL);
}

// 获取自身路径
const char* GetSelfPath()
{
	char DllDir[512];
	GetModuleFileNameA(GetSelfModuleHandle(), DllDir, 512);
	std::string StrPath(DllDir);
	size_t index = StrPath.find_last_of("/\\");
	StrPath = StrPath.substr(0, index);
	return StrPath.c_str();
}

  

Example 1, to obtain information through their own version GetFileVersionInfo

void GetVersionInfo(char* strVersion)
{
	TCHAR szVersionBuffer[1000] = _T("");
	DWORD dwVerSize;
	DWORD dwHandle;
	dwVerSize = GetFileVersionInfoSizeA(GetSelfPath(), &dwHandle);
	if (dwVerSize == 0)
		return;

	if (GetFileVersionInfoA(GetSelfPath(), 0, dwVerSize, szVersionBuffer))
	{
		VS_FIXEDFILEINFO * pInfo;
		unsigned int nInfoLen;

		if (VerQueryValue(szVersionBuffer, _T("\\"), (void**)&pInfo, &nInfoLen))
		{
			sprintf(strVersion, "%d.%d.%d.%d", 
				HIWORD(pInfo->dwFileVersionMS), LOWORD(pInfo->dwFileVersionMS),
				HIWORD(pInfo->dwFileVersionLS), LOWORD(pInfo->dwFileVersionLS));
			return strVersion;
		}
	}
	
	return;
}

  

Example 2, to obtain information through their own version FindResource

void GetVersionInfo(char* strVersion)
{
	HRSRC hsrc = FindResource(GetSelfModuleHandle(), MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
	HGLOBAL hgbl = LoadResource(GetSelfModuleHandle(), hsrc);
	BYTE *pBt = (BYTE *)LockResource(hgbl);
	VS_FIXEDFILEINFO* pFinfo = (VS_FIXEDFILEINFO*)(pBt + 40);
	sprintf(strVersion, "%d.%d.%d.%d",
		(pFinfo->dwFileVersionMS >> 16) & 0xFF,
		(pFinfo->dwFileVersionMS) & 0xFF,
		(pFinfo->dwFileVersionLS >> 16) & 0xFF,
		(pFinfo->dwFileVersionLS) & 0xFF);
}

  

  

  

Published 24 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/yangjf91/article/details/97789118