none
MultiByteToWideChar

    Question

  • I am calling this function in a function of mine which is returning a the converted WCHAR* string. Is it good pratice to return a pointer variable which is defined locally in the function?, secondly I am assuming that MultiByteToWideChar is creating the string on the heap but when I tried to use delete with it, but it gave runtime heap error.
    Wednesday, June 20, 2007 8:36 AM

Answers

  • You have to create the string itself on your own before you pass it in, it just populates the string.

     

    Something like:

     

    WCHAR *buff = new WCHAR[10];

    char *hello = "hello";

     

    MultiByteToWideChar(CP_UTF8, 0, hello, 0, buff, 10);

     

    Replace CP_UTF8 with your codepage of choice, the 0 before hello specifies default flags, the 0 after specifies for the function to automatically find the length of hello by looking for the first null-terminator, if you know it isn't a null-terminated string you can specify its length here, the 10 after buff is the size of buff.

    Wednesday, June 20, 2007 8:46 AM
  • IMO, it's better to take an allocated pointer or wstring as input parameter to hold your string. If you are returning a pointer, it should be valid after the function call. I mean either the pointer should be allocated using new and it should be free by the caller function.

    The following method will be safer rather than returning pointer from a function
    void Convert(char* pChar_i , wstring& wsConverted_o )
    {
     // Do the conversion
    }

    You can also use an allocated pointer instead of wstring
    Wednesday, June 20, 2007 8:48 AM

All replies

  • You have to create the string itself on your own before you pass it in, it just populates the string.

     

    Something like:

     

    WCHAR *buff = new WCHAR[10];

    char *hello = "hello";

     

    MultiByteToWideChar(CP_UTF8, 0, hello, 0, buff, 10);

     

    Replace CP_UTF8 with your codepage of choice, the 0 before hello specifies default flags, the 0 after specifies for the function to automatically find the length of hello by looking for the first null-terminator, if you know it isn't a null-terminated string you can specify its length here, the 10 after buff is the size of buff.

    Wednesday, June 20, 2007 8:46 AM
  • Also, you can return a pointer fine as long as you remember to delete it because you're just returning the address of some memory. Never return a local reference Smile
    Wednesday, June 20, 2007 8:47 AM
  • IMO, it's better to take an allocated pointer or wstring as input parameter to hold your string. If you are returning a pointer, it should be valid after the function call. I mean either the pointer should be allocated using new and it should be free by the caller function.

    The following method will be safer rather than returning pointer from a function
    void Convert(char* pChar_i , wstring& wsConverted_o )
    {
     // Do the conversion
    }

    You can also use an allocated pointer instead of wstring
    Wednesday, June 20, 2007 8:48 AM