Windows API 和字符串连接 [英] Windows API and String concatenation

查看:47
本文介绍了Windows API 和字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 C++ 和 Windows API(来自 Java 背景)比较陌生,我只是在玩 Windows API 调用 MessageBox 和其他简单的函数,直到我尝试传递一个从自定义函数到 MessageBox 的连接字符串,我在生成的窗口中注意到一个奇怪的输出.

I'm relatively new to C++ and the Windows API (coming from a Java background) and I was just playing around with the Windows API calling MessageBox and other simple functions until I tried to pass a concatenated string from a custom function to MessageBox where I noticed a weird output in the generated window.

这是可疑的函数:

const char* addFoo(const char* strInput)
{
    return ("foo-" + std::string(strInput)).c_str();
}

它只是返回在前面添加了 foo- 的原始输入.(我希望我没有在那里做任何令人难以置信的错误)

It just returns the original input with a foo- added in front. (I hope I'm not doing anything incredibly wrong there)

在 main 中,我对 MessageBox 进行了两次调用,首先没有调用函数,而是动态地进行所有计算,然后调用函数:

In main I then do two calls to MessageBox first without calling the function but instead doing all the calculation on the fly, and afterwards calling the function:

const char* a = "bar";
MessageBox(NULL, ("foo-" + std::string(a)).c_str(), "The Foobar Experiment", MB_OK);
MessageBox(NULL, addFoo(a), "The Foobar Experiment", MB_OK);

这是我通过动态进行字符串连接得到的结果(案例 1):

This is the result I get by doing the string concatenation on the fly (case 1):

调用函数addFoo得到的结果(案例2):

The result I get by calling the function addFoo (case 2):

有谁知道为什么我使用我的 addFoo 函数在生成的窗口上得到这些不可读的字符?提前致谢,抱歉发了这么长的帖子.

Does anyone have any idea why I'm getting these unreadable characters on the generated window by using my addFoo function? Thanks in advance and sorry for the long post.

推荐答案

您的代码中有两个根本错误的地方,一个与 C++ 相关,另一个与 Windows 相关.

There are two fundamentally wrong things in your code, one being C++ related, the other being Windows related.

首先,您要返回一个指向本地实体的指针,即 c_str() 的返回值,它是一个指针.返回指向局部变量的指针是未定义的行为.你想要做的是返回一个 string,而不是一个指针.在 C++ 中,有诸如 std::stringstd::wstring 之类的字符串类型,它们实现了正确的复制语义,这些语义是安全无错误返回对象所必需的.

First, you are returning a pointer to a local entity, namely the return value of c_str() which is a pointer. Returning pointers to local variables is undefined behavior. What you want to do is return a string, not a pointer. In C++, there are string types such as std::string and std::wstring that implement the correct copy semantics that are required to have objects returned safely without error.

#include <string>
std::string addFoo(const char* strInput)
{
    return "foo-" + std::string(strInput);
}

<小时>

您的代码的第二个错误是,在 Windows 世界中,关于字符类型,您基本上有两种类型的应用程序.您有 MBCS 应用程序和 Unicode 应用程序.


The second thing wrong with your code is that in the Windows world, you have basically two types of applications with respect to character type. You have the MBCS application, and Unicode application.

如果您构建了一个 Unicode 应用程序,您对 MessageBox 的调用将不会成功编译,因为 MessageBox 使用宽字符串,而不是基于字符的字符串.在这种情况下,要使用的正确字符串类型是 std::wstring.

If you built a Unicode application, your calls to MessageBox would not have compiled successfully, since MessageBox takes wide character strings, not char based strings. In this case, the proper string type to use would be std::wstring.

您很可能构建了一个 MBCS 应用程序,这在当今时代变得非常罕见.

You more than likely built an MBCS application, which in this day and age are becoming very rare.

这篇关于Windows API 和字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆