DLL 调用 C++ 后出现奇怪的换行问题视窗

发布于 2024-11-16 21:37:28 字数 1400 浏览 6 评论 0原文

问题

我正在使用 Visual Studio 2010 在 Windows 上用 C++ 开发一个 32 位非托管应用程序。请原谅我缺乏 Windows 知识,因为我通常在 *nix 上进行开发。

最初,在我的程序中,我对 std::cout 的流插入运算符的调用工作正常。例如,以下语句按预期输出:

std::cout << "hello" << std::endl;

但是,以下代码不起作用:

std::cout << "\thello" << std::endl;
...call to DLL from Japanese company who won't respond to support requests...
std::cout << "\thello" << std::endl;

上面的代码打印:

<前><代码>你好

(倒菱形符号)你好(八分音符音乐符号)(倒o符号)

一旦我第一次调用这个 DLL,我对 std::cout 的输出就永远混乱了。打印的符号在 ASCII 表中找不到。倒置的 o 符号是一个单一的 unicode 字符,看起来像字母“o”,但 o 的黑色部分是白色,白色部分是黑色(反转颜色)。音乐符号是 unicode 八分音符字符。

关于为什么会发生这种情况以及如何解决它有什么想法吗?看来这个 DLL 搞乱了控制字符(以 \ 开头的字符)的输出方式。


到目前为止我所尝试的

我认为这可能是一个区域设置问题,因为该 DLL 来自一家日本公司。但是,在 DLL 调用之后,区域设置仍然是“C”,就像调用之前一样。我使用以下命令来查询语言环境:

printf ("Locale is: %s\n", setlocale(LC_ALL,NULL) );

我还认为这可能是某种奇怪的内存损坏,但似乎 \r\n 被 (音乐符号)(倒 o) 替换,而 \t 被倒钻石替换象征。所有控制字符似乎都有一个常规的“用 B 替换 A”模式,这并不表示内存损坏。

最后,我也尝试了这个:

std::cout << "blah" << '\r' << '\n';

并且我看到了由以下内容创建的相同垃圾字符:

std::cout << "blah" << std::endl;


Thanks in advance for any help and insight.

The Problem

I'm developing an 32 bit unmanaged application in C++ on Windows using Visual Studio 2010. Forgive my lack of Windows knowledge as I usually develop on *nix.

Initially, in my program my calls to std::cout's stream insertion operator work fine. For example, the following statement outputs as expected:

std::cout << "hello" << std::endl;

However, the following code does not work:

std::cout << "\thello" << std::endl;
...call to DLL from Japanese company who won't respond to support requests...
std::cout << "\thello" << std::endl;

The above code prints:

hello

(inverted diamond symbol)hello(eighth note music symbol)(inverted o symbol)

Once I have called this DLL for the first time my output to std::cout is forever messed up. The symbols that are printed are not found in an ASCII table. The inverted o symbol is a single unicode char that looks like the letter 'o' but the black part of the o is white, and the white part is black(inverted colors). The music symbol is the unicode 8th note character.

Any ideas on why this is happening and how to fix it? It seems that this DLL is messing up how control characters (chars starting with \) are outputted.


What I have tried so far

I thought this might be a locale issue since the DLL is from a Japanese company. However, after the DLL call the locale is still "C" just as it was before the call. I use the following to query the locale:

printf ("Locale is: %s\n", setlocale(LC_ALL,NULL) );

I also thought this might be some kind of bizarre memory corruption but it seems that the \r\n gets replaced by (music symbol)(inverted o) whereas \t gets replaced by an inverted diamond symbol. There seems to be a regular "replace A by B" pattern for all the control chars, which would not indicate memory corruption.

Lastly, I also tried this:

std::cout << "blah" << '\r' << '\n';

and I see the same garbage characters created by:

std::cout << "blah" << std::endl;


Thanks in advance for any help and insight.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟到的我 2024-11-23 21:37:28

看看这是否修复了它:

#include <iostream>
#include <locale>

int main()
{
    std::cout << "\thello" << std::endl;
    // ...call to DLL from Japanese company who won't respond to support requests...
    locale mylocale("");  // or "C"     // Construct locale object with the user's default preferences
    std::cout.imbue( mylocale );   // Imbue that locale
    std::cout << "\thello" << std::endl;  
    return 0;
}

查阅该库的文档,

  1. 区域设置的更改是否是设计使然
  2. ,是否可以配置,否则

可以也许将另一个流与 cout 关联

std::ostream cout2;
cout2.rdbuf(std::cout.rdbuf());

并使用它。我确信这不会是线程安全的。冲洗可能会“尴尬” - 但它应该有效

See whether this fixes it:

#include <iostream>
#include <locale>

int main()
{
    std::cout << "\thello" << std::endl;
    // ...call to DLL from Japanese company who won't respond to support requests...
    locale mylocale("");  // or "C"     // Construct locale object with the user's default preferences
    std::cout.imbue( mylocale );   // Imbue that locale
    std::cout << "\thello" << std::endl;  
    return 0;
}

Consult the documentation for that library whether

  1. the change of locale is by design
  2. it can be configured otherwise

You could perhaps associate another stream with cout

std::ostream cout2;
cout2.rdbuf(std::cout.rdbuf());

And use it. I'm sure that won't be thread safe. Flushing might be 'awkward' - but it should work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文