今天在運用VerQueryValue的時候發現了一個很詭異的問題,出錯了。不是這個函數出錯了,而是后面對其中的值的運用出錯了。
SDK中的代碼是這樣的
// Structure used to store enumerated languages and code pages.


struct LANGANDCODEPAGE
{
WORD wLanguage;
WORD wCodePage;
} *lpTranslate;

// Read the list of languages and code pages.
DWORD cbTranslate = 0;

VerQueryValue(pBlock,
TEXT("\\VarFileInfo\\Translation"),
(LPVOID*)&lpTranslate,
&cbTranslate);

// Read the file description for each language and code page.

for( i=0; i < (cbTranslate/sizeof(struct LANGANDCODEPAGE)); i++ )


{
wsprintf( SubBlock,
TEXT("\\StringFileInfo\\%04x%04x\\FileDescription"),
lpTranslate[i].wLanguage,
lpTranslate[i].wCodePage);

// Retrieve file description for language and code page "i".
VerQueryValue(pBlock,
SubBlock,
&lpBuffer,
&dwBytes);
}

我在使用的時候,直接調用的是lpTranslate[0].wLanguage,然而此時調用出錯了! 為何?可能是這個值不存在。
簡單的判斷 if (lpTranslate != NULL) 是不妥的。
我查看了一下SDK文檔
Return Values
If the specified version-information structure exists, and version information is available, the return value is nonzero. If the address of the length buffer is zero, no value is available for the specified version-information name.
If the specified name does not exist or the specified resource is not valid, the return value is zero.
如果cbTranslate為零的話,則沒有任何版本信息獲得,所以我的判斷if (lpTranslate != NULL)是不充分的。
所以需要改為if (lpTranslate != NULL && cbTranslate != 0), 這樣就OK了。
總結: 要認真看文檔, 有些條件要注意到!
posted on 2009-06-17 18:55
Sandy 閱讀(4977)
評論(0) 編輯 收藏 引用 所屬分類:
Windows Mobile