Char * to LPWSTR

Discussion in 'C++' started by MattWilson.6185@gmail.com, Nov 25, 2006.

  1. Guest

    Hello!

    I am trying to convert a char * to a LPWSTR, and I am going absolutly
    mad! I can't find anything besides typle L"string" Unfortunetaly I
    can't use that...

    basicaly the setup is

    callbackFromAnotherFunctionWhichMustBeOfChar(char* helpMe) {
    //...
    foo(some LPWSTR conversion);
    }

    this is the closest I have come to acomplishing this

    char* convertMe = new char[sizeof(materials.pTextureFilename)];
    strcpy(convertMe,materials.pTextureFilename);
    wchar_t fileNameBuff[1];
    int buffSize =
    MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),fileNameBuff,0);
    LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
    MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),gah,buffSize);

    it converts all of the characters but I get a bunch of junk at the end
    and I can't open the file.

    Thanks :)
    , Nov 25, 2006
    #1
    1. Advertising

  2. Martijn van Buul, Nov 25, 2006
    #2
    1. Advertising

  3. Ron Natalie Guest

    wrote:
    > Hello!
    >
    > I am trying to convert a char * to a LPWSTR, and I am going absolutly
    > mad! I can't find anything besides typle L"string" Unfortunetaly I
    > can't use that...
    >

    L"string" is an array of wchar_t characters.
    The topical question is how to change an array of
    char to an array of wchar_t.

    char narrowarray[] = "Mairzy doats and dozy doats.";
    wchar_t widearray[100]
    mbstowcs(widearray, narrowarray, 100);

    LPSTR is a Microsoft abomination
    Ron Natalie, Nov 25, 2006
    #3
  4. Guest

    > L"string" is an array of wchar_t characters.
    > The topical question is how to change an array of
    > char to an array of wchar_t.
    >
    > char narrowarray[] = "Mairzy doats and dozy doats.";
    > wchar_t widearray[100]
    > mbstowcs(widearray, narrowarray, 100);
    >
    > LPSTR is a Microsoft abomination


    Here Here! Thank you so much for the answer! I now have a curiosity
    question if any are willing to indulge, to solve the problem i had
    earlier I used

    strcpy(convertMe,materials.pTextureFilename);
    wchar_t fileNameBuff[1];
    int buffSize
    =MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),fileNameBuff,0);
    LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
    MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),gah,buffSize);
    /*LOOK HERE!!*/
    wstring final;
    for(int i = 0; i < sizeof(convertMe)*2+1;i++) {
    final.push_back(gah);

    Can anyone tell me, why did I have to use a size of 2 + 1? It makes NO
    sence at all to me, I mean I understand a wchar is twice as many bytes,
    however in traversing the array does it access each part per byte
    (char)? And if so then how does push_back understand it's getting char
    and not wchar? Even when it's clearly being fed a wchar... I should
    be able to say final.push_back(L"w"); not final.push_back"(L"w");
    final.push_back(0); or whatever it be

    thanks ;)

    Matt
    , Nov 25, 2006
    #4
  5. Dag Viken Guest

    <> wrote in message
    news:...
    > Hello!
    >
    > I am trying to convert a char * to a LPWSTR, and I am going absolutly
    > mad! I can't find anything besides typle L"string" Unfortunetaly I
    > can't use that...
    >
    > basicaly the setup is
    >
    > callbackFromAnotherFunctionWhichMustBeOfChar(char* helpMe) {
    > //...
    > foo(some LPWSTR conversion);
    > }


    Matt,
    You should probably post this to: microsoft.public.vc.language

    Your problem is that you are not copying the terminating null.
    Try this :

    int buffSize = MultiByteToWideChar(CP_ACP, 0, convertMe,
    strlen(convertMe)+1, NULL, 0);
    LPWSTR gah = new wchar_t[buffSize];
    MultiByteToWideChar(CP_ACP, 0, convertMe, strlen(convertMe)+1, gah,
    buffSize);

    or, even easier:

    int buffSize = (int)strlen(convertMe) + 1;
    LPWSTR gah = new wchar_t[buffSize];
    MultiByteToWideChar(CP_ACP, 0, convertMe, buffSize, gah, buffSize);

    Now gah will have the null-terminated Unicode string.

    Dag
    Dag Viken, Nov 25, 2006
    #5
  6. Jack Klein Guest

    On 25 Nov 2006 07:54:51 -0800, wrote in
    comp.lang.c++:

    > Hello!
    >
    > I am trying to convert a char * to a LPWSTR, and I am going absolutly
    > mad! I can't find anything besides typle L"string" Unfortunetaly I
    > can't use that...
    >
    > basicaly the setup is
    >
    > callbackFromAnotherFunctionWhichMustBeOfChar(char* helpMe) {
    > //...
    > foo(some LPWSTR conversion);
    > }
    >
    > this is the closest I have come to acomplishing this
    >
    > char* convertMe = new char[sizeof(materials.pTextureFilename)];
    > strcpy(convertMe,materials.pTextureFilename);
    > wchar_t fileNameBuff[1];
    > int buffSize =
    > MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),fileNameBuff,0);
    > LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
    > MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),gah,buffSize);
    >
    > it converts all of the characters but I get a bunch of junk at the end
    > and I can't open the file.
    >
    > Thanks :)


    You say you can't use standard C++ features, and your code uses
    non-standard, Windows specific types and API functions, all off-topic
    here.

    You need to ask this in a Windows group, such as
    news:comp.os.ms-windows.programmer.win32.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Nov 27, 2006
    #6
  7. Tim D Guest

    wrote:

    >> L"string" is an array of wchar_t characters.
    >> The topical question is how to change an array of
    >> char to an array of wchar_t.
    >>
    >> char narrowarray[] = "Mairzy doats and dozy doats.";
    >> wchar_t widearray[100]
    >> mbstowcs(widearray, narrowarray, 100);
    >>
    >> LPSTR is a Microsoft abomination

    >
    > Here Here! Thank you so much for the answer! I now have a curiosity
    > question if any are willing to indulge, to solve the problem i had
    > earlier I used
    >
    > strcpy(convertMe,materials.pTextureFilename);
    > wchar_t fileNameBuff[1];
    > int buffSize
    > =MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),fileNameBuff,0);
    > LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
    > MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),gah,buffSize);
    > /*LOOK HERE!!*/
    > wstring final;
    > for(int i = 0; i < sizeof(convertMe)*2+1;i++) {
    > final.push_back(gah);
    >
    > Can anyone tell me, why did I have to use a size of 2 + 1? It makes NO
    > sence at all to me, I mean I understand a wchar is twice as many bytes,
    > however in traversing the array does it access each part per byte
    > (char)? And if so then how does push_back understand it's getting char
    > and not wchar? Even when it's clearly being fed a wchar... I should
    > be able to say final.push_back(L"w"); not final.push_back"(L"w");
    > final.push_back(0); or whatever it be
    >
    > thanks ;)
    >
    > Matt


    MultiByteToWideChar does not return a nul terminated C-style string, just
    the converted characters. If you want a C-style string, you need to do the
    bookkeeping yourself:

    int buffSize = MultiByteToWideChar(
    CP_ACP,
    0,
    materials.pTextureFilename, // no need to copy the src string
    -1, // -1 means src string is nul terminated
    NULL, // not used when calc'ing size
    0);
    wchar_t gah = new wchar_t[buffSize + 1]; // translated chars plus trailing
    nul
    MultiByteToWideChar(
    CP_ACP,
    0,
    materials.pTextureFilename,
    -1,
    gah,
    buffSize);
    gah[buffSize] = 0; // make it a C-string

    Suppose you did something like this:

    char fred[]="fred";
    char* newfred = new char[strlen(fred)];
    strcpy(newfred,fred);

    You'd blow up because you neglected to include the extra nul that strcpy
    puts on the end of the string. With MultiByteToWideChar, you need to do the
    strcpy-like bookkeeping yourself.
    Tim D, Nov 28, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    542
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,016
    Ian Collins
    May 9, 2006
  3. Replies:
    2
    Views:
    1,170
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Feb 26, 2007
  4. Replies:
    1
    Views:
    504
  5. Trups
    Replies:
    10
    Views:
    3,919
    Nick Keighley
    Nov 4, 2008
Loading...

Share This Page