How to open a resource string in Visual C ++ 2010?

I created a basic stringtable resource in Visual C ++. I am trying to access this resource. However, my program cannot 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 cannot find the resource and always returns null.

I created a simple raster resource, and this new program determines that it is 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"); } } 

This finds the bitmap.

Are stringtable resources handled differently with something?

+4
source share
3 answers

Assuming you don't want to use LoadString (), this should help ...

Rows and string tables do work differently when using FindResource () and FindResourceEx (). From this article in KB :

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

The line with the identifier, nStringID, is in the block with the identifier, nBlockID, given by the following formula:

nBlockID = (nStringID / 16) + 1; // Pay attention to integer division.

The bottom 4 bits of nStringID indicate which record in the block contains the actual string. After you have calculated the identifier of the block that you want to pass to FindResource (), and the index in the block where the row exists, you need to look at the contents to find the row you need.

The following code should get 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++; } } 
+5
source

You can use a LoadString . Here is some text from the MSDN FindResource documentation ...

An application can use FindResource to search for any type of resource, but this function should be used only if the application should access the binary resource data by making subsequent calls to LoadResource and then LockResource.

To use the resource immediately ...

... use LoadString!

+2
source

After 2 days of research, I found this (it works!):

 #include <atlstr.h> ...... ATL::CString str; WORD LangID = MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT); str.LoadString(NULL,IDS_STRING101, LangID); 
0
source

All Articles