最近尝试用一些WINDOWS API来修改和获取资源,根据MSDN,这些API主要有:
-
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.
我手动的插入了一个二进制资源(RT_RCDATA),不过发现FindResource这里总是返回NULL,
最后问题终于解决了,要注意的地方主要有2点:
1,可能是VC的BUG,加入资源后需要手动修改 .rc文件,加入
/////////////////////////////////////////////////////////////////////////////
//
// RCDATA
//
IDR_RCDATA2 RCDATA "res\\rcdata1.bin"
否则.rc文件里只有这句:
/////////////////////////////////////////////////////////////////////////////
//
// RCDATA
//
2,这里指定了RCDATA 放在 res\\rcdata1.bin里,如果你的rcdata1.bin的内容是空的,也会FindResource失败。
还有,就是如果你想修改正在执行的程序的资源,恐怕用UpdateResource是会失败的,即使调用EndUpdateResource也不行!
只能修改未执行的exe,如果有朋友知道什么解决办法可以修改正在执行中程序的资源,欢迎告知!
下面是MSDN上关于如何修改exe资源的例子: 从hand.exe 中读取资源并复制到 foot.exe。
1
HRSRC hResLoad;
//
handle to loaded resource
2
HANDLE hExe;
//
handle to existing .EXE file
3
HRSRC hRes;
//
handle/ptr. to res. info. in hExe
4
HANDLE hUpdateRes;
//
update resource handle
5
char
*
lpResLock;
//
pointer to resource data
6
BOOL result;
7
//
Load the .EXE file that contains the dialog box you want to copy.
8
hExe
=
LoadLibrary(
"
hand.exe
"
);
9
if
(hExe
==
NULL)
10
{
11
ErrorHandler(
"
Could not load exe.
"
);
12
}
13
14
//
Locate the dialog box resource in the .EXE file.
15
hRes
=
FindResource(hExe,
"
AboutBox
"
, RT_DIALOG);
16
if
(hRes
==
NULL)
17
{
18
ErrorHandler(
"
Could not locate dialog box.
"
);
19
}
20
21
//
Load the dialog box into global memory.
22
hResLoad
=
LoadResource(hExe, hRes);
23
if
(hResLoad
==
NULL)
24
{
25
ErrorHandler(
"
Could not load dialog box.
"
);
26
}
27
28
//
Lock the dialog box into global memory.
29
lpResLock
=
LockResource(hResLoad);
30
if
(lpResLock
==
NULL)
31
{
32
ErrorHandler(
"
Could not lock dialog box.
"
);
33
}
34
35
//
Open the file to which you want to add the dialog box resource.
36
hUpdateRes
=
BeginUpdateResource(
"
foot.exe
"
, FALSE);
37
if
(hUpdateRes
==
NULL)
38
{
39
ErrorHandler(
"
Could not open file for writing.
"
);
40
}
41
42
//
Add the dialog box resource to the update list.
43
result
=
UpdateResource(hUpdateRes,
//
update resource handle
44
RT_DIALOG,
//
change dialog box resource
45
"
AboutBox
"
,
//
dialog box name
46
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
//
neutral language
47
lpResLock,
//
ptr to resource info
48
SizeofResource(hExe, hRes));
//
size of resource info.
49
if
(result
==
FALSE)
50
{
51
ErrorHandler(
"
Could not add resource.
"
);
52
}
53
54
//
Write changes to FOOT.EXE and then close it.
55
if
(
!
EndUpdateResource(hUpdateRes, FALSE))
56
{
57
ErrorHandler(
"
Could not write changes to file.
"
);
58
}
59
60
//
Clean up.
61
if
(
!
FreeLibrary(hExe))
62
{
63
ErrorHandler(
"
Could not free executable.
"
);
64
}