locked
incompatible types - from 'char [4]' to 'LPCWSTR'

    Question

  • Hi

    #include<windows.h>
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    {
        MessageBox(NULL,"test","test",MB_OK);
        return 0;
    }



    a.c
    .\a.c(4) : warning C4133: 'function' : incompatible types - from 'char [4]' to 'LPCWSTR'
    .\a.c(4) : warning C4133: 'function' : incompatible types - from 'char [4]' to 'LPCWSTR'

    how to solve that problem?
    Sunday, May 28, 2006 10:20 PM

Answers

  •  Chris Vega wrote:
    MarianL,


        MessageBox(NULL, (LPTSTR)"test" , (LPTSTR)"test", MB_OK);

     

    -chris

    Chris, I think the problem is that the program is being compiled for Unicode (wide strings), the VC++ 2005 Express Edition default for a Win32 application.  Use of the char[5] array "test" is not compatible. (LPTSTR) might be accepted for an LPCWSTR, but I hope not.

     - Dennis

    PS: Bad news. The (LPTSTR) cast works but the program produces garbage because the cast is not the truth.  I will leave as an exercise why exactly two garbage characters are produced and not some random long sequence of trash instead.

    Monday, May 29, 2006 3:54 AM
  •  MarianL wrote:
    Hi

    #include<windows.h>
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    {
        MessageBox(NULL,"test","test",MB_OK);
        return 0;
    }



    a.c
    .\a.c(4) : warning C4133: 'function' : incompatible types - from 'char [4]' to 'LPCWSTR'
    .\a.c(4) : warning C4133: 'function' : incompatible types - from 'char [4]' to 'LPCWSTR'

    how to solve that problem?

    I am offering two answers.

    First, try L"test" in both places that you have "test".  (This sitll may not be enough, I'm guessing).

    Secondly, here is how to handle compiler errors in general.  The first part of the message names the location of the file and the line of the file that the warning is about.

    You can double-click on the line in the error window and VC++ will bring up the file and move to the correct line.

    Because char[4] is most likely what VC++ says the type of string "test" is, it looks like both of them do not agree with what is required for the two parameters.  (See below about this.)

    To find out what is required for LPCWSTR, you can search for that in VC++ Help Search, and you can search for MessageBox too.  In my LPCWSTR search, I found a link to a previous question, "HELP with LPCWSTR!" that provided the correct answer.  Some of this material is a little obscure, and I found the API page for MessageBox by using a Web Search and coming up with this helpful page.


    I got a different error message than you, and I wonder if your compiler settings are the same as mine (and whether you are using VC++ 2005).  Here's what happened.  (I have the Platform SDK installed, so I am able to work your example.)

    1. I created a project by clicking New | Project ...

    2. I selected project type Win32 and the Win32 Console Application project template.  I named the project MessageTest.  I unchecked Create directory for solution

    3. In the application settings dialog, I selected the Windows Application application type and checked Empty project.

    4. After the solution was opened, I right-clicked the Source Files folder in Solution Explorer and selected Add | New Item ... .  In the Add New Item dialog I selected category Visual C++ and the C++ file (.cpp) installed template.  I named the item MessageTest.

    5. When the blank MessageTest.cpp page opened in VC++, I pasted in the 7 lines of your program.

    6. When I compiled it (F7 key), the error message that I got was as follows (in one line):

    • Description: error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [5]' to 'LPCWSTR'
    • File: MessageTest.cpp
    • Line: 4

    7. In the Output window there is the additional message:

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    This is not all that helpful, but the other information (6) is more accurate than what you reported. 

    8. I have my Project Settings with Warning Level W3 but I do not have warnings treated as errors. 

    I'm curious, what compiler are you using?  My Visual C++ 2005 Express Edition seems more uptodate.

     - Dennis

    Sunday, May 28, 2006 11:42 PM
  • MarianL,

    How are you compiling your program? I mean, your program looks fine. Although not neccessary, you can try casting your literal string to LPTSTR, as follows:



    #include<windows.h>
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    {
        MessageBox(NULL, (LPTSTR)"test" , (LPTSTR)"test", MB_OK);
        return 0;
    }

     


    Regards,

    -chris
    Monday, May 29, 2006 3:18 AM
  •  MarianL wrote:
    yup... with cast: no warnings, but the same result.

    Marian, did you change "test" to L"test" in both places and get a correct compile and execution?

    I was able to do that.  I also documented how I confirmed that was what the error message was about.

     - Dennis

    Monday, May 29, 2006 3:49 PM
  • All replies

    •  MarianL wrote:
      Hi

      #include<windows.h>
      int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
      {
          MessageBox(NULL,"test","test",MB_OK);
          return 0;
      }



      a.c
      .\a.c(4) : warning C4133: 'function' : incompatible types - from 'char [4]' to 'LPCWSTR'
      .\a.c(4) : warning C4133: 'function' : incompatible types - from 'char [4]' to 'LPCWSTR'

      how to solve that problem?

      I am offering two answers.

      First, try L"test" in both places that you have "test".  (This sitll may not be enough, I'm guessing).

      Secondly, here is how to handle compiler errors in general.  The first part of the message names the location of the file and the line of the file that the warning is about.

      You can double-click on the line in the error window and VC++ will bring up the file and move to the correct line.

      Because char[4] is most likely what VC++ says the type of string "test" is, it looks like both of them do not agree with what is required for the two parameters.  (See below about this.)

      To find out what is required for LPCWSTR, you can search for that in VC++ Help Search, and you can search for MessageBox too.  In my LPCWSTR search, I found a link to a previous question, "HELP with LPCWSTR!" that provided the correct answer.  Some of this material is a little obscure, and I found the API page for MessageBox by using a Web Search and coming up with this helpful page.


      I got a different error message than you, and I wonder if your compiler settings are the same as mine (and whether you are using VC++ 2005).  Here's what happened.  (I have the Platform SDK installed, so I am able to work your example.)

      1. I created a project by clicking New | Project ...

      2. I selected project type Win32 and the Win32 Console Application project template.  I named the project MessageTest.  I unchecked Create directory for solution

      3. In the application settings dialog, I selected the Windows Application application type and checked Empty project.

      4. After the solution was opened, I right-clicked the Source Files folder in Solution Explorer and selected Add | New Item ... .  In the Add New Item dialog I selected category Visual C++ and the C++ file (.cpp) installed template.  I named the item MessageTest.

      5. When the blank MessageTest.cpp page opened in VC++, I pasted in the 7 lines of your program.

      6. When I compiled it (F7 key), the error message that I got was as follows (in one line):

      • Description: error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [5]' to 'LPCWSTR'
      • File: MessageTest.cpp
      • Line: 4

      7. In the Output window there is the additional message:

      Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

      This is not all that helpful, but the other information (6) is more accurate than what you reported. 

      8. I have my Project Settings with Warning Level W3 but I do not have warnings treated as errors. 

      I'm curious, what compiler are you using?  My Visual C++ 2005 Express Edition seems more uptodate.

       - Dennis

      Sunday, May 28, 2006 11:42 PM
  • MarianL,

    How are you compiling your program? I mean, your program looks fine. Although not neccessary, you can try casting your literal string to LPTSTR, as follows:



    #include<windows.h>
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    {
        MessageBox(NULL, (LPTSTR)"test" , (LPTSTR)"test", MB_OK);
        return 0;
    }

     


    Regards,

    -chris
    Monday, May 29, 2006 3:18 AM
  •  Chris Vega wrote:
    MarianL,


        MessageBox(NULL, (LPTSTR)"test" , (LPTSTR)"test", MB_OK);

     

    -chris

    Chris, I think the problem is that the program is being compiled for Unicode (wide strings), the VC++ 2005 Express Edition default for a Win32 application.  Use of the char[5] array "test" is not compatible. (LPTSTR) might be accepted for an LPCWSTR, but I hope not.

     - Dennis

    PS: Bad news. The (LPTSTR) cast works but the program produces garbage because the cast is not the truth.  I will leave as an exercise why exactly two garbage characters are produced and not some random long sequence of trash instead.

    Monday, May 29, 2006 3:54 AM
  • I see, thanks for that. I haven't tried VC++ Express Edition yet.

    Cheers,

    -chris

    Monday, May 29, 2006 4:29 AM
  • yup... with cast: no warnings, but the same result.
    Monday, May 29, 2006 9:25 AM
  •  MarianL wrote:
    yup... with cast: no warnings, but the same result.

    Marian, did you change "test" to L"test" in both places and get a correct compile and execution?

    I was able to do that.  I also documented how I confirmed that was what the error message was about.

     - Dennis

    Monday, May 29, 2006 3:49 PM
  • it works now.... with L"string"
    thanks :)
    Tuesday, May 30, 2006 11:27 AM
  • Hi MarianL,
    Each windows function like MessageBox is actually a macro which emits MessageBoxA when UNICODE is not defined, and, MessageBoxW when UNICODE is defined.
    To solve the above problem, you have to write
    MessageBox(NULL, L"test", L"test", MB_OK);
    But after somedays you want to create a ansi version of the code and you undefine UNICODE, then you will have to change the text in all places.There is a better alternative available to this problem, all you have to do is write the statement in the following way
    MessageBox(NULL, TEXT("test"), TEXT("test"), MB_OK);
    Here, the TEXT macro will put an L if UNICODE is defined else write the text as it is.I hope this will answer your query.
    Wednesday, June 21, 2006 4:15 PM
  • Thank you...
    but....
     : error C2065: 'TEXT' : undeclared identifier
    Saturday, June 24, 2006 10:41 AM
  • Hi: make sure you add:

    #include <tchar.h>

    to your includes.

    Saturday, June 24, 2006 1:41 PM
  • Rags/Jonathan

    Hey thanks for the help.  I'm just starting out with forgers win32api tutorial, using 2005 Express Edition.  Your suggestions seem to have done the trick.  

    Thanks guys

    Saturday, July 21, 2007 4:25 AM
  • Is there any tutorial for porting 32-bit API to 64-bit API ? (especially the one that shows the pointer arithmatic in detail)

    Thanks,
    Chintan.
    Monday, July 23, 2007 12:56 PM
  • just go to the project property Pages (Alt + F7).
    In Configuration Properties, go to General and then select Character Set.
    Change to  " Use Multi-Byte Character Set"
    Friday, March 12, 2010 1:59 PM
  • I am sorry about the necro post, but since this thread was in top few hits returned by Google, I thought it prudent to add to this thread rather that attempt to start a new one. I hope this is OK with the moderators.

     

    I just installed VSE 2010, run the same simple program. I found that I had to cast each string as LPCWSTR as well as use the "L" identifier.

     

     

    MessageBox(NULL, (LPCWSTR)L"test", (LPCWSTR)L"test", MB_OK);

    Friday, March 11, 2011 3:41 PM
  • By default, Visual Studio C++ will use Unicode (wide-character) strings, rather than narrow (ANSI) strings. By specifying the "L" prefix, you are indicating that the string should be wide-character.

    The LPCWSTR cast is unnecessary, and you can drop it (but not the "L" prefix).

    Moving forward, it is a good habit to use Unicode strings and not narrow strings.

    Friday, March 11, 2011 9:09 PM