D
driayu
Unregistered / Unconfirmed
GUEST, unregistred user!
修改PE文件版本信息(简单演示)收藏struct VS_VERSIONINFO { WORD wLength; WORD wValueLength; WORD wType; WCHAR szKey[]; WORD Padding1[]; VS_FIXEDFILEINFO Value; WORD Padding2[]; WORD Children[]; }; struct KString { WORD wLength; WORD wValueLength; WORD wType; WCHAR szKey[]; WORD Padding[]; WORD Value[]; }; struct StringTable { WORD wLength; WORD wValueLength; WORD wType; WCHAR szKey[]; WORD Padding[]; KString Children[]; }; struct StringFileInfo { WORD wLength; WORD wValueLength; WORD wType; WCHAR szKey[]; WORD Padding[]; StringTable Children[]; }; DWORD versioninfosize = GetFileVersionInfoSize("c://TestVerInfo.exe",0); if(versioninfosize > 0){ PWideChar versioninfodata=(PWideChar)new BYTE[versioninfosize]; try{ ZeroMemory(versioninfodata,versioninfosize); if(GetFileVersionInfo("c://TestVerInfo.exe",0,versioninfosize,versioninfodata)){ UINT uTemp = 0 ; VS_VERSIONINFO * pVerInfo; LPBYTE pOffsetBytes; struct LANGANDCODEPAGE { WORD wLanguage; WORD wCodePage; } * lpTranslate = NULL; VerQueryValue(versioninfodata, PChar("//VarFileInfo//Translation"),(LPVOID*)&lpTranslate,&uTemp); AnsiString lengcode=IntToHex(lpTranslate->wLanguage,4)+IntToHex(lpTranslate->wCodePage,4); LPTSTR pValueBuffer; if(!VerQueryValue(versioninfodata,("//StringFileInfo//"+lengcode+"//CompanyName").c_str(),(LPVOID*)&pValueBuffer,&uTemp)) return; int iLen; pVerInfo = (VS_VERSIONINFO *)versioninfodata; char * p; VS_FIXEDFILEINFO *fixInfo; p = ((char * )(pVerInfo->szKey) ) + (wcslen(pVerInfo->szKey)+1 ) * sizeof(wchar_t); iLen = pVerInfo->wLength - (p - (char *)pVerInfo); //VS_VERSIONINFO Padding1[]: wLength + wValueLength + wType + szKey[] 32-bit Alignment int iii = (wcslen(pVerInfo->szKey))*2%4==0?0wcslen(pVerInfo->szKey))*2%4; fixInfo =(VS_FIXEDFILEINFO *) (p + iii); if (pVerInfo->wValueLength == 0 ) { return; }else{ p = (char *)fixInfo + sizeof(VS_FIXEDFILEINFO); iLen -= sizeof(VS_FIXEDFILEINFO); //check VS_FIXEDFILEINFO's Signature if(fixInfo->dwSignature!=0xfeef04bd)return; } StringFileInfo * strFileInfo; strFileInfo = (StringFileInfo *)( p); StringTable *strtab; p = (char *)strFileInfo->szKey + (wcslen(strFileInfo->szKey) + 1) * sizeof(wchar_t); //StringFileInfo Padding[]: wLength + wValueLength + wType + szKey[] 32-bit Alignment iii = (wcslen(strFileInfo->szKey))*2%4==0?0wcslen(strFileInfo->szKey))*2%4; strtab = (StringTable *)(p + iii); p = (char *)strtab->szKey + (wcslen(strtab->szKey)+1) * sizeof(wchar_t); //StringTable Padding[]: wLength + wValueLength + wType + szKey[] 32-bit Alignment iii = (wcslen(strtab->szKey))*2%4==0?0wcslen(strtab->szKey))*2%4; p += iii; while (( (p - (char *)strtab))<strtab->wLength){ KString *ks; ks = (KString *)p; int iilen = wcslen(ks->szKey)+1; p = (char *)ks->szKey + (iilen ) * sizeof(wchar_t) ; //String Padding[]: wLength + wValueLength + wType + szKey[] 32-bit Alignment iii = (iilen - 1)*2%4==0?0iilen - 1)*2%4; p += iii; if (ks->wValueLength) { ShowMessage((wchar_t *)p); } if (!_wcsicmp(ks->szKey,L"CompanyName")){ memcpy(p,L"测试修改公司名/0",((ks->wValueLength-1)> (wcslen(L"测试修改公司名/0")+1)?(wcslen(L"测试修改公司名/0")+1)ks->wValueLength-1))*sizeof(wchar_t)); HANDLE hResource=BeginUpdateResource("c://TestVerInfo.exe",false); if(NULL!=hResource){ if(UpdateResource(hResource, RT_VERSION, MAKEINTRESOURCE(VS_VERSION_INFO), lpTranslate -> wLanguage, versioninfodata, versioninfosize)){ EndUpdateResource(hResource,false); } } break; } p +=((ks->wValueLength) * 2 )+ //32-bit Alignment Adj ((ks->wValueLength) * 2 % 4 ==0?0:2) ; } } }__finally{ delete []versioninfodata; } }