开发者

Strange newline issue after DLL call C++ Windows

开发者 https://www.devze.com 2023-03-14 03:06 出处:网络
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.

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 bef开发者_运维技巧ore 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消