cannot convert from  'char [1024]' to 'LPCWSTR'

LearnerMan
LearnerMan used Ask the Experts™
on
I have the following in my MFC project:

char msg[1024];
strcpy (msg, "Unknown media type ");
MessageBox (NULL, msg, _T("WARNING!"), MB_OK);

I get the following error:
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [1024]' to 'LPCWSTR'


How do I fix this?????
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Many options. Two most recommended:
1. Use WCHAR. Your code will look like:
LPCWSTR msg = "Unknown media type";
MessageBox (NULL, msg, L"WARNING!", MB_OK | MB_SETFOREGROUND);

or, if you prefer:
WCHAR msg[1024] = { 0 };
wcscpy(msg, L"Unknown media type");
MessageBox (NULL, msg, L"WARNING!", MB_OK | MB_SETFOREGROUND);

2. In the project settings, on the main page, set the Multibyte Character Set.
By default new project in VS support Unicode. This is native for Windows. If you want to use char for Win API functions, set Multibyte Character Set.

Commented:
Actually, this question is in the MFC zone, so you could use CString class and do not know about this character sets.
CString msg;
msg = _T("Unknown Media type";
MessageBox(msg);
 
AndyAinscowFreelance programmer / Consultant

Commented:
Your problem - you are mixing ANSI (single width chars) and UNICODE (double width chars).

Either use one or the other as pgnatyuk suggests.
OR
convert if you have a char input at one side and require a UNICODE then you can use mbstowcs:

The mbstowcs function converts up to a maximum number of count multibyte characters pointed to by mbstr to a string of corresponding wide characters that  are determined by the current locale. It stores the resulting wide-character  string at the address represented by wcstr.


from the help files:
// crt_mbstowcs.c
// compile with: /W1
// illustrates the behavior of the mbstowcs function

#include <stdlib.h>
#include <stdio.h>
#include <locale.h>

int main( void )
{
    size_t size;
    int nChar = 2; // number of characters to convert
    int requiredSize;

    unsigned char    *pmbnull  = NULL;
    unsigned char    *pmbhello = NULL;
    char* localeInfo;
   
    wchar_t *pwchello = L"\x3042\x3043"; // 2 Hiragana characters
    wchar_t *pwc;

    /* Enable the Japanese locale and codepage */
    localeInfo = setlocale(LC_ALL, "Japanese_Japan.932");
    printf("Locale information set to %s\n", localeInfo);
   
    printf( "Convert to multibyte string:\n" );

    requiredSize = wcstombs( NULL, pwchello, 0); // C4996
    // Note: wcstombs is deprecated; consider using wcstombs_s
    printf("  Required Size: %d\n", requiredSize);

    /* Add one to leave room for the null terminator. */
    pmbhello = (unsigned char *)malloc( requiredSize + 1);
    if (! pmbhello)
    {
        printf("Memory allocation failure.\n");
        return 1;
    }
    size = wcstombs( pmbhello, pwchello, requiredSize + 1); // C4996
    // Note: wcstombs is deprecated; consider using wcstombs_s
    if (size == (size_t) (-1))
    {
        printf("Couldn't convert string. Code page 932 may"
                " not be available.\n");
        return 1;
    }
    printf( "  Number of bytes written to multibyte string: %u\n",
            (unsigned int) size );
    printf( "  Hex values of the " );
    printf( " multibyte characters: %#.2x %#.2x %#.2x %#.2x\n",
            pmbhello[0], pmbhello[1], pmbhello[2], pmbhello[3] );
    printf( "  Codepage 932 uses 0x81 to 0x9f as lead bytes.\n\n");

    printf( "Convert back to wide-character string:\n" );

    /* Assume we don't know the length of the multibyte string.
     Get the required size in characters, and allocate enough space. */

    requiredSize = mbstowcs(NULL, pmbhello, 0); // C4996
    /* Add one to leave room for the NULL terminator */
    pwc = (wchar_t *)malloc( (requiredSize + 1) * sizeof( wchar_t ));
    if (! pwc)
    {
        printf("Memory allocation failure.\n");
        return 1;
    }
    size = mbstowcs( pwc, pmbhello, requiredSize + 1); // C4996
    if (size == (size_t) (-1))
    {
       printf("Couldn't convert string--invalid multibyte character.\n");
    }
    printf( "  Characters converted: %u\n", (unsigned int)size );
    printf( "  Hex value of first 2" );
    printf( " wide characters: %#.4x %#.4x\n\n", pwc[0], pwc[1] );
    free(pwc);
    free(pmbhello);
}
HI. One simple solution is there. Either you set your project properties to "Using Multibyte Character Set" or " Not Set. Hope this will solve your problem.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial