c获取exe和dll的版本信息
struct progress { char strFullPath[256]; //程序绝对路径 char strSoftwareName[256]; //软件名称 char strSoftwareVersion[256]; //软件版本号 char strSoftwareVendors[256]; //软件厂商 char strStratTime[16]; //201509251313,软件启动时间,当时间为“”则表示软件当前未启动 unsigned iStartCount; //软件启动次数 unsigned iRunintTotalTime; //软件运行总时间 }; void GetInfoFromExeAndDll(progress &proItem) { struct LANGANDCODEPAGE { WORD wLanguage; WORD wCodePage; }; DWORD dwSize = 0; UINT uiSize = GetFileVersionInfoSize(proItem.strFullPath, &dwSize); if (0 == uiSize) { return; } PTSTR pBuffer = new TCHAR[uiSize]; if (NULL == pBuffer) { return; } memset((void*)pBuffer, 0, uiSize); //获取exe 或 DLL 的资源信息,存放在pBuffer内 if (!GetFileVersionInfo(proItem.strFullPath, 0, uiSize, (PVOID)pBuffer)) { return; } LANGANDCODEPAGE *pLanguage = NULL; //这里这样设置没关系了。 UINT uiOtherSize = 0; //获取资源相关的 codepage 和language if (!VerQueryValue(pBuffer, "\\VarFileInfo\\Translation",(PVOID*)&pLanguage, &uiOtherSize)) { return; } //重点 char* pTmp = NULL; TCHAR SubBlock[MAX_PATH]; memset((void*)SubBlock, 0, sizeof(SubBlock)); UINT uLen = 0; //获取每种 CodePage 和 Language 资源的相关信息
//这个地方原本是用for循环获取每一种CodePage 和 Language,但是获取了他的长度后发现,长度太短了,而我们想要获取的东西却又比较多,经过测试,只使用第一组数据即可
//在字符串格式化的\\最后一个字符串的名称就是我们想要获取的属性名称,它们可以是
/*
CompanyName FileDescription FileVersion InternalName LegalCopyright OriginalFilename ProductName ProductVersion Comments LegalTrademarks PrivateBuild SpecialBuild
*/
int ret = uiOtherSize / sizeof(LANGANDCODEPAGE); if (ret > 0) { wsprintf(SubBlock,TEXT("\\StringFileInfo\\%04x%04x\\ProductName"),pLanguage[0].wLanguage,pLanguage[0].wCodePage); if (VerQueryValue(pBuffer, SubBlock, (PVOID*)&pTmp, &uLen)) { if (strlen(pTmp)>0) strcpy_s(proItem.strSoftwareName, pTmp); } wsprintf(SubBlock, TEXT("\\StringFileInfo\\%04x%04x\\CompanyName"), pLanguage[0].wLanguage, pLanguage[0].wCodePage); if(VerQueryValue(pBuffer, SubBlock, (PVOID*)&pTmp, &uLen)) strcpy_s(proItem.strSoftwareVendors, pTmp); wsprintf(SubBlock, TEXT("\\StringFileInfo\\%04x%04x\\FileVersion"), pLanguage[0].wLanguage, pLanguage[0].wCodePage); if(VerQueryValue(pBuffer, SubBlock, (PVOID*)&pTmp, &uLen)) strcpy_s(proItem.strSoftwareVersion, pTmp); } delete[]pBuffer; pBuffer = NULL; }