修改PE文件版本信息(简单演示)

  1. struct VS_VERSIONINFO {
  2.   WORD  wLength;
  3.   WORD  wValueLength;
  4.   WORD  wType;
  5.   WCHAR szKey[];
  6.   WORD  Padding1[];
  7.   VS_FIXEDFILEINFO Value; 
  8.   WORD  Padding2[]; 
  9.   WORD  Children[]; 
  10. };
  11. struct KString {
  12.   WORD   wLength; 
  13.   WORD   wValueLength; 
  14.   WORD   wType; 
  15.   WCHAR  szKey[]; 
  16.   WORD   Padding[]; 
  17.   WORD   Value[]; 
  18. };
  19. struct StringTable {
  20.   WORD   wLength; 
  21.   WORD   wValueLength; 
  22.   WORD   wType; 
  23.   WCHAR  szKey[]; 
  24.   WORD   Padding[]; 
  25.   KString Children[];
  26. };
  27. struct StringFileInfo {
  28.   WORD        wLength;
  29.   WORD        wValueLength;
  30.   WORD        wType;
  31.   WCHAR       szKey[];
  32.   WORD        Padding[];
  33.   StringTable Children[];
  34. };
  1.     DWORD versioninfosize = GetFileVersionInfoSize("c://TestVerInfo.exe",0);
  2.     if(versioninfosize > 0){
  3.         PWideChar versioninfodata=(PWideChar)new BYTE[versioninfosize];
  4.         try{
  5.             ZeroMemory(versioninfodata,versioninfosize);
  6.             if(GetFileVersionInfo("c://TestVerInfo.exe",0,versioninfosize,versioninfodata)){
  7.                 UINT uTemp  =   0 ;
  8.                 VS_VERSIONINFO * pVerInfo;
  9.                 LPBYTE pOffsetBytes;
  10.                 struct LANGANDCODEPAGE {
  11.                     WORD wLanguage;
  12.                     WORD wCodePage;
  13.                 } * lpTranslate  =  NULL;
  14.                 VerQueryValue(versioninfodata, PChar("//VarFileInfo//Translation"),(LPVOID*)&lpTranslate,&uTemp);
  15.                 AnsiString lengcode=IntToHex(lpTranslate->wLanguage,4)+IntToHex(lpTranslate->wCodePage,4);
  16.                 LPTSTR pValueBuffer;
  17.                 if(!VerQueryValue(versioninfodata,("//StringFileInfo//"+lengcode+"//CompanyName").c_str(),(LPVOID*)&pValueBuffer,&uTemp)) return;
  18.                 int iLen;
  19.                 pVerInfo = (VS_VERSIONINFO *)versioninfodata;
  20.                 char * p;
  21.                 VS_FIXEDFILEINFO *fixInfo;
  22.                 p = ((char * )(pVerInfo->szKey) ) + (wcslen(pVerInfo->szKey)+1 ) * sizeof(wchar_t);
  23.                 iLen = pVerInfo->wLength - (p - (char *)pVerInfo);
  24.                 //VS_VERSIONINFO Padding1[]: wLength + wValueLength + wType + szKey[] 32-bit Alignment
  25.                 int iii = (wcslen(pVerInfo->szKey))*2%4==0?0:(wcslen(pVerInfo->szKey))*2%4;
  26.                 fixInfo =(VS_FIXEDFILEINFO *) (p + iii);
  27.                 if (pVerInfo->wValueLength == 0 ) {
  28.                     return;
  29.                 }else{
  30.                     p = (char *)fixInfo + sizeof(VS_FIXEDFILEINFO);
  31.                     iLen -= sizeof(VS_FIXEDFILEINFO);
  32.                     //check VS_FIXEDFILEINFO's Signature
  33.                     if(fixInfo->dwSignature!=0xfeef04bd)return;
  34.                 }
  35.                 StringFileInfo * strFileInfo;
  36.                 strFileInfo = (StringFileInfo *)( p);
  37.                 StringTable *strtab;
  38.                 p = (char *)strFileInfo->szKey + (wcslen(strFileInfo->szKey) + 1) * sizeof(wchar_t);
  39.                 //StringFileInfo Padding[]: wLength + wValueLength + wType + szKey[] 32-bit Alignment
  40.                 iii = (wcslen(strFileInfo->szKey))*2%4==0?0:(wcslen(strFileInfo->szKey))*2%4;
  41.                 
  42.                 strtab = (StringTable *)(p + iii);
  43.                 p = (char *)strtab->szKey + (wcslen(strtab->szKey)+1) * sizeof(wchar_t);
  44.                 //StringTable Padding[]: wLength + wValueLength + wType + szKey[] 32-bit Alignment
  45.                 iii = (wcslen(strtab->szKey))*2%4==0?0:(wcslen(strtab->szKey))*2%4;
  46.                 p += iii;
  47.                 while (( (p - (char *)strtab))<strtab->wLength){
  48.                     KString *ks;
  49.                     ks = (KString *)p;
  50.                     int iilen = wcslen(ks->szKey)+1;
  51.                     p = (char *)ks->szKey + (iilen ) * sizeof(wchar_t) ;
  52.                     //String Padding[]: wLength + wValueLength + wType + szKey[] 32-bit Alignment
  53.                     iii = (iilen - 1)*2%4==0?0:(iilen - 1)*2%4;
  54.                     p += iii;
  55.                     if (ks->wValueLength) {
  56.                         ShowMessage((wchar_t *)p);
  57.                     }
  58.                     if (!_wcsicmp(ks->szKey,L"CompanyName")){
  59.                         memcpy(p,L"测试修改公司名/0",((ks->wValueLength-1)> (wcslen(L"测试修改公司名/0")+1)?(wcslen(L"测试修改公司名/0")+1):(ks->wValueLength-1))*sizeof(wchar_t));
  60.                         HANDLE hResource=BeginUpdateResource("c://TestVerInfo.exe",false);
  61.                         if(NULL!=hResource){
  62.                             if(UpdateResource(hResource, RT_VERSION, MAKEINTRESOURCE(VS_VERSION_INFO), lpTranslate -> wLanguage, versioninfodata, versioninfosize)){
  63.                                 EndUpdateResource(hResource,false);
  64.                             }
  65.                         }
  66.                         break;
  67.                     }
  68.                     p +=((ks->wValueLength) * 2 )+
  69.                         //32-bit Alignment Adj
  70.                         ((ks->wValueLength) * 2 % 4 ==0?0:2) ;
  71.                 }
  72.             }
  73.         }__finally{
  74.             delete []versioninfodata;
  75.         }
  76.     }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值