How to convert PWSTR to char * ?
-
Here's an example of doing that:
char szBuffer[255];
WideCharToMultiByte(CP_ACP,
0,
/* your PWSTR var goes here */,
-1,
szBuffer,
sizeof(szBuffer),
NULL,
NULL);Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af
-
Here's an example of doing that:
char szBuffer[255];
WideCharToMultiByte(CP_ACP,
0,
/* your PWSTR var goes here */,
-1,
szBuffer,
sizeof(szBuffer),
NULL,
NULL);Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af
Hi Tom I am not at all experienced with ATL, but I was wondering why we cannot use W2A macro to achieve what the guy wanted to do! Nish
The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday
-
Here's an example of doing that:
char szBuffer[255];
WideCharToMultiByte(CP_ACP,
0,
/* your PWSTR var goes here */,
-1,
szBuffer,
sizeof(szBuffer),
NULL,
NULL);Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af
hey tom can you not just use: USES_CONVERSION; *pChar = W2A(*pWideChar); ????
situations to avoid #37:
"good morning ... how many sugars do you take in your coffee ... and what was your name again?"coming soon: situations to avoid #38: "...and the dog was there too?"
-
hey tom can you not just use: USES_CONVERSION; *pChar = W2A(*pWideChar); ????
situations to avoid #37:
"good morning ... how many sugars do you take in your coffee ... and what was your name again?"coming soon: situations to avoid #38: "...and the dog was there too?"
Lauren, I asked Tom this very question just now. I was about 30 seconds later than you were though :-) Nish
The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday
-
Lauren, I asked Tom this very question just now. I was about 30 seconds later than you were though :-) Nish
The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday
hehe great minds nish great minds :)
situations to avoid #37:
"good morning ... how many sugars do you take in your coffee ... and what was your name again?"coming soon: situations to avoid #38: "...and the dog was there too?"
-
hehe great minds nish great minds :)
situations to avoid #37:
"good morning ... how many sugars do you take in your coffee ... and what was your name again?"coming soon: situations to avoid #38: "...and the dog was there too?"
-
hey tom can you not just use: USES_CONVERSION; *pChar = W2A(*pWideChar); ????
situations to avoid #37:
"good morning ... how many sugars do you take in your coffee ... and what was your name again?"coming soon: situations to avoid #38: "...and the dog was there too?"
Yep. Does the same thing and definitely much less typing and remembering of parameter values. Great catch! By the way, check out what these macros expand to :omg:
Original C++ code
void SayHello2(PWSTR wstrName)
{
USES_CONVERSION;
AfxMessageBox(W2A(wstrName));
}Expanded macros
void SayHello2(PWSTR wstrName)
{
int _convert = 0;
_convert;
UINT _acp = ATL::_AtlGetConversionACP() ;
_acp;
LPCWSTR _lpw = 0;
_lpw;
LPCSTR _lpa = 0;
_lpa;AfxMessageBox((((_lpw = wstrName) == 0) ? 0
: (_convert = (lstrlenW(_lpw)+1)*2,
AtlW2AHelper((LPSTR) _alloca(_convert),
_lpw,
_convert,
_acp))));
}Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af
-
hehe great minds nish great minds :)
situations to avoid #37:
"good morning ... how many sugars do you take in your coffee ... and what was your name again?"coming soon: situations to avoid #38: "...and the dog was there too?"
lauren wrote: great minds nish great minds Yeah :-) I guess so :-) Nish
The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday
-
Hi Tom I am not at all experienced with ATL, but I was wondering why we cannot use W2A macro to achieve what the guy wanted to do! Nish
The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday
Like a lot of things in Windows development, there's more than one way to accomplish this task. I'm just more accustomed to using the Win32 functions. As I told Lauren, using the ATL macros is certainly a lot less typing and remembering of parameters and therefore, probably the better way to accomplish this conversion task. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af
-
Yep. Does the same thing and definitely much less typing and remembering of parameter values. Great catch! By the way, check out what these macros expand to :omg:
Original C++ code
void SayHello2(PWSTR wstrName)
{
USES_CONVERSION;
AfxMessageBox(W2A(wstrName));
}Expanded macros
void SayHello2(PWSTR wstrName)
{
int _convert = 0;
_convert;
UINT _acp = ATL::_AtlGetConversionACP() ;
_acp;
LPCWSTR _lpw = 0;
_lpw;
LPCSTR _lpa = 0;
_lpa;AfxMessageBox((((_lpw = wstrName) == 0) ? 0
: (_convert = (lstrlenW(_lpw)+1)*2,
AtlW2AHelper((LPSTR) _alloca(_convert),
_lpw,
_convert,
_acp))));
}Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af
Ouch, that expanded code hurts my eyes :-) Nish
The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday
-
Like a lot of things in Windows development, there's more than one way to accomplish this task. I'm just more accustomed to using the Win32 functions. As I told Lauren, using the ATL macros is certainly a lot less typing and remembering of parameters and therefore, probably the better way to accomplish this conversion task. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af
Tom Archer wrote: Like a lot of things in Windows development, there's more than one way to accomplish this task. I'm just more accustomed to using the Win32 functions. As I told Lauren, using the ATL macros is certainly a lot less typing and remembering of parameters and therefore, probably the better way to accomplish this conversion task. I think it's cool to know multiple ways to do something. It can be quite handy at times. Nish p.s. Got my mail? [with ppt]
The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday
-
Tom Archer wrote: Like a lot of things in Windows development, there's more than one way to accomplish this task. I'm just more accustomed to using the Win32 functions. As I told Lauren, using the ATL macros is certainly a lot less typing and remembering of parameters and therefore, probably the better way to accomplish this conversion task. I think it's cool to know multiple ways to do something. It can be quite handy at times. Nish p.s. Got my mail? [with ppt]
The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday
Especially something like this where the person may need to use the Win32 SDK. Regarding the PPT. Yes, thank you. I replied to you. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af
-
Hi Tom I am not at all experienced with ATL, but I was wondering why we cannot use W2A macro to achieve what the guy wanted to do! Nish
The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday
The most serious drawback of W2A, W2CA, xx2xx, is that for each invocation, new stack space is allocated. So, you can't really use them in a loop. Other than that, I use them all the time. :) Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
-
The most serious drawback of W2A, W2CA, xx2xx, is that for each invocation, new stack space is allocated. So, you can't really use them in a loop. Other than that, I use them all the time. :) Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
Tim Smith wrote: The most serious drawback of W2A, W2CA, xx2xx, is that for each invocation, new stack space is allocated. So, you can't really use them in a loop. I think these issues have been partially improved in ATL 7.0 Nish
The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday