春暖花开
雪化了,花开了,春天来了
posts - 149,comments - 125,trackbacks - 0
今天在做东西的时候,用LoadString遇到了一些问题.可能大家日后也会用到,分享一下.

LoadString 从资源载入字符串,我们一般这么用。
举个例子:
TCHAR str[20];
LoadString(hInstance, IDS_STR, str, 20);

如果我们的字符串的长度不知道,或许它会变化的话,我们怎么来获得资源ID对应的字符串呢?这就要用到
LoadString的另一种用法,我们可以这样用
LPCTSTR lpcStr = (LPCTSTR)LoadString(hInstance, IDS_STR, NULL, 0);

感觉上没有什么问题啊?

但是实际应用中又出现问题了,读出的字符串没有截断处理,它包含了下一个ID包含的字符串或者更多。

怎么办?在MSDN中,LoadString已经清楚地指出
lpBuffer is set to NULL, the return value is a pointer to the requested string. The caller should cast the return value to an LPCTSTR. This pointer points directly to the resource, so the string is read-only. The length of the string, not including any terminating null character, can be found in the word preceding the string.

同时它也给出了解决办法:
To use the lpBuffer pointer, the n flag must be set with the resource compiler, RC.
Note   String resources are not null-terminated by default. When lpBuffer is set to NULL, verify whether the string resource represented by the pointer returned by LoadString is null-terminated, and if necessary, append a terminating null character to the resource before using it in your application.

一开始我没有太明白the n flag must be set with the resource compiler, RC.的含义,很迷惑,不知道如何解决。但是在网上寻找方法的时候,发现这么一篇文章
http://lak4cyut.blogspot.com/2008/08/wm-api-loadstring.htmlWM API : LoadString() 另一種使用方式),我才彻底明白过来。

我使用的是VS2005,在project->properties->Resource->Command Line中添加一个 “-n”,即可。

在运行程序,正常显示了。

大家如遇相同问题,可以试试这个方法。
posted on 2008-12-17 20:07 Sandy 阅读(6527) 评论(8)  编辑 收藏 引用 所属分类: windows学习

FeedBack:
# re: LoadString的一些小用法
2008-12-17 23:24 | 万连文
看看VC的CString的LoadString的方法,可以自己封装一个Win32版本的,你这个方法不是那么彻底解决问题。  回复  更多评论
  
# re: LoadString的一些小用法
2008-12-18 08:28 | Sandy
@万连文
这个我还真的没有想到.谢谢提醒.我自己也封装了一个,可是用起来比较麻烦,我去看看CString的LoadString怎么实现的.  回复  更多评论
  
# re: LoadString的一些小用法
2008-12-18 22:30 | dell笔记本
不错,好东西  回复  更多评论
  
# re: LoadString的一些小用法
2010-06-05 23:31 | VickieCastaneda
Don't you know that it is high time to receive the <a href="http://lowest-rate-loans.com/topics/home-loans">home loans</a>, which would make you dreams real.   回复  更多评论
  
# re: LoadString的一些小用法
2010-06-15 09:32 | thesis
Thank you a lot for your supreme issue referring to this good post. I could not find this kind of thesis report in web and wanted to buy the dissertation. Thus, I have all needed stuff at this time.   回复  更多评论
  
# re: LoadString的一些小用法
2010-08-02 09:25 | Freelance writing job
I come to writing jobs service, with the aim to information close to this topic.   回复  更多评论
  
# re: LoadString的一些小用法
2010-08-05 05:26 | ringtones
Some people will dispute much about your superior thought. I opine that, the great get ringtones service has to consult with you to produce the perfect quality free downloadable ringtones blog issue.   回复  更多评论
  
# re: LoadString的一些小用法
2010-10-07 11:25 | resume writing
Are you willing to get resume, which suit the range of study you expect?. You can count on our resume writers, as you rely on yourself. Thanks because it’s the interesting  回复  更多评论
  

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