dumpbin/headers
.rsrc name
40000040 flags
Initialized Data
Read Only
windbg !address
Type 01000000 MEM_IMAGE
Protect 00000002 PAGE_READONLY
State 00001000 MEM_COMMIT
ǫ��������, ���ǫ��ͱ����α�� :-)
�ϴ��dz�ʵ, ����ϴ�ͻ����Ƿ��� :-D
void __stdcall Test() {
HRSRC hrsrc = FindResource(0, MAKEINTRESOURCE(IDR_RCDATA1), RT_RCDATA);
if (!hrsrc)
except();
HGLOBAL hglob = LoadResource(0, hrsrc);
if (!hglob)
except();
LPVOID pvoid = LockResource(hglob);
if (!pvoid)
except();
if (0 != UnlockResource(hglob))
except();
if (0 != FreeResource(hglob))
except();
Beep(800, 100);
}
������δ����ǰ�16-bit��win3.1SDKд��, ÿ����������, ��Ϊ16-bit��NE(.dll/.exe)��Resource�ǿ��Բ��ʹ������һ����ص�, ����֮��Ҳ����ж�ص�
DW Flag word.
0010h = MOVEABLE Resource is not fixed.
0020h = PURE Resource can be shared.
0040h = PRELOAD Resource is preloaded.
"If the resource identified by the hglb parameter has been discarded"
"The resource is not loaded until the LockResource function is called"
1)
HRSRC FindResource(hinst, lpszName, lpszType)
HINSTANCE hinst; /* handle of module containing resource */
LPCSTR lpszName; /* address of resource name */
LPCSTR lpszType; /* address of resource type */
Returns
The return value is the handle of the named resource if the function is successful. Otherwise, it is NULL.
2)
HGLOBAL LoadResource(hinst, hrsrc)
HINSTANCE hinst; /* handle of file containing resource */
HRSRC hrsrc; /* handle of resource */
Returns
The return value is the instance handle of the global memory object containing the data associated with the resource. It is NULL if
no such resource exists.
Comments
When finished with a resource, an application should free the global memory associated with it by using the FreeResource function.
If the specified resource has been loaded, this function simply increments the reference count for the resource.
The resource is not loaded until the LockResource function is called to translate the handle returned by LoadResource into a far
pointer to the resource data.
3)
void FAR* LockResource(hglb)
HGLOBAL hglb; /* handle of resource */
The LockResource function locks the given resource. The resource is locked in memory and its reference count is incremented
(increased by one). The locked resource
is not subject to discarding.
Returns
The return value points to the first byte of the loaded resource if the function is successful. Otherwise, it is NULL.
Comments
The resource remains locked in memory until its reference count is decreased to zero by calls to the FreeResource function.
If the resource identified by the hglb parameter has been discarded, the resource-handler function (if any) associated with the
resource is called before the LockResource function returns. The resource-handler function can recalculate and reload the resource
if necessary. After the resource-handler function returns, LockResource makes another attempt to lock the resource and returns with
the result.
Use the UnlockResource macro to unlock a resource that was locked by LockResource.
4)
BOOL UnlockResource(hglblResData)
HGLOBAL hglblResData; /* handle of memory object to unlock */
The UnlockResource macro unlocks the resource specified by the hglblResData parameter and decreases the reference count of the
resource by one.
Returns
The return value is zero if the object's reference count is decreased to zero. Otherwise, it is nonzero.
/*****************************************************************************\
* windows.h - Windows functions, types, and definitions *
* Version 3.10 *
* Copyright (c) 1985-1992, Microsoft Corp. All rights reserved. *
*******************************************************************************
#define UnlockResource(h) GlobalUnlock(h)
5)
BOOL FreeResource(hglbResource)
HGLOBAL hglbResource; /* handle of loaded resource */
The FreeResource function decrements (decreases by one) the reference count of a loaded resource. When the reference count reaches
zero, the memory occupied by
the resource is freed.
Returns
The return value is zero if the function is successful. Otherwise, it is nonzero, indicating that the function has failed and the
resource has not been freed.
��δ�����SDK6.1����, ��ȫ����.
�´���Ͳ�����ôд, ֻҪFindResource, LoadResource + (����cast)�Ϳ�����,
1) FindResource���ص��Ǹ���Դ��(.rsrcӳ��)�ڴ��Ŀ¼���ַ.
2) LoadResource���ص��Ǹ���Դ��(.rsrcӳ��)�ڴ�ı����ַ, ���ַ���ڴ���ֻ����, ��������һ�� *(int*)hglob = 1; ����Access violation.
3) LockResourceֻ����ʽ�ϱ��ּ���, ûʵ������(��������cast����), Windows XP���൱��LockResource(hglb) { return (void*) hglb; }
kernel32!LockResource:
7c80cd27 8bff mov edi,edi
7c80cd29 55 push ebp
7c80cd2a 8bec mov ebp,esp
7c80cd2c 8b4508 mov eax,dword ptr [ebp+8] {hglb}
7c80cd2f 5d pop ebp
7c80cd30 c20400 ret 4
4) UnlockResourceҲ����ʽ�ϱ��ּ���, ûʵ������, �������0, �����hResData�DZ��ʽ, ��ִ�б��ʽ(side effects), Ȼ����0ΪUnlockResource��ֵ, �൱��"reference count is decreased to zero"
/************************************************************************
* winbase.h -- This module defines the 32-Bit Windows Base APIs *
* Copyright (c) Microsoft Corp. All rights reserved. *
************************************************************************/
#define UnlockResource(hResData) ((hResData), 0)
5) FindResourceҲ����ʽ�ϱ��ּ���, ûʵ������, Windows XP���൱��FreeResource(hglb) { return 0; } "successful"
0040106f ff1500204000 call dword ptr [rc!_imp__FreeResource (00402000)] ds:0023:00402000={kernel32!FreeResource (7c8260aa)}
kernel32!FreeResource:
7c8260aa 33c0 xor eax,eax
7c8260ac c20400 ret 4
"acc" <jo...@sina.com> wrote:
> �ô�һ�û�����ϣ����������ϴ?
>
> ��Դ����ֻ���ġ�������ʱ�����š�ֻ����Դ�������Ծͷ���ȥ�ˡ�LoadResource
> �����ء�����Դ�������ġ�
>
> "acc" <jo...@sina.com> wrote:
>> ���⣬ͨ�� LoadResource �����ء��Ķ���ֻ����Դ��ֻ�����ڴ棬����ΪʲôҪ������
>>
>> "acc" <jo...@sina.com> wrote:
>>> msdn ˵��
>>>
>>> LoadResource �ķ���ֵ����� HGLOBAL ��Ϊ�������ݡ�
>>> ���� LoadResource Function
>>>
>>> .dll/.exe �е���Դӳ���ڽ�̿ռ䣬LoadResource ֻ�Ǹ�������ַ��ģ����ֹ����Դռ�ݵĿռ��ͷš����� FreeLibrary �ͷ� .dll �е���Դ��
>>>
>>>
>>>
>>>
>>> ���� INFO: FreeResource Not Necessary to Free Resources in Win32
>>>
>>> ��ô LockResource �Ǹ�ɶ�ģ���ķ���ֵ�� LoadResource ����ֵ��һ��ֻ�����Ͳ�ͬ��UnlockResource �����ѱ�����LockResource �Ȳ���Ҫ�������Ѿ��ת����ָ�룬Ҳ
>>> ����Ҫ������Ҫ������ζ�Ų���Ҫ��������������
>>>
>>> ���ԣ�����Ҫ���� LockResource �������
>>>
>>> ��INFO: FreeResource Not Necessary to Free Resources in Win32 ���ű������������д����� DeleteCursor ����