std::string to const WCHAR*
-
-
I want to draw a string (from a variable) with GDI+ so I'm trying to do this: m_graphics->DrawString(m_ac, -1, &font, PointF(8,8), NULL, &grnbrush); but the member m_ac is a std::string. How can I get it into the const WCHAR* format for this call?
Stick^ wrote:
How can I get it into the const WCHAR* format for this call?
At a minimum:
m_graphics->DrawString(m_ac**.c_str()**, -1, &font, PointF(8,8), NULL, &grnbrush);
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
I want to draw a string (from a variable) with GDI+ so I'm trying to do this: m_graphics->DrawString(m_ac, -1, &font, PointF(8,8), NULL, &grnbrush); but the member m_ac is a std::string. How can I get it into the const WCHAR* format for this call?
MultiByteToWideChar(...)
is the de-facto way to convert from ANSI to Unicode (Wide) strings, so you can look into that. For something a little more Q&D, look into the (ATL) Conversion Macros likeA2WC(...)
,T2WC(...)
, etc. Note that on VC++ 6.0, these macros use the_alloc(...)
function (if a conversion is required) so you have to be careful using them directly in loops. Peace!-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
Stick^ wrote:
How can I get it into the const WCHAR* format for this call?
At a minimum:
m_graphics->DrawString(m_ac**.c_str()**, -1, &font, PointF(8,8), NULL, &grnbrush);
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
Does that only work if the string is of type
std::wstring
? Peace!-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
I want to draw a string (from a variable) with GDI+ so I'm trying to do this: m_graphics->DrawString(m_ac, -1, &font, PointF(8,8), NULL, &grnbrush); but the member m_ac is a std::string. How can I get it into the const WCHAR* format for this call?
Use a
wstring
and call the c_str() method on it.If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Does that only work if the string is of type
std::wstring
? Peace!-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!)The
c_str()
method is a member of bothstring
andwstring
. Is that what you are asking?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Stick^ wrote:
How can I get it into the const WCHAR* format for this call?
At a minimum:
m_graphics->DrawString(m_ac**.c_str()**, -1, &font, PointF(8,8), NULL, &grnbrush);
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
MultiByteToWideChar(...)
is the de-facto way to convert from ANSI to Unicode (Wide) strings, so you can look into that. For something a little more Q&D, look into the (ATL) Conversion Macros likeA2WC(...)
,T2WC(...)
, etc. Note that on VC++ 6.0, these macros use the_alloc(...)
function (if a conversion is required) so you have to be careful using them directly in loops. Peace!-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
Use a
wstring
and call the c_str() method on it.If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
The
c_str()
method is a member of bothstring
andwstring
. Is that what you are asking?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
No, I am verifying that even though he is using
std::string
(ANSI) instead ofstd::wstring
(Unicode/Wide), he can still expect to get aconst WCHAR*
returned bystd::string::c_str()
? I am under the impression that he will get aconst **char***
fromstd::string::c_str()
, and aconst **WCHAR***
(orconst wchar_t*
) fromstd::**w**string::c_str()
. Peace!-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
Don't think I can use ATL and not sure how I would do that anyway yet. I'm programming a win32 dll.
You can use the conversion macros without adding "real" ATL support to your project. Anyway,
MultiByteToWideChar(...)
is likely the way to go - look it up in MSDN for usage info. Peace!-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
No, I am verifying that even though he is using
std::string
(ANSI) instead ofstd::wstring
(Unicode/Wide), he can still expect to get aconst WCHAR*
returned bystd::string::c_str()
? I am under the impression that he will get aconst **char***
fromstd::string::c_str()
, and aconst **WCHAR***
(orconst wchar_t*
) fromstd::**w**string::c_str()
. Peace!-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!)Because
string.c_str()
returns achar*
is why I indicated it would need to be a minimum. A typecast would still need to be applied (to make it wide). I don't use the STL, but a better solution would be to usewstring
instead.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
ummm seems that would do the reverse of what I want. I want to go to a wide string from a cstr.
As I do not use the STL, I did not want to commit to a definite solution. That's why I prefaced it with, "At a minimum." From there you could have just cast it to a
WCHAR*
. A better solution is to use awstring
instead.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Stick^ wrote:
Would if I could, but I am given a c_str and must convert it to a wide, hence the question.
c_str()
is a method ofwstring
. No conversion is necessary as it is already "wide." Now if you are unable to changestd::string
tostd::wstring
, that's a different problem.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
You are given a
const char*
you mean? If that is the case, use MultiByteToWideChar to convert theconst char*
to an array ofconst wchar_t*
and pass that to your draw function (NOTE: that will only work for UNICODE builds -- for a more generic approach, look at the A2T macro)If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
You are given a
const char*
you mean? If that is the case, use MultiByteToWideChar to convert theconst char*
to an array ofconst wchar_t*
and pass that to your draw function (NOTE: that will only work for UNICODE builds -- for a more generic approach, look at the A2T macro)If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
No. I'm given a std::string... here's the declaration: string m_ac; Now, if I attempt: m_graphics->DrawString(m_ac, -1, &font, PointF(8,8), NULL, &grnbrush); I get: Error 1 error C2664: 'Gdiplus::Status Gdiplus::Graphics::DrawString(const WCHAR *,INT,const Gdiplus::Font *,const Gdiplus::RectF &,const Gdiplus::StringFormat *,const Gdiplus::Brush *)' : cannot convert parameter 1 from 'std::string' to 'const WCHAR *' so as you can clearly see, the m_ac is NOT already wide. I have tried to do something like this: WCHAR str[20+1] = L""; WideCharToMultiByte(CP_ACP, 0, m_ac.c_str(), -1, str, wcslen(str)+1, NULL, NULL); but this does not work either. Nor does just m_ac in parameter 3.
-
Because
string.c_str()
returns achar*
is why I indicated it would need to be a minimum. A typecast would still need to be applied (to make it wide). I don't use the STL, but a better solution would be to usewstring
instead.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
The const char* returned by c_str() is not a wide string, while casting it to WCHAR* will allow the code to compile, it will not work correctly. Peace!
-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
The const char* returned by c_str() is not a wide string, while casting it to WCHAR* will allow the code to compile, it will not work correctly. Peace!
-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!)James R. Twine wrote:
while casting it to WCHAR* will allow the code to compile, it will not work correctly.
From the context, I think he meant casting the entire string, and not the pointer. :)
-- Mr. Bender's Wardrobe by ROBOTANY 500
-
No. I'm given a std::string... here's the declaration: string m_ac; Now, if I attempt: m_graphics->DrawString(m_ac, -1, &font, PointF(8,8), NULL, &grnbrush); I get: Error 1 error C2664: 'Gdiplus::Status Gdiplus::Graphics::DrawString(const WCHAR *,INT,const Gdiplus::Font *,const Gdiplus::RectF &,const Gdiplus::StringFormat *,const Gdiplus::Brush *)' : cannot convert parameter 1 from 'std::string' to 'const WCHAR *' so as you can clearly see, the m_ac is NOT already wide. I have tried to do something like this: WCHAR str[20+1] = L""; WideCharToMultiByte(CP_ACP, 0, m_ac.c_str(), -1, str, wcslen(str)+1, NULL, NULL); but this does not work either. Nor does just m_ac in parameter 3.
Have you tried "mbstowcs" or "mbstowcs_s" in stdlib.h.
std::string mbstr = "The string!"; std::vector<wchar_t> wcstr(mbstr.size() + 1, L'\0'); size_t written = 0; mbstate_t state = {0}; const char* pmbstr = mbstr.c_str(); errno_t result = mbsrtowcs_s(&written, &wcstr[0], wcstr.size(), &pmbstr, mbstr.size(), &state); std::wcout << &wcstr[0] << std::endl;
-- modified at 20:19 Thursday 28th September, 2006 -
James R. Twine wrote:
while casting it to WCHAR* will allow the code to compile, it will not work correctly.
From the context, I think he meant casting the entire string, and not the pointer. :)
-- Mr. Bender's Wardrobe by ROBOTANY 500
You mean to create a temporary, as in
wstring( sTheString.c_str() )
? I do not think that will work, either... Unless I am missing the intent of your smiley, that is. Peace!-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!)