I stumbled upon MSFT KB artice 311259, which suggests 3 solutions, including
the one discussed in this thread.
Alas, none of the 3 solutions appear to solve the problem.
The solutions listed are:
PtrToStringChars, as discussed in this thread.
StringToHGlobalAnsi
CString
>> The supplied function should not be a member function - make it a
>> global or namespace scope function.
[quoted text clipped - 6 lines]
> : no operator found which takes a right-hand operand of type
> 'std::wstring' (or there is no acceptable conversion)
You're using a wide-character string (std::wstring) with a narrow-character
stream (std::ofstream). Unfortunately, that doesn't work.
What you really want is to use a wide-character stream (std::wofstream) with
a narrowing facet (assuming you want ASCII/ANSI text in your outpout file
and not Unicode). Unfortunately, VC++ does not supply a suitable facet
(although the Dinkumware Unabridged library does include one).
What you can do instead is use the .NET framework to do the narrowing
conversion for you:
<untested code>
std::string ToUnmanagedNarrowString(System::String* str)
{
HGLOBAL hg =
(HGLOBAL)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);
const char* pc = static_cast<const char*>GlobalLock(hg);
std::string ret(pc,pc+str->get_Length());
GlobalFree(hg);
return ret;
}
</untested code>
Note that if Unicode output is acceptable (for example, that ofstream
actually goes to the console), simply changing your ofstream to a wofstream
should be sufficient.
-cd
Howard Kaikow - 02 Jun 2005 15:45 GMT
> You're using a wide-character string (std::wstring) with a narrow-character
> stream (std::ofstream). Unfortunately, that doesn't work.
[quoted text clipped - 11 lines]
> {
> HGLOBAL hg =
(HGLOBAL)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str)
;
> const char* pc = static_cast<const char*>GlobalLock(hg);
> std::string ret(pc,pc+str->get_Length());
[quoted text clipped - 6 lines]
> actually goes to the console), simply changing your ofstream to a wofstream
> should be sufficient.
I an writing to a file.
I already display the info in a listbox, I'm just trying to get the stuff
from the listbox into a file, so I can print the file.
Perhaps there's a better way to do that?
Carl Daniel [VC++ MVP] - 02 Jun 2005 15:51 GMT
> "Carl Daniel [VC++ MVP]"
>> Note that if Unicode output is acceptable (for example, that ofstream
[quoted text clipped - 7 lines]
> stuff from the listbox into a file, so I can print the file.
> Perhaps there's a better way to do that?
No, not really. If you're using .NET for your UI (and hence Unicode) and
youi want to write out a narrow-character file, then you have to explicitly
do the narrowing conversion somewhere.
-cd
Howard Kaikow - 02 Jun 2005 16:20 GMT
> No, not really. If you're using .NET for your UI (and hence Unicode) and
> youi want to write out a narrow-character file, then you have to explicitly
> do the narrowing conversion somewhere.
I have no problem using the same expressions when adding to a LIstBox
control.
So, I expect that I should byte the bullet and use the System::IO
namespace.
Carl Daniel [VC++ MVP] - 02 Jun 2005 15:51 GMT
> <untested code>
> std::string ToUnmanagedNarrowString(System::String* str)
> {
> HGLOBAL hg =
> (HGLOBAL)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);
> const char* pc = static_cast<const char*>GlobalLock(hg);
const char* pc = static_cast<const char*>(GlobalLock(hg));
> std::string ret(pc,pc+str->get_Length());
> GlobalFree(hg);
> return ret;
> }
> </untested code>
-cd