WRL Helper class to convert Platform::String^ (PCWSTR) to ANSI char* ?


  • ATL had easy macros to convert (securely) between the different character formats. Does WRL provide similar functionality?

    Whats the recommended way to do that, if one does not want to use WideCharToMultiByte and friends all over the place (buffer overflow errors waiting to happen).

    Thursday, April 19, 2012 8:47 AM

All replies

  • Hello,

    You can write this function by yourself, like this:

    	String^ str1 = "AAAAAAAA";
    	wstring wstr( str1->Data() ); 
    	int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL); 
    	char* buffer = new char[len + 1];  
    	WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL); 
    	buffer[len] = '\0'; 


    Best regards,

    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Friday, April 20, 2012 5:28 AM
  • There are no built in WRL conversion macro's. A full-blown scenario might help convince the developers to add them (no promises). The response I got to an internal mail asking about conversion macro's was 'why do you need them'?


    Friday, April 20, 2012 5:13 PM
  • Well, we need it for 3rd party libraries that do not handle wide chars. As they say on the build// conference. "Use whatever library you want" ;) We do, and run into all kind of problems ;) Character mapping is only one of them.

    For now we could use the ATL macros provided for char conversion though.

    Saturday, April 21, 2012 8:16 AM
  • I've sent this feedback to the WRL team. Regarding using ATL conversion macro's, I can't really say yea or nay. I did a quick test and it seems to work.  ATL in general isn't compatible with Metro style applications due to lack of registry access so it's not going to be a deeply tested scenario, please take that into consideration in your own test plan.

    Wednesday, April 25, 2012 11:27 PM