如何在Visual C ++ 2010中打开资源字符串? [英] How do you open a resource string in Visual C++ 2010?

查看:158
本文介绍了如何在Visual C ++ 2010中打开资源字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual C ++中创建了一个基本的字符串资源。我试图访问该资源。但是,我的程序似乎找不到资源。在这里:

I created a basic stringtable resource in Visual C++. I am trying to access that resource. However, my program can't seem to find the resource. Here:

int main(int argc, char* argv[])
{
    HRSRC hRsrc;
    hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDS_STRING102), RT_STRING);
    if (hRsrc == NULL) {
        printf("Not found\n");
    } else {
        printf("Found\n");
    }
}

此程式找不到资源,空值。

This program can't find the resource and always returns null.

我创建了一个简单的位图资源,这个新程序识别出只是很好。在这里:

I created a simple bitmap resource and this new program identifies that just fine. Here:

int main(int argc, char* argv[])
{
    HRSRC hRsrc;
    hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDB_BITMAP1), RT_BITMAP);
    if (hRsrc == NULL) {
        printf("Not found\n");
    } else {
        printf("Found\n");
    }
}

这会找到位图。

是否可以不同地处理stringtable资源?

Do stringtable resources get handled somehow differently?

推荐答案

不要使用LoadString()这将有助于...

Assuming you do not want to use LoadString() this should help...

使用FindResource()和FindResourceEx()时,字符串和字符串表的处理方式不同。从此KB 文章:

Strings and string tables are indeed treated differently when using FindResource() and FindResourceEx(). From this KB article:


字符串资源存储为字符串块。每个块可以有
最多16个字符串,表示可以加载/更新的
字符串资源的最小粒度。每个块由标识符(ID)标识
,从一(1)开始。当
调用FindResource,LoadResource和UpdateResource函数时,我们使用这个ID。

String resources are stored as blocks of strings. Each block can have up to sixteen strings and represents the smallest granularity of string resource that can be loaded/updated. Each block is identified by an identifier (ID), starting with one (1). We use this ID when calling the FindResource, LoadResource and UpdateResource functions.

ID为nStringID的字符串位于ID为
nBlockID,由以下公式给出:

A string with ID, nStringID, is located in the block with ID, nBlockID, given by the following formula:

nBlockID =(nStringID / 16)+ 1; //注意整数除法。

nBlockID = (nStringID / 16) + 1; // Note integer division.

nStringID的低4位表示块中的哪个条目包含实际字符串。一旦计算出要传递给FindResource()的块ID和存在字符串的块中的索引,您必须扫描其内容以找到您要查找的字符串。

The lower 4 bits of nStringID indicates which entry in the block contains the actual string. Once you have calculated the block ID to pass to FindResource() and the index in the block where the string exists you have to scan through it's contents to find the string you are looking for.

下面的代码可以让你开始。

The following code should get you started.

const WCHAR *stringPtr;
WCHAR stringLen;

//  Get the id of the string table block containing the target string
const DWORD blockID = (nID >> 4) + 1;

//  Get the offset of teh target string in the block
const DWORD itemID = nID % 0x10;

//  Find the resource
HRSRC hRes = FindResourceEx(
    hInst,
    RT_STRING,
    MAKEINTRESOURCE(blockID),
    wLanguage);
if (hRes)
{
    HGLOBAL hBlock = LoadResource(hInst, hRes);
    const WCHAR *tableDataBlock = reinterpret_cast<LPCWSTR>(LockResource(hBlock));
    const DWORD tableBlockSize = SizeofResource(hInst, hRes);
    DWORD searchOffset = 0;
    DWORD stringIndex = 0;

    //  Search through the section for the appropriate entry.
    //  The first two bytes of each entry is the length of the string
    //  followed by the Unicode string itself. All strings entries 
    //  are stored one after another with no padding.
    while(searchOffset < tableBlockSize)
    {
        if (stringIndex == itemID)
        {
            //  If the string has size. use it!
            if (tableDataBlock[searchOffset] != 0x0000)
            {
                stringPtr = &tableDataBlock[searchOffset + 1];
                stringLen = tableDataBlock[searchOffset];
            }
            //  Nothing there -
            else
            {
                stringPtr = NULL;
                stringLen = 0;
            }

            //  Done
            break;
        }

        //  Go to the next string in the table
        searchOffset += tableDataBlock[searchOffset] + 1;

        //  Bump the index
        stringIndex++;
    }
}

这篇关于如何在Visual C ++ 2010中打开资源字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆