请高人指教如何用代码修改EXE或者DLL文件的版本号,谢谢!加急!

无聊客 2005-11-01 03:10:31
因为环境不是PC,用一些普通的工具或者VS修改以后,DLL都跑不起来了,所以我想自己解析,直接读到版本号再改掉就得了,但是对格式不是很了解,有哪位大虾比较了解的请帮个忙提示一下,给个sample或者helloworld什么的,不甚感激!
...全文
874 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
an_bachelor 2005-11-01
  • 打赏
  • 举报
回复
以下的例子是更新對話框資源的,但是更新版本信息同樣可以,因爲版本信息也是一種資源。也許用資源訪問函數的思路更好一些。

Using Resources

--------------------------------------------------------------------------------

This section contains code snippets for the following tasks:

Updating Resources
Creating a Resource List
Updating Resources
The following example copies a dialog box resource from one executable file, Hand.exe, to another, Foot.exe, by following these steps:

Use the LoadLibrary function to load the executable file Hand.exe.
Use the FindResource and LoadResource functions to locate and load the dialog box resource.
Use the LockResource function to retrieve a pointer to the dialog box resource data.
Use the BeginUpdateResource function to open an update handle to Foot.exe.
Use the UpdateResource function to copy the dialog box resource from Hand.exe to Foot.exe.
Use the EndUpdateResource function to complete the update.
The following code implements these steps.

Security Alert Using LoadLibrary incorrectly can compromise the security of your application by loading the wrong DLL. Refer to the LoadLibrary documentation for information on how to correctly load DLLs with different versions of Microsoft Windows.
Hide Example

HRSRC hResLoad; // handle to loaded resource
HANDLE hExe; // handle to existing .EXE file
HRSRC hRes; // handle/ptr. to res. info. in hExe
HANDLE hUpdateRes; // update resource handle
char *lpResLock; // pointer to resource data
BOOL result;
// Load the .EXE file that contains the dialog box you want to copy.
hExe = LoadLibrary("hand.exe");
if (hExe == NULL)
{
ErrorHandler("Could not load exe.");
}

// Locate the dialog box resource in the .EXE file.
hRes = FindResource(hExe, "AboutBox", RT_DIALOG);
if (hRes == NULL)
{
ErrorHandler("Could not locate dialog box.");
}

// Load the dialog box into global memory.
hResLoad = LoadResource(hExe, hRes);
if (hResLoad == NULL)
{
ErrorHandler("Could not load dialog box.");
}

// Lock the dialog box into global memory.
lpResLock = LockResource(hResLoad);
if (lpResLock == NULL)
{
ErrorHandler("Could not lock dialog box.");
}

// Open the file to which you want to add the dialog box resource.
hUpdateRes = BeginUpdateResource("foot.exe", FALSE);
if (hUpdateRes == NULL)
{
ErrorHandler("Could not open file for writing.");
}

// Add the dialog box resource to the update list.
result = UpdateResource(hUpdateRes, // update resource handle
RT_DIALOG, // change dialog box resource
"AboutBox", // dialog box name
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), // neutral language
lpResLock, // ptr to resource info
SizeofResource(hExe, hRes)); // size of resource info.
if (result == FALSE)
{
ErrorHandler("Could not add resource.");
}

// Write changes to FOOT.EXE and then close it.
if (!EndUpdateResource(hUpdateRes, FALSE))
{
ErrorHandler("Could not write changes to file.");
}

// Clean up.
if (!FreeLibrary(hExe))
{
ErrorHandler("Could not free executable.");
}
无聊客 2005-11-01
  • 打赏
  • 举报
回复
用工具我估计是不行了,大部分工具实现方法我想应该大同小异,我试了好几个(VS2005也试了),还是不行
无聊客 2005-11-01
  • 打赏
  • 举报
回复
DLL是CE的,但是需要在PC上改它,所以我觉的首先不能用windows API,因为从目前看来CE上的结构不一样,用WINDOWS API不行,估计要要知道整个文件的结构,定位到版本信息的位置,然后改掉版本号。回家再看看。。。。。。
老夏Max 2005-11-01
  • 打赏
  • 举报
回复
后面几个API好像是不支持WinCE的,你可以在模拟器中编译/运行一下看看.EVC中如果不支持应该会有提示的.
老夏Max 2005-11-01
  • 打赏
  • 举报
回复
在WinCE上可能需要主意Unicode的处理,获得的那几个API都是支持WinCE的.
yayaniuniu502 2005-11-01
  • 打赏
  • 举报
回复
试一试Hex Workshop和EmEditor吧
老夏Max 2005-11-01
  • 打赏
  • 举报
回复
WinCE上的东东我不是很清楚,理论上CE的文件可能和PC上的文件结构有点不同,你可以看看MSDN对这几个API的说明.
老夏Max 2005-11-01
  • 打赏
  • 举报
回复
修改的例子当中只是简单的修改了版本号,并没有修改"产品版本",所以需要参考读取的例子修改相关的产品版本信息!
无聊客 2005-11-01
  • 打赏
  • 举报
回复
to vcleaner(我没当大哥很久了.......) :
你的代码在windows xp上没有问题,但是我的DLL是CE的,经过以上代码处理之后,DLL就无效了。是不是在CE上结构有一些不同?望指教,谢谢!
无聊客 2005-11-01
  • 打赏
  • 举报
回复
十万分感谢楼上的几位兄弟,我先试试去。
老夏Max 2005-11-01
  • 打赏
  • 举报
回复
修改的例子中的两个字节对齐的宏特别重要,所以贴上所有其代码:
#include "stdafx.h"

struct VS_VERSIONINFO
{
WORD wLength;
WORD wValueLength;
WORD wType;
WCHAR szKey[1];
WORD wPadding1[1];
VS_FIXEDFILEINFO Value;
WORD wPadding2[1];
WORD wChildren[1];
};

struct
{
WORD wLanguage;
WORD wCodePage;
} *lpTranslate;

void main( void )
{
VS_VERSIONINFO *pVerInfo;
LPBYTE pOffsetBytes;
VS_FIXEDFILEINFO *pFixedInfo;
LPCTSTR lpszFile = _T("c:\\winnt\\system32\\atl.dll");
DWORD dwHandle,
dwSize,
dwResult = 0;


// determine the size of the resource information
dwSize = GetFileVersionInfoSize(lpszFile, &dwHandle);
if (0 < dwSize)
{
LPBYTE lpBuffer = new BYTE[dwSize];

if (GetFileVersionInfo(lpszFile, 0, dwSize, lpBuffer) != FALSE)
{
// these macros help to align on r-byte boundaries (thanks Ted Peck)
#define roundoffs(a,b,r) (((BYTE *) (b) - (BYTE *) (a) + ((r) - 1)) & ~((r) - 1))
#define roundpos(a,b,r) (((BYTE *) (a)) + roundoffs(a,b,r))

// 'point to' the start of the version information block
pVerInfo = (VS_VERSIONINFO *) lpBuffer;

// the fixed section starts right after the 'VS_VERSION_INFO' string
pOffsetBytes = (BYTE *) &pVerInfo->szKey[_tcslen(pVerInfo->szKey) + 1];

pFixedInfo = (VS_FIXEDFILEINFO *) roundpos(pVerInfo, pOffsetBytes, 4);

// increment the numbers!
pFixedInfo->dwFileVersionMS = pFixedInfo->dwFileVersionMS + 0x00010001;
pFixedInfo->dwFileVersionLS = pFixedInfo->dwFileVersionLS + 0x00010001;
pFixedInfo->dwProductVersionMS = pFixedInfo->dwProductVersionMS + 0x00010001;
pFixedInfo->dwProductVersionLS = pFixedInfo->dwProductVersionLS + 0x00010001;

HANDLE hResource = BeginUpdateResource(lpszFile, FALSE);
if (NULL != hResource)
{
UINT uTemp;

// get the language information
if (VerQueryValue(lpBuffer, _T("\\VarFileInfo\\Translation"), (LPVOID *) &lpTranslate, &uTemp) != FALSE)
{
// could probably just use LANG_NEUTRAL/SUBLANG_NEUTRAL
if (UpdateResource(hResource, RT_VERSION, MAKEINTRESOURCE(VS_VERSION_INFO), lpTranslate->wLanguage, lpBuffer, dwSize) != FALSE)
{
if (EndUpdateResource(hResource, FALSE) == FALSE)
dwResult = GetLastError();
}
else
dwResult = GetLastError();
}
}
else
dwResult = GetLastError();
}
else
dwResult = GetLastError();

delete [] lpBuffer;
}
else
dwResult = GetLastError();

if (0 != dwResult)
wprintf(_T("Operation was not successful. Result = %lu\n"), dwResult);
}

老夏Max 2005-11-01
  • 打赏
  • 举报
回复
或者文件的版本号使用下面的API:
GetFileVersionInfoSize
GetFileVersionInfo
VerQueryValue
更新版本号使用下面的API:
BeginUpdateResource
UpdateResource
EndUpdateResource

获得的例子参考:
http://www.codeproject.com/file/VersionInfo.asp
http://www.codeproject.com/file/xfileproperties.asp
修改的例子参考:
http://www.codeproject.com/samples/UpdateVersion.asp
teli_eurydice 2005-11-01
  • 打赏
  • 举报
回复
up
无聊客 2005-11-01
  • 打赏
  • 举报
回复
......
无聊客 2005-11-01
  • 打赏
  • 举报
回复
路过的朋友帮顶一下啊,谢谢先!

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧