春暖花开
雪化了,花开了,春天来了
posts - 149,comments - 125,trackbacks - 0

今天在运用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 阅读(4839) 评论(0)  编辑 收藏 引用 所属分类: Windows Mobile

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理