2011-07-16 49 views
4

我在Visual C++中創建了一個基本的stringtable資源。我正在嘗試訪問該資源。但是,我的程序似乎無法找到資源。這裏:如何在Visual C++ 2010中打開資源字符串?

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"); 
    } 
} 

這個程序找不到資源,總是返回null。

我創建了一個簡單的位圖資源,這個新程序確定了這一點。這裏:

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資源得到不同處理方式不同嗎?

回答

2

您可以直接使用LoadString代替。下面是從MSDN文檔FindResource一些文字...

應用程序可以使用FindResource找到任何類型的資源,而是應該使用這個功能只有在應用程序必須通過向後續調用訪問二進制資源數據LoadResource,然後到LockResource。

要立即使用的資源...

...使用加載鏈!

+2

如果必須使用'FindResource'字符串,那麼你需要在ID通過'MAKEINTRESOURCE((IDS_STRING102> > 4)+1)'。更容易使用LoadString,然後...FindResource並不是用於直觀的字符串加載。 :( – Ben

5

假設你不想使用加載鏈()這應該幫助...

字符串和字符串表使用FindResource()和FindResourceEx時確實區別對待()。從this KB文章:

字符串資源存儲爲字符串塊。每個塊可以有多達16個字符串的 ,並且代表可以加載/更新的最小粒度的 字符串資源。每個塊由標識符(ID)標識爲 ,從一個(1)開始。當調用FindResource,LoadResource和UpdateResource函數時,我們使用這個ID 。

與ID,nStringID的字符串,位於由下面的公式給出與ID塊, nBlockID,:

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

nStringID的低4位指示塊中的哪個條目包含實際字符串。一旦計算了要傳遞給FindResource()的塊ID以及存在該字符串的塊中的索引,就必須掃描它的內容以找到要查找的字符串。

下面的代碼應該讓你開始。

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++; 
    } 
} 
0

的研究2天,我發現在這之後(它的工作原理!):

#include <atlstr.h> 

...... 

ATL::CString str; 
WORD LangID = MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT); 
str.LoadString(NULL,IDS_STRING101, LangID); 
相關問題