convert LPSTR to LPCWSTR

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • X Enterprises
    New Member
    • Dec 2007
    • 15

    convert LPSTR to LPCWSTR

    Hello :)

    I'm not new to C++, I just don't use it as much as I used to.

    But anyway, I need to convert LPSTR to LPCWSTR.

    Here's the function i'm using it in:
    Code:
    MYCOMMAND void PrintText( LPSTR pString )
     {
    	if(pString)
    	{
    		MessageBox(NULL, pString, "", MB_OK);
    	}
     }
    This gives an error.

    Help is appreciated.

    Thanks.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    MessageBox is a macro that calls MessageBoxA or MessageBoxW depending upon the character set of your project.

    Microsoft provides a set of macros called the TCHAR mappings that you are to use in your programs. You might Google TCHAR for more info.

    In your specific case, use the TEXT TCHAR macro:
    Code:
    MessageBox(NULL, TEXT(pString), TEXT(""), MB_OK);

    Comment

    • X Enterprises
      New Member
      • Dec 2007
      • 15

      #3
      that doesn't work.

      I get this error:
      error C2065: 'LpString' : undeclared identifier

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        My mistake. TEXT is used for literals to create an LPCTSTR.

        This is what I should have said:

        Code:
        MessageBox(NULL, (LPCTSTR)(pString), TEXT(""), MB_OK);
        You would typcast your LPSTR to a LPCTSTR which represents an LPCSTR if you program is compiled using ASCII or an LPCWSTR is your code is compiled using Unicode. The MessageBox function needs LPCTSTR arguments.

        The only difference between a LPSTR and an LPCSTR is that the LPCSTR is constant and the LPSTR is not.

        Comment

        • X Enterprises
          New Member
          • Dec 2007
          • 15

          #5
          Thank you. The code compiles and makes the dll and everything. :)

          But, when I run the function, the main text is all garbled and in these Chinese characters and things. Any help with that? How would I convert the LPSTR to a regular string?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Try this:
            Code:
            CHAR pString[] = "Hello world!";
            WCHAR pWideString[80];
            MultiByteToWideChar(CP_ACP,0,pString,-1,pWideString,80);
            MessageBoxW(NULL, pWideString, TEXT(""), MB_OK);
            All MessageBox does is call MessageBoxA or MessageBoxW.

            All MessageBoxA does is convert your LPCTSTR string to a WCHAR string and then calls MessageBoxW.

            That is, it is MessageBoxW that ios always called.

            All this switching between ASCII and Unicode means switching your code between CHAR (char) and WCHAR (wchar_t) automatically. To help this out, Microsoft has developed the TCHAR system of macros. A TCHAR is a char when you are using ASCII (called multibyte by Microsoft) and a wchar_t when you are using Unicode.

            The TCHAR equivalent of your code is:
            Code:
            TCHAR pString[] = TEXT("Hello world!");
            MessageBox(NULL, pString, TEXT(""), MB_OK);
            Here the TEXT macro has properly converted your string.

            You should be using only TCHAR and the TCHAR function mappings in your code. That is, you should not be using things like LPSTR in programs that need to run both multibyte and Unicode.

            Considering that all new code need only run Unicode, use only WCHAR and MessageBoxW.

            Comment

            • X Enterprises
              New Member
              • Dec 2007
              • 15

              #7
              I thank you so much for this explaining. I really do. :)

              I can't use your example because I have to use the LPSTR argument in the function. (It's for a plugin I'm making.)

              But just another question:

              Code:
              string Nstr = pString;
              (I want this because I want to edit the string with string functions.)
              I've compiled it and it compiles fine. How would I convert the string into the type MessageBox needs?

              Thank you so much. :)

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                You mean Nstr?

                If so, you use:
                Code:
                string Nstr = "Hello world";
                WCHAR pWideString[80]; 
                MultiByteToWideChar(CP_ACP,0,Nstr.c_str(),-1,pWideString,80); 
                MessageBoxW(NULL, pWideString, TEXT(""), MB_OK);
                string::c_str() returns a const char* that points to a C-string representing the data in the string object. Use that in your MultuByteToWide Char conversion to Unicode.

                Comment

                • X Enterprises
                  New Member
                  • Dec 2007
                  • 15

                  #9
                  Thank you for all of the help.

                  Luckily, I found a better way to accomplish what I wanted to do with a language I'm more comfortable with. (VB/C#)


                  Thanks again soo much. :)

                  I'm glad I could learn some things from this experience.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    That's true but VB/C# is slower because they contain code to make your life easier. Ease in programming always has a speed penalty.

                    Comment

                    Working...