C++ 获取文件版本

static string GetFileVersion() 
{
	char FileName[MAX_PATH] = {0};
	::GetModuleFileName(NULL, FileName, sizeof(FileName) - 1);

	string asVer = "";
	VS_FIXEDFILEINFO *pVsInfo;
	unsigned int iFileInfoSize = sizeof(VS_FIXEDFILEINFO);

	int iVerInfoSize = GetFileVersionInfoSize(FileName, NULL);
	if(iVerInfoSize != 0){
		char *pBuf = NULL;
		while(!pBuf){
			pBuf = new char[iVerInfoSize];
		}
		if(GetFileVersionInfo(FileName, 0, iVerInfoSize, pBuf)){
			if(VerQueryValue(pBuf, "\\", (void **)&pVsInfo, &iFileInfoSize)){
				sprintf(pBuf, "%d.%d.%d.%d", HIWORD(pVsInfo->dwFileVersionMS), LOWORD(pVsInfo->dwFileVersionMS), HIWORD(pVsInfo->dwFileVersionLS), LOWORD(pVsInfo->dwFileVersionLS));
				asVer = pBuf;
			}
		}
		delete pBuf;
	}
	return asVer;
}

你可能感兴趣的:(基础)