以下是预定义的资源类型。
| Constant/value | Description |
|---|---|
|
快捷键表。 |
|
动画游标。 |
|
动画图标。 |
|
位图资源。 |
|
依赖于硬件的游标资源。 |
|
对话框。 |
|
允许资源编辑工具将字符串与 .rc 文件相关联。 通常,字符串是提供符号名称的头文件的名称。 资源编译器分析字符串,但否则将忽略该值。 例如: 1 DLGINCLUDE "MyFile.h" |
|
字体资源。 |
|
字体目录资源。 |
|
与硬件无关的游标资源。 |
|
与硬件无关的图标资源。 |
|
HTML 资源。 |
|
依赖于硬件的图标资源。 |
|
并排程序集清单。 |
|
菜单资源。 |
|
消息表条目。 |
|
即插即用资源。 |
|
应用程序定义的资源(原始数据)。 |
|
字符串表条目。 有关详细信息 , 请参阅下面的备注。 |
|
版本资源。 |
|
VXD。 |
注解
字符串表资源
使用 EnumResourceNamesW 等函数枚举字符串表资源(类型RT_STRING),系统不会枚举每个字符串资源 ID;而是枚举资源块。 必须检查这些块,以确定它们实际包含的字符串资源 ID。
字符串资源打包成块,每个块都包含 16 个长度前缀的字符串,这些字符串表示 16 个连续 ID(其中一些 ID 可能未使用;请参阅下文)。 给定字符串资源 ID X后,它将放置在资源块编号 (X \ 16) + 1 中(其中 \ 表示整数除法)。 在该块中,资源可以作为第三个条目找到 N,其中 N = X % 16。
下表显示了一些示例字符串资源 ID 及其所在位置的块号(和偏移量):
| 资源标识符 (ID) | 阻止# | Offset |
|---|---|---|
| 1 |
(1 \ 16) + 1 = 1 |
1 % 16 = 1 |
| 2 |
(2 \ 16) + 1 = 1 |
2 % 16 = 2 |
| 5 |
(5 \ 16) + 1 = 1 |
5 % 16 = 5 |
| 15 |
(15 \ 16) + 1 = 1 |
15 % 16 = 15 |
| 20 |
(20 \ 16) + 1 = 2 |
20 % 16 = 4 |
| 32 |
(32 \ 16) + 1 = 3 |
32 % 16 = 0 |
| 50 |
(50 \ 16) + 1 = 4 |
50 % 16 = 2 |
| 100 |
(100 \ 16) + 1 = 7 |
100 % 16 = 4 |
任何未使用的(缺失)资源 ID 都由零长度字符串表示。 因此,在上面的示例中,block #1 将具有零长度字符串,表示 ID 0、3、4 和 6 - 14。 块中的任何字符串都没有终止 NULL 字符,因为允许字符串资源包含嵌入 的 NUL。 因此,块 #1 的内存布局如下所示,假设每个字符串的值为“Hello world”(长 11 个字符),尖括号中的数字表示整数(而不是文本字符):
<0><11>Hello world<11>Hello world<0><0>
<11>Hello world<0><0><0><0><0><0><0><0>
<0><11>Hello world
以下代码片段显示了一个枚举回调函数,它将像其他类型的(如RT_ICON)一样枚举单个RT_STRING资源,而不是块:
// Number of entries in the string table.
constexpr UINT STRING_TABLE_SIZE{ 16 };
// Returns the original resource ID from a given block / offset.
inline UINT GetStringResourceIdFromStringTable(LPCWSTR lpName, const unsigned int index)
{
_ASSERT(index < STRING_TABLE_SIZE);
return ((reinterpret_cast<UINT>(lpName) - 1) * STRING_TABLE_SIZE) + index;
}
// Helper function that will enumerate string table blocks, looking for resources.
BOOL EnumerateResourceNamesWrapperForStrings(HMODULE hModule, LPWSTR lpName, LONG_PTR lParam, ENUMRESNAMEPROCW lpEnumFunc)
{
// No need to free or unlock resources in Win32, so OK to throw away intermediates
auto ptr = (wchar_t*)LockResource(LoadResource(hModule, FindResource(hModule, lpName, RT_STRING)));
if (ptr)
{
for (unsigned int i = 0; i < STRING_TABLE_SIZE; ++i)
{
wchar_t size = *ptr;
if (size > 0)
{
auto id = GetStringResourceIdFromStringTable(lpName, i);
// Invoke the callback for this string resource ID.
auto callbackResult = lpEnumFunc(hModule, RT_STRING, MAKEINTRESOURCE(id), lParam);
if (!callbackResult)
{
return callbackResult;
}
}
// Skip to next potential string in the block
ptr += size + 1;
}
return TRUE;
}
// Couldn't load the string table entry; something is wrong.
return FALSE;
}
// Wrapper function for EnumResourceNamesW that will enumerate individual string resources.
BOOL EnumResourceNamesIncludingStringsW(HMODULE hModule, LPCWSTR lpType, ENUMRESNAMEPROCW lpEnumFunc, LONG_PTR lParam)
{
struct param_wrapper { ENUMRESNAMEPROCW lpEnumFunc; LONG_PTR lParam; } params{ lpEnumFunc, lParam };
// Use a simple lambda to either call our String helper, or directly call the user's callback
return EnumResourceNamesW(hModule, lpType, [](auto hModule, auto lpType, auto lpName, auto lParam)
{
auto params = reinterpret_cast<param_wrapper*>(lParam);
if (lpType == RT_STRING)
{
return EnumerateResourceNamesWrapperForStrings(hModule, lpName, params->lParam, params->lpEnumFunc);
}
return params->lpEnumFunc(hModule, lpType, lpName, params->lParam);
}, reinterpret_cast<LONG_PTR>(¶ms));
};
//////////
// Sample callback that just increments a counter.
BOOL CountResources(HMODULE, LPCWSTR, LPWSTR, LONG_PTR lParam)
{
// Add one to the count...
(*(reinterpret_cast<UINT*>(lParam)))++;
return TRUE;
}
// Sample usage:
void CountStringsInExplorer()
{
auto lib = LoadLibraryExW(LR"(c:\windows\explorer.exe)", nullptr, LOAD_LIBRARY_AS_DATAFILE);
if (!lib)
{
return;
}
UINT nStringBlocks{ 0 };
UINT nStrings{ 0 };
// Count the number of string blocks using raw Win32 API.
EnumResourceNamesW(lib, RT_STRING, CountResources, (LONG_PTR)&nStringBlocks);
// Count the number of actual strings, using the wrapper.
EnumResourceNamesIncludingStringsW(lib, RT_STRING, CountResources, (LONG_PTR)&nStrings);
// Outputs something like:
//
// Explorer.exe contains 44 strings (in 17 blocks).
//
std::wcout << L"Explorer.exe contains " << nStrings << L" strings (in " << nStringBlocks
<< L" blocks)." << std::endl;
}
Requirements
| 要求 | 价值 |
|---|---|
| 标题 |
|