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”,即可。

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

大家如遇相同问题,可以试试这个方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值