Sign in to follow this  

How do you convert from 'const wchar_t *' to 'WCHAR [32]'

This topic is 2959 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

Hi, Trying to figure out a conversion. I am passing in a std::wstring refrence to a function and then trying to assign it to a D3DXFONT_DESC member. int CreateFont(std::wstring& name) { D3DXFONT_DESC lf. lf.FaceName = name.ctr(); } This gives error C2440: '=' : cannot convert from 'const wchar_t *' to 'WCHAR [32]' Any suggestions? Regards Chad

Share this post


Link to post
Share on other sites
Quote:
Original post by Fox32
You need to memcpy or strcpy the string from name.c_str() to the wchar_t array. Remember that lf.FaceName must be a null terminated string!


You will probably not want to use strcpy() since it is not designed to handle multibyte strings. memcpy() will not null-terminate for you. According to MSDN the function wcscpy() can be used; it has the same signature as strcpy() but takes widechar arguments.

EDIT: And obviously you need to be careful not to overflow the buffer too. :)

Share this post


Link to post
Share on other sites
Quote:
And obviously you need to be careful not to overflow the buffer too. :)


wcscpy_s would solve this problem.

Edit:


errno_t result = wcscpy_s(lf.FaceName, name.ctr());




If result is non zero, your name is probably longer than 31 characters and than lf.FaceName isn't modified.

Share this post


Link to post
Share on other sites
Excuse me if I am a dope but I am trying this and it is not working.

int CreateFont(std::wstring& name)
{
D3DXFONT_DESC lf.
assert(fontName.length() < sizeof(lf.FaceName));
strncpy_s(lf.FaceName, sizeof(lf.FaceName), fontName.c_str(), fontName.length() + 1);
}


error C2664: 'errno_t strncpy_s(char *,rsize_t,const char *,rsize_t)' : cannot convert parameter 1 from 'WCHAR [32]' to 'char *'

Regards

Chad


Share this post


Link to post
Share on other sites
Hmm this seem sto be compiling fine now but I am worried about overflow

int CreateFont(std::wstring& name)
{
D3DXFONT_DESC lf.
assert(fontName.length() < sizeof(lf.FaceName));
wcscpy_s(lf.FaceName, sizeof(lf.FaceName), fontName.c_str());
}

how would you suggest I check for them.

Regards

Chad

Share this post


Link to post
Share on other sites
Quote:
Original post by chadsxe
error C2664: 'errno_t strncpy_s(char *,rsize_t,const char *,rsize_t)' : cannot convert parameter 1 from 'WCHAR [32]' to 'char *'


If you use wide strings, you need to use the wide string version of strncpy_s called wcsncpy_s.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
That depends on where the string is coming from. If it's chosen by the user, definitely not.


It is chosen by the user. What steps can I take to assure that the user is under or equal to or less then 31 characters.

Regards

Chad

Share this post


Link to post
Share on other sites
Quote:
Original post by chadsxe
int CreateFont(std::wstring& name)
{
D3DXFONT_DESC lf.
assert(fontName.length() < sizeof(lf.FaceName));
wcscpy_s(lf.FaceName, sizeof(lf.FaceName), fontName.c_str());
}
sizeof returns the size in bytes, so if lf.FaceName is a wide string, the length of the string is actually half of what sizeof returns. But actually, you probably don't even need to worry about that. Try something like this:

int CreateFont(std::wstring &name)
{
D3DXFONT_DESC lf;
wcscpy_s(lf.FaceName, name.c_str());
}
A few things to point out:

The second parameter to wcscpy_s is only needed if you don't know at compile-time the size of the buffer. Otherwise, some template magic is used to insert the correct value, and that's what I'm doing here.

In this case, you can probably get away with simply truncating the string if it's too long. You've really got to consider it on a case-by-case basis, though. That's the difficulty of working with C APIs, unfortunately... (wcscpy_s will truncate the string if it's too long).

Share this post


Link to post
Share on other sites
Quote:
Original post by chadsxe
int CreateFont(std::wstring& name)
{
D3DXFONT_DESC lf.
assert(fontName.length() < sizeof(lf.FaceName));
wcscpy_s(lf.FaceName, sizeof(lf.FaceName), fontName.c_str());
}
sizeof returns the size in bytes, so if lf.FaceName is a wide string, the length of the string is actually half of what sizeof returns. But actually, you probably don't even need to worry about that. Try something like this:

int CreateFont(std::wstring &name)
{
D3DXFONT_DESC lf;
wcscpy_s(lf.FaceName, name.c_str());
}
A few things to point out:

The second parameter to wcscpy_s is only needed if you don't know at compile-time the size of the buffer. Otherwise, some template magic is used to insert the correct value, and that's what I'm doing here.

In this case, you can probably get away with simply truncating the string if it's too long. You've really got to consider it on a case-by-case basis, though. That's the difficulty of working with C APIs, unfortunately... (wcscpy_s will truncate the string if it's too long).

Share this post


Link to post
Share on other sites

This topic is 2959 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this