Jump to content
  • Advertisement
Sign in to follow this  
v0dKA

Converting from std::string to LPCWSTR

This topic is 4850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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(), ... );

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!