Converting from std::string to LPCWSTR

Started by
17 comments, last by JoshM 18 years, 8 months ago
I'm trying to use FindFirstFile() in my project. The first argument to that function is a LPCWSTR string (for some reason, MSDN and my build log differ on what the first argument is, but I'd rather listen to my compiler). The string I would like to pass as the first argument is stored in std::string in my project, and I need to somehow convert it to LPCWSTR. Any ideas?
.:<<-v0d[KA]->>:.
Advertisement
If the string were in a std::wstring, you could directly use std::wstring::c_str(), but since it is in a narrow-character string, you'll need to convert it to wide characters first. Give me five minutes to find out the easiest way to do that.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
You may want to try using the mbstowcs() function. Alternately, it sounds like your project is being compiled with UNICODE defined, you may be able to disable this in your project properties.
Hmmm, still getting errors. Here's what I tried:

std::string b = ...;LPCWSTR a;mbstowcs( a, b.c_str(), b.size() );FindFirstFile( a, ... );


The error happens at mbstowcs():
error C2664: 'mbstowcs' : cannot convert parameter 1 from 'LPCWSTR' to 'wchar_t *'
Conversion loses qualifiers

What did I do wrong?
.:<<-v0d[KA]->>:.
A LPCWSTR is a pointer to a const wide character string. To use mbstowcs() you need to pass it a pointer to an array of wide characters (wchar_t) to fill with the wide characters that have been converted from the narrow characters.
I don't think std::string guarantees contiguous storage, so that's the best I can come up with to do the conversion.

std::wstring widen(const std::string& str){   const std::string::size_type size = str.size();   std::vector<wchar_t> vec(size);   typedef std::ctype<wchar_t> facet_t;   facet_t& facet = std::use_locale<facet_t>( std::locale() );   facet.widen( str.data(), str.data() + size, &vec.front() );   return std::wstring( vec.begin(), vec.end() );}FindFirstFile( widen(str).c_str(), ... );
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
All right, that problem was solved. How about this next one:

std::string filename = filedata.cFileName;

The compiler is flagging this line because it can't convert from WCHAR [260] to std::string. I never had this problem until I installed the Beta of VC++ 2005 Express, which seems to really nitpick variable, particularly string, types.

How do I convert from WCHAR to std::string?
.:<<-v0d[KA]->>:.
The reverse of mbstowcs() is wcstombs(). Again, I'd check your project to see if you can't get your project built with narrow characters. Or just use std::wstring in the first place.
That's the inverse problem. You have an array of wide characters, so you'll want to use a narrow() function instead of widen().

std::string narrow(const std::wstring& str){   const std::wstring::size_type size = str.size();   std::vector<char> vec(size);   typedef std::ctype<wchar_t> facet_t;   facet_t& facet = std::use_locale<facet_t>( std::locale() );   facet.narrow( str.data(), str.data() + size, &vec.front() );   return std::string( vec.begin(), vec.end() );}


Note that this probably isn't the most efficient code ever...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Well, most of my string problems were fixed. Besides narrow and wide, are there other string formats I need to be aware of?

How do most people deal with strings? Back when I used VC++ 6.0, std::string was the solution to all my problems - I simply didn't have to worry about different formats. When I downloaded the Express betas, it complained where 6.0 didn't. It seems to really nitpick different variable types, and to be perfectly honest, it's beginning to annoy me.

std::string has heaps of useful operators and member functions, and I've been using it for years. If a certain function did not accept it as an argument, I would just use the c_str() member function. Ever since I downloaded VC 2005 betas, I have to spend time figuring out how to convert strings for any functions that takes strings as arguments.

So I was thinking, is there a better way to deal with strings? Or is mbstowcs() and wcstombs() (or Fruny's code, though looking at it gives me a headache) enough to get me out of most string problems?
.:<<-v0d[KA]->>:.

This topic is closed to new replies.

Advertisement