简体   繁体   English

DLL调用C ++ Windows后出现奇怪的换行符问题

[英]Strange newline issue after DLL call C++ Windows

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. 我正在使用Visual Studio 2010在Windows上用C ++开发32位非托管应用程序。请原谅我缺乏Windows知识,因为我通常在* nix上进行开发。

Initially, in my program my calls to std::cout's stream insertion operator work fine. 最初,在我的程序中,我对std :: cout的流插入运算符的调用工作正常。 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) (倒置菱形符号)您好(八音符符号)(倒置o符号)

Once I have called this DLL for the first time my output to std::cout is forever messed up. 一旦我第一次调用此DLL,我对std :: cout的输出就永远混乱了。 The symbols that are printed are not found in an ASCII table. 被印刷的符号以ASCII表中找到。 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). 倒置的o符号是单个unicode字符,看起来像字母“ o”,但o的黑色部分是白色,白色部分是黑色(倒置颜色)。 The music symbol is the unicode 8th note character. 音乐符号是Unicode八音符。

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. 似乎该DLL弄乱了如何输出控制字符(以\\开头的字符)。


What I have tried so far 到目前为止我尝试过的

I thought this might be a locale issue since the DLL is from a Japanese company. 我认为这可能是语言环境问题,因为DLL来自一家日本公司。 However, after the DLL call the locale is still "C" just as it was before the call. 但是,在DLL调用之后,语言环境仍然是“ C”,就像在调用之前一样。 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. 我还认为这可能是某种奇怪的内存损坏,但似乎\\ r \\ n被替换为(音乐符号)(倒置o),而\\ t被替换为反转的菱形符号。 There seems to be a regular "replace A by B" pattern for all the control chars, which would not indicate memory corruption. 对于所有控制字符,似乎都有一个常规的“ A替换B”模式,这并不表示内存损坏。

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. 在此先感谢您的帮助和见解。

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 您也许可以将另一个流与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 冲洗可能很“尴尬”-但应该可以

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM